C program to find rows and columns in a given matrix

We can find rows and columns of a given matrix in c.

#include<stdio.h>

int m[][4] = {
             {1, 2, 2, 3},
             {2, 3, 2, 8},
             {2, 3, 2, 8},
             {4, 2, 3, 5}
             };

int n[][5] = {
              {1, 2, 2, 3, 3},
              {2, 3, 2, 8, 3},
              {2, 3, 2, 8, 4},
              {2, 3, 2, 8, 5},
              {2, 3, 2, 8, 5},
              {4, 2, 3, 5, 6}
              };
int main ()
{

      int row = sizeof (m) / sizeof (m[0]);
      int col = sizeof (m[0]) / sizeof (m[0][0]);

      int nrow = sizeof (n) / sizeof (n[0]);
      int ncol = sizeof (n[0]) / sizeof (n[0][0]);

printf ("%d %d \n", row, col);
printf ("%d %d\n", nrow, ncol);
return 0;
}

Output:

4 4
6 5

Note:

int n[][5] = {
              {1, 2, 2, 3, 3},
              {2, 3, 2, 8, 3},
              {2, 3, 2, 8, 4},
              {2, 3, 2, 8, 5},
              {2, 3, 2, 8, 5},
              {4, 2, 3, 5, 6}
              };

Here the matrix n is containing integers. n[0] points to first array of above matrix. sizeof (n) / sizeof (n[0]) will give rows while sizeof (n[0]) / sizeof (n[0][0]) will give columns.



Related Contents to follow