0
votes

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");
     }
     
2
Hi Michael and welcome to StackOverflow. Please take the tour, read How to Ask and provide a minimal reproducible example.Yunnosch
Is M really uninitialised when you use it? MRE please.Yunnosch
Please apply indentation, it makes structure analysis so much easier.Yunnosch
Perhaps you need to encapsulate the code into seperate functions to make it easier to read and debug. Something like setMatrix() with the nested for loops and the read from cin, and checkMatrix() to see whether diagonals are equal, maybe even a getDiagonal() and getAntiDiagonal().jackw11111
I am not allowed to use any special functions or librariesMichael

2 Answers

1
votes

As already pointed out by @Bob__, there are a couple of issues with the code:

  1. first, in C, array indices start from 0.

  2. Furthermore, in your inner loop, over j, you only read a single entry but then you try to compare the diagonal entry with the anti-diagonal entry which you haven't read in yet. You probably want to read in the entire matrix first and then, in a separate loop, check whether the diagonal and anti-diagonal are the same.

for (i=0; i < M; i++){
    for (j = 0; j< M; j++){
      scanf("%d", &m[i][j]);
    }
}
  1. Given that array indices in C start at 0, there is a missing pair of parantheses in the matrix index calculation: the index for the anti-diagonal entry should be m[ M - (i+1) ][j]. Once you have read in the entire matrix, something like this ought to work:
for(i = 0; i < M; i++){
    if( m[i][i] != m[M - (i+1)][i] ){
        b = 0;
    }
}
0
votes

Let's consider the main issues in the posted code.

  • if (M<0 || M>100) printf("Wrong input!\n"); I'd argue that a 0-dimensioned matrix wouldn't be acceptable, but your milage may vary.

  • The values of the matrix are "scanned" from the standard stream and checked at the same time. m is uninitialized, so that its values are indetermined. While setting the elements of, say, the first row, the code is trying to compare those with the last row, which is yet to be entered. It's necessary to performe the checks after all the elements are inputted.

  • In C, array indices start from 0, while in this snippet the loop are like for (i=1; i<=M; i++), skipping the first element and accessing one past the end (which has undefined behavior). The anti-diagonal should be defined by i + j == M - 1.