Advertisement
Razali

Diagonal Matrices

Nov 13th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. 3a)
  2.  
  3. /*
  4.  * <summary> This function determines if a square matrix  is descending diagonal </summary>
  5.  * <params>
  6.         "matrix" = The array containing the int elements
  7.         "size" = Number of elements both row and column-wise
  8.    </params>
  9.  * <return> "1" : Is desc diagonal,   "0": Otherwise  </return>
  10.  * <precond> "size" > 0 </precond>
  11.  */
  12. int isDescDiagonal(int matrix[][MAX_SIZE], int size)
  13. {
  14.     int row, col;
  15.    
  16.     for(row = 0; row < size; row++)
  17.     {
  18.         //Ignore row-0 col-0, it can be any value
  19.         for(col = 1; col < size; col++)
  20.         {
  21.             //Diagonal
  22.             if(row == col)
  23.             {
  24.                 /* Not concurrently descending */
  25.                 if(matrix[row-1][col-1] - 1 != matrix[row][col])
  26.                     return 0;
  27.             }
  28.             /* If non-diagonal elements are not zero */
  29.             else if(matrix[row][col] != 0)
  30.                 return 0;
  31.         }
  32.     }
  33.    
  34.     return 1;
  35. }
  36.  
  37. 3b)
  38.  
  39. /*
  40.  * <summary> This function determines if a square matrix  is anti-diagonal </summary>
  41.  * <params>
  42.         "matrix" = The array containing the int elements
  43.         "size" = Number of elements both row and column-wise
  44.    </params>
  45.  * <return> "1" : Is anti diagonal,   "0": Otherwise  </return>
  46.  * <precond> "size" > 0 </precond>
  47.  */
  48. int isAntiDiagonal(int matrix[][MAX_SIZE], int size)
  49. {
  50.     int row, col;
  51.    
  52.     for(row = 0; row < size; row++)
  53.     {
  54.         for(col = 0; col < size; col++)
  55.         {
  56.             //Diagonal
  57.             if(row+col != size - 1)
  58.             {
  59.             /* If non-diagonal elements are not zero */
  60.                 if(matrix[row][col] !== 0)
  61.                     return 0;
  62.             }
  63.         }
  64.     }
  65.    
  66.     return 1;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement