Advertisement
Guest User

Untitled

a guest
Jul 7th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. // Identity matrix check
  2. //
  3. // requires N and m[N][N] matrix
  4.  
  5. #include <stdio.h>
  6.  
  7. #include "matrix.h" // defines N and m[N][N]
  8.  
  9. void showMatrix (int m[N][N], int n);
  10. int isIdent (int m[N][N], int n);
  11.  
  12. int main (void)
  13. {
  14. printf ("The matrix\n");
  15. showMatrix (m, N);
  16. if (isIdent (m, N))
  17. printf ("is an identity matrix\n");
  18. else
  19. printf ("is not an identity matrix\n");
  20. return 0;
  21. }
  22.  
  23. // display a n x n matrix row-by-row
  24. void showMatrix (int m[N][N], int n)
  25. {
  26. for (int row = 0; row < n; row++) {
  27. for (int col = 0; col < n; col++) {
  28. putchar (' ');
  29. printf ("%d", m[row][col]);
  30. }
  31. printf ("\n");
  32. }
  33. }
  34.  
  35. int isIdent (int m[N][N], int n)
  36. {
  37. for (int row = 0; row < n; row++)
  38. for (int col = 0; col < n; col++)
  39. if (row == col && m[row][col] != 1)
  40. return 0;
  41. else if (row != col && m[row][col] != 0)
  42. return 0;
  43. return 1;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement