# Thread: sum rows & columns in array

1. ## sum rows & columns in array

Howdy,

I need to take as input bi-dimensional array and summarize its columns (each column separately) & rows (each row separately).

This is what I wrote so far:

Code:
```

#include<stdio.h>

#define ROW 2

#define COL 2

void sumrz(int a[][COL],int *sum_row, int *sum_col);

void main ()

{

int a[ROW][COL],i,j,*sum_row,*sum_col;

for (i=0;i<ROW ;i++)

for (j=0;j<COL;j++)

scanf ("%d", &a[i][j]);

sumrz(a,sum_col,sum_row);

printf ("array is: \n");

for (i=0;i<ROW;i++)

for(j=0;j<COL;j++)

{

printf("%d",a[i][j]);

printf ("\n");

}

}

void sumrz(int a[][COL],int *sum_row, int *sum_col)

{

int i,j;

for (i=0;i<ROW;i++)

*sum_row =0;

for(j=0;j<COL;j++)

*sum_row+=a[i][j];

printf ("row sum is %d\n", *sum_row);

for (j=0;j<COL;j++)

*sum_col=0;

for (i=0;i<ROW;i++)

*sum_col+=a[i][j];

printf ("column sum is %d\n", *sum_col);

}```
Its clear to me I misuse pointers. Will you help me right it correctly? Are ther any others false? Progarm does't give output.

Thanx!

Ronen 2. Just think about the problem:
Code:
```sum rows:
count from zero to row size -1
sum[thisrownumber] += thisrowofthearray[thiscell];```
Do the same for columns. Basicly, your problem is you need an array which is as big as the total number of rows, so you can store the sum of each row in it. (Or, don't store it, and just display it instead. Then do the same for rows.

An example:
Code:
```int array, rowtotals;

sumrows( array, rowtotals );```
I won't do more than the pesudocode here, becuase that already does basicly all the work for you, not that you didn't make an effort.

Oh, and main returns an int, and you're just using pointers without actually making them point to something:
Code:
`int a[ROW][COL],i,j,*sum_row,*sum_col;`
Quzah. 3. That's some poor indentation - so hard to see where the code flow really is.

I think you meant to do this in main
Code:
```int a[ROW][COL],i,j,sum_row,sum_col;
sumrz(a,&sum_col,&sum_row);``` 4. ## sum rows & columns in array

the problem is I'm not sure how do I treat the pointers. How do I link between them & my result of sum of the rows & columns.
Please explain it to me with the code of this program, this way I'll hopefully grab it better.

cheers!

Ronen 5. Well following sumrz(a,sum_col,sum_row);

Try
printf( "sums=%d,%d\n", sum_col, sum_row);

Everything else looks OK at first glance 6. I get these 2 warnings from compiler:
warning C4700: local variable 'sum_col' used without having been initialized
warning C4700: local variable 'sum_row' used without having been initialized

it was also before your comment.
other compiler respond with odd numbers in 'sum_col' & 'sum_rowl'

Ronen 7. Originally Posted by ronenk
I need to take as input bi-dimensional array and summarize its columns (each column separately) & rows (each row separately).

This is what I wrote so far:
• Code reindented
• misuse of pointers fixed

Code:
```
#include<stdio.h>

#define ROW 2
#define COL 2

void    sumrz (int a[][COL],
int *sum_row,
int *sum_col);

void main ()
{
int     a[ROW][COL],
i,
j,
sum_row,
sum_col;

for (i = 0; i < ROW; i++)

for (j = 0; j < COL; j++)

scanf ("%d", &a[i][j]);

sumrz (a, &sum_col, &sum_row);

printf ("array is: \n");

for (i = 0; i < ROW; i++)

for (j = 0; j < COL; j++)
{
printf ("%d", a[i][j]);
printf ("\n");
}
}

void sumrz (int a[][COL],
int *sum_row,
int *sum_col)
{
int     i,
j;

for (i = 0; i < ROW; i++)

*sum_row = 0;

for (j = 0; j < COL; j++)

*sum_row += a[i][j];

printf ("row sum is %d\n", *sum_row);

for (j = 0; j < COL; j++)

*sum_col = 0;

for (i = 0; i < ROW; i++)

*sum_col += a[i][j];

printf ("column sum is %d\n", *sum_col);
}```
In addition, you seem to have a serious problem with missing { }'s, making your algorithm to run in a strange way. I suggest you always use the curly braces whatever the number of lines in the code structure (real-life shows that this number natural trend is growth).

An extra detail. I recommend that you define the variables on a unique line for each of them. Factorization is attractive, but it makes the code hard to maintain and to modularize. It also breaks my personnal C-rule:

The scope of an object shall be reduced to minimum. 8. now i got it riht.

Thanx!  Popular pages Recent additions 