# Thread: Need help working with an array

1. I wanted it to print the whole matrix, though, not just a single element. I tried doing printf("&#37;d", mat[row][col]), but that didn't work...it gave me that big number I was telling you about earlier. Is this wrong? If so, what would be the correct way to print the whole matrix?

2. Wait -- did you print out all 25 numbers without any spaces between them?

3. No, I'd enter them with spaces between them, like 1 2 3 4 5 etc.

4. So? Show the actual code that you used to print them out.

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

#define DIM 5

int main()
{
int mat[DIM][DIM] = {0}, row, col, rowSum, colSum;

printf("Enter 25 numbers: ");
for (row = 0; row < DIM; row++)
for (col = 0; col < DIM; col++)
scanf("&#37;d", &mat[row][col]);

printf("\n%d\n", mat[row][col]);

rowSum += mat[row];
colSum += mat[col];

printf("The sum of the rows is: %d", rowSum);
printf("\n");
printf("The sum of the colums is: %d", colSum);

return 0;
}```
Here's what happens when I run the program:

Enter 25 numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25

2621304
The sum of the rows is: -594119456
The sum of the colums is: 13106020
Process returned 0 (0x0) execution time : 14.185 s
Press any key to continue.

2621304 is supposed to be the matrix. Ignore the sums; I know they're wrong.

6. At the end of the for loop, both row and col have been incremented past legal subscripts. You have to put the print statement inside a double-for loop, because it's not as though going through an array and printing it differs from going through an array and reading it in in any way whatsoever.

7. Like I explained a week ago, you have a pair of for loops to read the data in.

Now use a pair of for loops to scan all the elements of the array. Then figure out at which point where
sumOfSomething[row] += mat[row][col];
would be a good place.

8. Take a look at this, it may help you out a bit

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

#define N 5

int main(void)
{
int i,j,k=1,row_tot,col_tot;
int a[N][N];
//initialising the matrix

for(i=0;i<N;i++)
for(j=0;j<N;j++)
a[i][j]=k++;
//printing matrix and calculating row totals
for(i=0;i<N;i++)
{
row_tot=0;
for(j=0;j<N;j++)
{
printf("&#37;3d ",a[i][j]);
row_tot+=a[i][j];
}
printf("|| %d ",row_tot);
printf("\n\n");
}
printf("==========================\n");
for(i=0;i<N;i++)
{
col_tot=0;
for(j=0;j<N;j++)
col_tot+=a[j][i];
printf("%3d ",col_tot);
}
printf("\n\n");
system("PAUSE");
return 0;
}```

9. s/bit/totally
You could have at least left a bit of mystery for the OP to work on.

10. Okay, a put a printf in the nested for loop, but instead of printing the 5x5 matrix, it just prints all 25 numbers right in a row. How do I fix this?

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

#define DIM 5

int main()
{
int mat[DIM][DIM] = {0}, row, col, rowSum, colSum;

printf("Enter 25 numbers: ");
for (row = 0; row < DIM; ++row)
{
for (col = 0; col < DIM; ++col)
{
scanf("&#37;d", &mat[row][col]);
printf("%d ", mat[row][col]);
}
}

rowSum += mat[row][col];
colSum += mat[row][col];

printf("The sum of the rows is: %d", rowSum);
printf("\n");
printf("The sum of the colums is: %d", colSum);

return 0;
}```

11. If you want a new line, you have to print it.

Popular pages Recent additions