This is the code I have so far. I believe it has to do with the checkdiag2 function itself, as I watched the program run in steps and it never proceeded past the last 'if' statement. I'm not sure why. Is this an invalid condition? I tried it previously with the i that was assigned the first value of the anti diagonal.
#include <stdio.h>
#include <stdlib.h>
int checkdiag2 (int **m, int size);
int
main(void)
{
int i, j, dim, result;
int **arr;
FILE *in;
in = fopen("matrix3.txt", "r");
fscanf(in, "%d", &dim);
arr = (int**) calloc(dim, sizeof(int*));
for (i=0; i<dim; ++i)
arr[i] = (int*) calloc(dim, sizeof(int));
for (i=0; i<dim; ++i)
for (j=0; j<dim; ++j)
fscanf(in, "%d", &arr[i][j]);
result = checkdiag2(arr, dim);
if (result == 1)
printf("The matrix is %dx%d and all the numbers on the antidiagonal are the same", dim, dim);
else
printf("The matrix is %dx%d and all the numbers on the antidiagonal are NOT the same", dim, dim);
for (i=0; i<dim; ++i)
free(arr[i]);
free(arr);
return(0);
}
int checkdiag2 (int **m, int size)
{
int i, j, count=0;
i = m[0][size];
for (i=0; i<size; ++i)
for (j=0; j<size; ++j)
if ((i+j)==size)
if(m[i][j]==m[0][size])
count=count+1;
if (count==size)
return(1);
else
return(0);
}
The data in the file that I'm using is
8
8 9 4 5 6 7 8 5
8 8 4 5 6 7 5 5
8 9 8 5 6 5 5 5
8 9 4 8 5 5 8 5
8 9 4 5 8 7 8 5
8 9 5 5 6 8 8 4
8 5 4 5 6 7 8 4
5 9 4 5 6 7 8 8
And the result prints that the anti diagonal is not the same.
Array indices in C are 0-based; thus, if your matrix is NxN, the indices for any element on the anti-diagonal will sum to N-1, not N.