Hello,
I have recently been introduced to arrays, but I cannot seem to get this program to calculate the sum of all elements correctly:
Code:
#include <stdio.h>
int main()
{
int i, m, col=0, row=0;
int matrix[5][5]={{4, 6, 25, 88, 5},
{34, 5, 300, 4, 65},
{78, 43, 11, 90, 125},
{98, 585, 12, 63, 21},
{45, 35, 9, 5, 1}};
printf("In the array:");
printf("\n");
for(m = 0 ; m < 5 ; m++)
{
for(i = 0 ; i < 5 ; i++)
{ printf("%d ", matrix[m][i]);
col+= matrix[m][i];
}
row = col + matrix[m][i];
printf("\n");
}
printf("\n\n");
printf("the sum of the elements is %d", row);
return 0;
}
The output should look like this:
In the array:
4 6 25 88 5
34 5 300 4 65
78 43 11 90 125
98 585 12 63 21
45 35 9 5 1
the sum of the elements is 1757
Im getting 1762 as the sum