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 be interchanged. First, we interchange the Row; after the Row interchange, we interchange the Columns.

pythagorean triples in a range

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 variables '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 saving them 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 matrix 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: Use a nested for loop with conditions i and j for reading elements of matrix m1, also assign the same value to matrix m2

STEP 6: Read the number of rows to be exchanged into the variables a and b.

STEP 7: By using for loop with the condition i, do step8

STEP 8: Swap the elements of rows as follows

  1. c = m1[a-1][i]
  2. m1[a-1][i] = m1[b-1][i]
  3. m1[b-1][i] = c.

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

STEP 10: By using for loop display the given Matrix stored in m2, because m1 has the row interchanged matrix.

STEP 11: Use a for loop with the condition i, do step12

STEP 12: Swap the elements of columns as follows

  1. r = m2[i][p-1]
  2. m2[i][p-1] = m2[i][q-1]
  3. m2[i][q-1] = r.

STEP 13: Display the Matrix m1 after interchanging the two Rows.

STEP 14: Display the Matrix m2 after interchanging the two Columns.

C Source Code

                                          #include <stdio.h>

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