C Program to interchange two rows and columns of a matrix

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to interchange columns and rows of a matrix?

We want to interchange the Rows and Columns of a given Matrix; as we discussed, the Matrix is a set of elements in a Row and Column format. So what we are doing is that we need to interchange the Row and Column. For example, a Matrix [123] and Column interchange 2 and 3 means the resulting Matrix will be [132] like that in Row also.

In this C program, after accepting the user Matrix, we are getting the Columns and Rows that need to interchange. First, we interchange the Row; after the Row interchange, we interchange the Columns.

The logic of this C program is, first declare two Matrix 'm1' and 'm2'.Then read the Order of the Matrix into 'm' and 'n'. Then read the Matrix from the user and save it into 'm1 and m2. Read the number of Rows to be exchanged into the variable 'a' and 'b'. Then interchange the Rows using for loop and keep it in the Matrix m1. Read the number of columns to be exchanged into the variable 'p' and 'q'. Display the given Matrix as the elements in m2. Then interchange the Columns in 'm2' by using for loop and save it in m2. Finally, display the Matrix after Row interchange as m1 and the Matrix after Column interchange as m2.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the variables m1[][],m2[][] as integer type.

STEP 3: Declare the integer variables i,j,m,n,a,b,c,p,q,r.

STEP 4: Read the Order of the Matrix into m and n.

STEP 5: set i=0.

STEP 6: By using the for loop with the condition i

STEP 7: set j=0.

STEP 8: By using the for loop with the condition j

STEP 9: Read the number into m1[i][j].

STEP 10: m2[i][j] = m1[i][j].

STEP 11: Increment 'j' by one do step 8.

STEP 12: Increment 'i' by one do step 6.

STEP 13: Read the number of Rows to be exchanged into the variable a and b.

STEP 14: Set i=0.

STEP 15: By using for loop with the condition i

STEP 16: Assign c = m1[a-1][i],m1[a-1][i] = m1[b-1][i],m1[b-1][i] = c.

STEP 17: Increment 'i' by one, do step 16.

STEP 18: Read the number of columns to be exchanged into the variables p and q.

STEP 19: By using for loop display the given Matrix.

STEP 20: By using another for loop with the condition i

STEP 21: Assign r = m2[i][p-1],m2[i][p-1] = m2[i][q-1],m2[i][q-1] = r;

STEP 22: Display the Matrix after interchanging the two Rows as each element of m1 using for loop.

STEP 23: Display the Matrix after interchanging the two Columns as each element of m2 using for loop.

C Source Code

                                          #include <stdio>

void main() {
  static int m1[10][10], m2[10][10];
  int i, j, m, n, a, b, c, p, q, r;
  printf("Enter the order of the matrix\n");
  scanf("%d %d", & m, & n);
  printf("Enter the coefficients of the matrix\n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      scanf("%d,", & m1;
        [i][j]);
      m2[i][j] = m1[i][j];
    }
  }
  printf("Enter the numbers of two rows to be exchanged \n");
  scanf("%d %d", & a, & b);
  for (i = 0; i < m; ++i) {
    c = m1[a - 1][i]; /* first row has index is 0 */
    m1[a - 1][i] = m1[b - 1][i];
    m1[b - 1][i] = c;
  }
  printf("Enter the numbers of two columns to be exchanged\n");
  scanf("%d %d", & p, & q);
  printf("The given matrix is \n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j)
      printf(" %d", m2[i][j]);
    printf("\n");
  }
  for (i = 0; i < n; ++i) {
    r = m2[i][p - 1]; /* first column index is 0 */
    m2[i][p - 1] = m2[i][q - 1];
    m2[i][q - 1] = r;
  }
  printf("The matrix after interchanging the two rows(in the original matrix)\n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      printf(" %d", m1[i][j]);
    }
    printf("\n");
  }
  printf("The matrix after interchanging the two columns(in the original matrix)\n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j)
      printf(" %d", m2[i][j]);
    printf("\n");
  }
}
                                      

OUTPUT

Enter the order of the matrix
3 *3

Enter the co-efficient of the matrix
1 2 4
5 7 9
3 0 6

Enter the numbers of two rows to be exchanged
1 2

Enter the numbers of two columns to be exchanged
2 3

The given matrix is
 1 2 4
 5 7 9
 3 0 6

The matrix after interchanging the two rows (in the original matrix)
 5 7 9
 1 2 4
 3 0 6

The matrix after interchanging the two columns(in the original matrix)
 1 4 2
 5 9 7
 3 6 0