The whole numbers positioned in a square NxN such that all lines and main diagonal have the same sum.
For example, the square below
2 7 11 9 5 6 4 3 13
is a mysterious square of sum 20, since all lines (2 + 7 + 11 = 20, 9 + 5 + 6 = 20 and 4 + 3 + 13 = 20) 20) have the same sum (20).
Write a program that, given a square, determines whether it is mysterious or not and what its sum (if it is magical).
Entry
The first line contains an integer N. The next N lines contain N integers each, separated by exactly one whitespace.
Output
Your program should print a single line with an integer representing the sum of the magic square or -1 if the square is not magic.
PS: I can not use vector, I can only use repetition structures.
EXAMPLE 1
ENTRY:
3 2 7 11 9 5 6 4 3 13
OUTPUT:
20
PS: My output was:
-1
Here is my code:
int main()
{
int n, i, j, elem;
int , soma_linhas = 0, soma_dp = 0;
scanf("%d", &n);
//leitura dos elementos
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
scanf("%d", &elem);
//somatorio de cada linha
aux += i;
soma_linhas = aux;
}
}
//somatorio da diagonal principal
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(i == j){
aux += i + j;
soma_dp = aux;
}
}
}
if(soma_linhas == soma_dp)
printf("%d", soma_linhas);//se for quadrado misterioso tanto faz mostra a linhas ou da diagonal
else
printf("-1");
return 0;
}
Where am I going wrong?