Hello! My task is to check if the elements of main and anti diagonal of quadratic matrix are same. If this condition is satisfied 'Yes' should be printed; 'No' in the opposite case. For example, the condition is satisfied for matrix 3x3 with all rows equal to: 1 2 3. My code worked for all the matrices except the one with negative numbers and the one with dimension 5x5 with 4 rows: 1 2 3 4 5 and 5th row: 1 2 3 4 6, and I don't know why. I am beginner. I hope you could help.
int i,j,M,b=0,m[100][100];
do{
printf("Dimension of matrix: ");
scanf("%d", &M);
if (M<0 || M>100)
printf("Wrong input!\n");
}while (M<0 || M>100);
printf("Enter the elements: ");
for (i=0;i<M;i++){
for (j=0;j<M;j++){
scanf("%d", &m[i][j]);
}
}
for (i=0;i<M;i++){
for (j=0;j<M;j++){
if(m[i][j]!=m[M-(i+1)][j])
b=0;
else if ((i==j || i+j==M-1) && (m[i][j]==m[M-(i+1)][j]))
b=1;
else b=0;
}
}
if(b==1)
printf("YES");
else printf("NO");
}
M
really uninitialised when you use it? MRE please. – YunnoschsetMatrix()
with the nested for loops and the read from cin, andcheckMatrix()
to see whether diagonals are equal, maybe even agetDiagonal()
andgetAntiDiagonal()
. – jackw11111