I’m trying to make a function in C that calculates the sum of the elements on the right side of a 2 dimensional square matrix. (only the 1 elements in the below comments)
So far I’ve got this but it’s incorrect since it calculates the sum of the elements for the whole matrix:
#define N 5
int a[N][N] ={{0,0,0,0,1},
{0,0,0,1,1},
{0,0,1,1,1},
{0,1,1,1,1},
{1,1,1,1,1}};
/*
{0,0,0,0,1},
{0,0,0,1,1},
{0,0,1,1,1},
{0,1,1,1,1},
{1,1,1,1,1},
sum =
a[0][4] +
a[1][3] + a[1][4] +
a[2][2] + a[2][3] + a[2][4] +
a[3][1] + a[3][2] + a[3][3] + a[3][4] +
a[4][0] + a[4][1] + a[4][2] + a[4][3] + a[4][4]
*/
int sumSndBisRight(int a[N][N]) {
int i, j, sum = 0, k = N - 1;
for( i = 0;i < N;i++)
for( j = (N - 1);j >= 0;j--)
sum += a[i][j];
return sum;
}
void main() {
int sum;
sum = sumSndBisRight(a);
printf("%d", sum);
}
Thanks in advance for your help.
Change
to