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;