# Thread: Multi-Array Problem: Calculate Sum of Rows and Columns (Beginner)

1. ## Multi-Array Problem: Calculate Sum of Rows and Columns (Beginner)

Hello,

First time poster and learning about arrays.

The problem asks to build a 5x5 matrix and then to calculate the sum of the rows and columns.

The code (pasted below) works well for single digits, but when I input the number '10,' the program goes haywire. (Note: an input of 100, 20, 30, etc works....)

Any reason why my arrays aren't reading the number '10'? Appreciate your comments!

Thanks.

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

#define N 5

int main(){

int mat[N][N] = {0};
int ch, col, row, i, sum = 0;

for (row = 0; row < N; row++){
printf("\nEnter row %d: ", row + 1);
for (col = 0; col < N; col++){
scanf(" %d", &ch);
if (ch != '\n'){
mat[row][col] = ch;
}
else
break;
}

}

printf("\n\n");
printf("Row totals:");

for (row = 0; row < N; row++)
for (col = 0; col < N; col++){

i = mat[row][col];
sum += i;
if (col == 4){
printf(" %d", sum);
sum = 0;
break;
}

}

printf("\n\n");
printf("Column totals:");

for (col = 0; col < N; col++)
for (row = 0; row < N; row++){

i = mat[row][col];
sum += i;
if (row == 4){
printf(" %d", sum);
sum = 0;
break;
}

}

printf("\n\n");

return 0;``` 2. You are breaking the loop when the input value == '\n'. The numerical value of '\n' is (commonly) 10. I would just get rid of that since you don't really want to break the loop early anyway.
Code:
```#include <stdio.h>

#define N 5

int main() {
int mat[N][N] = {{0}};

for (int row = 0; row < N; row++) {
printf("\nEnter row %d: ", row + 1);
for (int col = 0; col < N; col++)
scanf("%d", &mat[row][col]);
}

printf("\n\nRow totals:");
for (int row = 0; row < N; row++) {
int sum = 0;
for (int col = 0; col < N; col++)
sum += mat[row][col];
printf(" %d", sum);
}

printf("\n\nColumn totals:");
for (int col = 0; col < N; col++) {
int sum = 0;
for (int row = 0; row < N; row++)
sum += mat[row][col];
printf(" %d", sum);
}
printf("\n\n");

return 0;
}``` 3. Thanks! Also, your code is obviously much cleaner, and I learned a couple of things from it.

Just out of curiosity: would you prefer to use scanf over getchar when dealing with numbers only? For example, would the use of getchar to register numerical inputs be recommended in this program? Originally Posted by john.c You are breaking the loop when the input value == '\n'. The numerical value of '\n' is (commonly) 10. I would just get rid of that since you don't really want to break the loop early anyway.
Code:
```#include <stdio.h>

#define N 5

int main() {
int mat[N][N] = {{0}};

for (int row = 0; row < N; row++) {
printf("\nEnter row %d: ", row + 1);
for (int col = 0; col < N; col++)
scanf("%d", &mat[row][col]);
}

printf("\n\nRow totals:");
for (int row = 0; row < N; row++) {
int sum = 0;
for (int col = 0; col < N; col++)
sum += mat[row][col];
printf(" %d", sum);
}

printf("\n\nColumn totals:");
for (int col = 0; col < N; col++) {
int sum = 0;
for (int row = 0; row < N; row++)
sum += mat[row][col];
printf(" %d", sum);
}
printf("\n\n");

return 0;
}``` 4. Originally Posted by SanFi would you prefer to use scanf over getchar when dealing with numbers only? For example, would the use of getchar to register numerical inputs be recommended in this program?
scanf is more appropriate than getchar for numerical input. getchar reads a character at a time, so you would need to build up a multi-digit number one character at a time, something like:
Code:
```#include <stdio.h>
#include <ctype.h>  // isdigit and friends

int main()
{
int n = 0;  // the integer we are building up

printf("Enter a integer: ");

// getchar returns a character code as an int
for (int c; isdigit(c = getchar()); )
{
// Multiply n by 10 to shift its decimal digits over.
// Subtract the character code for '0' from c
// to get it's numerical digit value, and add it to n.

n = n * 10 + (c - '0');
}

printf("\nn: %d\n", n);

return 0;
}```
Note that in the above program if you enter an integer larger than MAX_INT (for 32-bits it's 2147483647) then n will overflow and give the wrong result. This is generally the case for scanf, too.

Probably the best option for reading an integer is to read a line of input as a string and then parse it for an the integer with strtol. This allows better error detection (particularly out-of-range errors which scanf doesn't generally detect at all). It's a little involved to check all the possible errors, though. Popular pages Recent additions #### Tags for this Thread

array, beginner question, col, row, sum 