So, I am trying to make a program that computes and outputs the sum of each diagonal in a [10][10] array. This is what I have so far. Yes I know it only adds up the thing row by row then column by column. That's my problem, I have no idea how to find the sum of the diagonals!
Code:
#include <stdio.h>
#include <stdlib.h>
void printArray( const int a[][10]);
int main (void)
{
int a;
int i;
int x;
int total;
int num;
int array[10][10];
for (i = 0; i < 10; i++)
{ for ( x = 0; x < 10; x++)
{ num = 1 + rand() % 10;
array[i][x] = num;
}
}
line 22-> printArray ( array );
for ( a = 0; a < 10; a++)
{ for (i = a; i < 10; i++)
{ for ( x = 0; x < 10; x++)
{ total += array[i][x];
printf("%4d\n", total);
}
}
}
for ( a = 1; a < 10; a++)
{ for (i = 0; i < 10; i++)
{ for ( x = a; x < 10; x++)
{ total += array[i][x];
printf("%4d\n", total);
}
}
}
return 0;
}
void printArray( const int a[][10])
{
int i;
int x;
for (i = 0; i < 10; i++)
{ for ( x = 0; x < 10; x++)
{ printf("%d", a[i][x]);
}
printf("\n");
}
}