This blog is under construction

Sunday, 28 July 2013

C program to find the rank of a matrix

Write a C program to find the rank of a matrix(order 2).


  #include <stdio.h>
  #define ORDER 2

  int main() {
        int i, j, n = ORDER, det, matrix[2][2];

        /* get the input matrix from the user */
        printf("Enter your entries for the input matrix:\n");
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        scanf("%d", &matrix[i][j]);
                }
        }

        /* find the determinant of the given matrix */
        det = (matrix[0][0] * matrix[1][1]) -
                (matrix[0][1] * matrix[1][0]);

        /*
         * If the determinant value is 0, then
         * rank is 1.  otherwise, rank is 2.
         */
        if (det) {
                printf("Rank of the given matrix is 2!!\n");
        } else {
                printf("Rank of the given matrix is 1!!\n");
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your entries for the input matrix:
  10 20
  30 40
  Rank of the given matrix is 2!!

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your entries for the input matrix:
  10 20
  10 20
  Rank of the given matrix is 1!!


No comments:

Post a Comment