GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jan-2007, 09:23
NONAME123123123 NONAME123123123 is offline
New Member
 
Join Date: Jan 2007
Posts: 2
NONAME123123123 is on a distinguished road

5x5 Array Help?


hey everyone i need to write a program using a 5 by 5 array. and im definately confused.
heres what i need to do

Write a program that reads in a 5x5 array, prints out the sum of the rows and the sum of the columns of the array.


heres what i got so far. but i dont kno if im on the right track or not?


CPP / C++ / C Code:
main ()
{
int num [5][5];
int counter=0;
int i=0;
int j=0;
int sum;

for(i=0;i<5;++i)
   for(j=0;j<5;++j)
{
printf("Enter Element=>",i,j);
scanf("%i",&num [i][j]);
}

sum=0;
for(i=0;i<5;++i)
   for(j=0;j<5;++j)
       sum+=num[i][j];
printf("element %i,%i =>%i \n",i,j,num [i][j]);


system("pause");
return 0;
}

Thanks everyone

x--Jay--x
Last edited by LuciWiz : 11-Jan-2007 at 09:29. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 11-Jan-2007, 11:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: 5x5 Array Help?


Quote:
Originally Posted by NONAME123123123
hey everyone i need to write a program using a 5 by 5 array. and im definately confused.
heres what i need to do

Write a program that reads in a 5x5 array, prints out the sum of the rows and the sum of the columns of the array.


heres what i got so far. but i dont kno if im on the right track or not?
You're getting there...

CPP / C++ / C Code:
main ()    // main is an INT function -- always
{          // You should ALWAYS indent after a {
int num [5][5];
int counter=0;
int i=0;
int j=0;
int sum;

for(i=0;i<5;++i)
   for(j=0;j<5;++j)
{
printf("Enter Element=>",i,j);  // Indent these too.  Also, display i & j so 
scanf("%i",&num [i][j]);        //   you know what element you are entering
}

sum=0;          // Move this line between the FOR statements
                // This way you will sum one full row and zero out
                //   the answer for the next row
                // And always put {'s around the FOR statement bodies
for(i=0;i<5;++i)
{
   for(j=0;j<5;++j)
   {
       sum+=num[i][j];
   }
// here is where one row has been calculated.  Output the answer here
}
printf("element %i,%i =>%i \n",i,j,num [i][j]);  // kill this

// Repeat the above loop with minor changes to sum the columns.


system("pause");  // never use this statement -- use getchar() instead
return 0;
}
On the system("pause"); question, see this
__________________

Age is unimportant -- except in cheese
  #3  
Old 12-Jan-2007, 07:49
davis
 
Posts: n/a

Re: 5x5 Array Help?


Quote:
Originally Posted by NONAME123123123
hey everyone i need to write a program using a 5 by 5 array. and im definitely confused.
heres what i need to do

Write a program that reads in a 5x5 array, prints out the sum of the rows and the sum of the columns of the array.


heres what i got so far. but i dont kno if im on the right track or not?

Thanks everyone

x--Jay--x


You're fairly close. You probably don't want to pre-increment your loop conditional variables. You probably want to post-increment them.

I don't know why WaltP says that "main is an INT function." Main returns an int, but main is not an "int function," since we do not describe a function by its return type. For example, if a function returned a pointer to a dynamically allocated user-defined structure of say a Box, we wouldn't call that function a "struct Box pointer function" any more than we (professional programmers) call main an "int function."

However, what WaltP means by what he says is valid, please ensure that your main returns an int. Since you are returning 0 in your code, you may as well declare main to return it...

I think that the following changes may be what you're looking to achieve in your code:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int num [5][5];
    int i = 0;
    int j = 0;
    int sum;

    for( i = 0; i < 5; i++ )
    {
        for( j = 0; j < 5; j++ )
        {
            printf( "Enter Element=> %d %d ",i, j );
            scanf( "%d", &num[i][j] );
        }
    }
    for( i = 0; i < 5; i++ )
    {
        sum = 0;
        for( j = 0; j < 5; j++ )
        {
            sum += num[i][j];
            printf( "element %d,%d => %d\n", i, j, num[i][j] );
        }
        printf( "sum of column %d = %d\n", i, sum );
    }

    return 0;
}

Output:

Code:
Enter Element=> 0 0 1 Enter Element=> 0 1 2 Enter Element=> 0 2 3 Enter Element=> 0 3 4 Enter Element=> 0 4 5 Enter Element=> 1 0 1 Enter Element=> 1 1 1 Enter Element=> 1 2 1 Enter Element=> 1 3 1 Enter Element=> 1 4 1 Enter Element=> 2 0 2 Enter Element=> 2 1 2 Enter Element=> 2 2 2 Enter Element=> 2 3 2 Enter Element=> 2 4 2 Enter Element=> 3 0 3 Enter Element=> 3 1 3 Enter Element=> 3 2 3 Enter Element=> 3 3 3 Enter Element=> 3 4 3 Enter Element=> 4 0 4 Enter Element=> 4 1 4 Enter Element=> 4 2 4 Enter Element=> 4 3 4 Enter Element=> 4 4 4 element 0,0 => 1 element 0,1 => 2 element 0,2 => 3 element 0,3 => 4 element 0,4 => 5 sum of column 0 = 15 element 1,0 => 1 element 1,1 => 1 element 1,2 => 1 element 1,3 => 1 element 1,4 => 1 sum of column 1 = 5 element 2,0 => 2 element 2,1 => 2 element 2,2 => 2 element 2,3 => 2 element 2,4 => 2 sum of column 2 = 10 element 3,0 => 3 element 3,1 => 3 element 3,2 => 3 element 3,3 => 3 element 3,4 => 3 sum of column 3 = 15 element 4,0 => 4 element 4,1 => 4 element 4,2 => 4 element 4,3 => 4 element 4,4 => 4 sum of column 4 = 20

I suppose that you have/had some intended use for the "counter" variable, but I removed it since it was unused in your code. I recommend that you research your compiler/tools so that you learn how to enable the highest degree of warnings so that it warns you of everything possible so that you are able to fix little problems as early-on as possible.


:davis:
  #4  
Old 12-Jan-2007, 09:23
NONAME123123123 NONAME123123123 is offline
New Member
 
Join Date: Jan 2007
Posts: 2
NONAME123123123 is on a distinguished road

Re: 5x5 Array Help?


thanks so much you guys. it was a big help. now im just working on getting it to print out the rows. so i put this line in. and it workes part of the time but it doesnt others?

rows = num[i][j] + num[i][j] + num[i][j] + num[i][j] + num[i][j];
  #5  
Old 12-Jan-2007, 10:49
davis
 
Posts: n/a

Re: 5x5 Array Help?


Quote:
Originally Posted by NONAME123123123
thanks so much you guys. it was a big help. now im just working on getting it to print out the rows. so i put this line in. and it workes part of the time but it doesnt others?

CPP / C++ / C Code:
rows = num[i][j] + num[i][j] + num[i][j] + num[i][j] + num[i][j];

...at least try to put your code in the appropriate [c]...[/c] tags!

Your above code is simply adding the same number to "rows" 5 times. I don't think that is what you want to do...

CPP / C++ / C Code:
#include <stdio.h>

int sum_rows( int matrix[5][5], const int row )
{
    int i;
    int result = 0;
    if( row >= 0 && row < 5 )
    {
	for( i = 0; i < 5; i++ )
	{
	    result += matrix[row][i];
	}
    }
    return result;
}

int sum_cols( int matrix[5][5], const int col )
{
    int i;
    int result = 0;
    if( col >= 0 && col < 5 )
    {
        for( i = 0; i < 5; i++ )
        {
            result += matrix[i][col];
        }
    }
    return result;
}

void print_matrix( int matrix[5][5] )
{
    int i;
    int j;
    printf( "\t0\t1\t2\t3\t4\n" );
    printf( "-------------------------------------------\n" );
    for( i = 0; i < 5; i++ )
    {
	printf( "%d |\t", i );
	for( j = 0; j < 5; j++ )
	{
	    printf( "%d\t", matrix[i][j] );
	}
	printf( "\n" );
    }
}

int main()
{
    int num[5][5];
    int i = 0;
    int j = 0;

    for( i = 0; i < 5; i++ )
    {
        for( j = 0; j < 5; j++ )
        {
            printf( "Enter Element=> %d %d ",i, j );
            scanf( "%d", &num[i][j] );
        }
    }

    print_matrix( num );
    for( i = 0; i < 5; i++ )
    {
	printf( "Sum of column[%d] = %d\n", i, sum_cols( num, i ) );
    }
    
    for( i = 0; i < 5; i++ )
    {
	printf( "Sum of row[%d] = %d\n", i, sum_rows( num, i ) );
    }

    return 0;
}

Output:

Code:
Enter Element=> 0 0 1 Enter Element=> 0 1 2 Enter Element=> 0 2 3 Enter Element=> 0 3 4 Enter Element=> 0 4 5 Enter Element=> 1 0 1 Enter Element=> 1 1 1 Enter Element=> 1 2 1 Enter Element=> 1 3 1 Enter Element=> 1 4 1 Enter Element=> 2 0 2 Enter Element=> 2 1 2 Enter Element=> 2 2 2 Enter Element=> 2 3 2 Enter Element=> 2 4 2 Enter Element=> 3 0 3 Enter Element=> 3 1 3 Enter Element=> 3 2 3 Enter Element=> 3 3 3 Enter Element=> 3 4 3 Enter Element=> 4 0 4 Enter Element=> 4 1 4 Enter Element=> 4 2 4 Enter Element=> 4 3 4 Enter Element=> 4 4 4 0 1 2 3 4 ------------------------------------------- 0 | 1 2 3 4 5 1 | 1 1 1 1 1 2 | 2 2 2 2 2 3 | 3 3 3 3 3 4 | 4 4 4 4 4 Sum of column[0] = 11 Sum of column[1] = 12 Sum of column[2] = 13 Sum of column[3] = 14 Sum of column[4] = 15 Sum of row[0] = 15 Sum of row[1] = 5 Sum of row[2] = 10 Sum of row[3] = 15 Sum of row[4] = 20

If you have difficulty visualizing what is happening, use graph paper or just draw a matrix representative of what it is that you expect to see. Alternatively, use a spreadsheet program to make your 5x5 matrix. Lable the headings appropriately so that you have zero-based indexes for rows and columns such that your coordinates match the "cell" for the value you expect to be located in the cell by your code. Then, as you establish your looping requirements, you can see that in order to gain access to the row data that you must sum the values for a given row without changing the row index but by changing the column index through the extent of your matrix.

When calculating the columnar values, you will need to need to loop through each row without changing the index for the column, just the row through the extent of the matrix.


:davis:
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
1-D array jack999 C Programming Language 16 16-May-2006 12:38
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
Help with an array using pointers glulu76 C++ Forum 1 01-May-2005 11:21
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.