# Thread: Computing the sum of each diagonal in an array

1. ## Computing the sum of each diagonal in an array

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");
}
}```

2. well, isn't exactly an error, is a warning so the compilation must have finish ok.

Anyway, didn't look at the code, just pointing a detail about your error message.

3. Yeah, it compiles and runs, but my results are just slightly off. I don't know if that is due to poor arithmetic, or if it has to do with the error message.

4. Oh wow, I just realized my code doesn't even do near that it is supposed to.