C Program to Interchange any two Rows & Columns in the given Matrix

«
»
This C Program interchanges any two rows & columns in the given matrix. The program swaps the values of any 2 rows and columns.

Here is source code of the C program to interchanges any two rows & columns in the given matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to accept a matrix of given order and interchange
  3.  * any two rows and columns in the original matrix
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     static int array1[10][10], array2[10][10];
  10.     int i, j, m, n, a, b, c, p, q, r;
  11.  
  12.     printf("Enter the order of the matrix \n");
  13.     scanf("%d %d", &m, &n);
  14.     printf("Enter the co-efficents of the matrix \n");
  15.     for (i = 0; i < m; ++i)
  16.     {
  17.         for (j = 0; j < n; ++j)
  18.         {
  19.             scanf("%d,", &array1[i][j]);
  20.             array2[i][j] = array1[i][j];
  21.         }
  22.     }
  23.     printf("Enter the numbers of two rows to be exchanged \n");
  24.     scanf("%d %d", &a, &b);
  25.     for (i = 0; i < m; ++i)
  26.     {
  27.         /*  first row has index is 0 */
  28.         c = array1[a - 1][i];
  29.         array1[a - 1][i] = array1[b - 1][i];
  30.         array1[b - 1][i] = c;
  31.     }
  32.     printf("Enter the numbers of two columns to be exchanged \n");
  33.     scanf("%d %d", &p, &q);
  34.     printf("The given matrix is \n");
  35.     for (i = 0; i < m; ++i)
  36.     {
  37.         for (j = 0; j < n; ++j)
  38.             printf(" %d", array2[i][j]);
  39.         printf("\n");
  40.     }
  41.     for (i = 0; i < n; ++i)
  42.     {
  43.         /*  first column index is 0 */
  44.         r = array2[i][p - 1];
  45.         array2[i][p - 1] = array2[i][q - 1];
  46.         array2[i][q - 1] = r;
  47.      }
  48.     printf("The matix after interchanging the two rows(in the original matrix) \n");
  49.     for (i = 0; i < m; ++i)
  50.     {
  51.         for (j = 0; j < n; ++j)
  52.         {
  53.             printf(" %d", array1[i][j]);
  54.         }
  55.         printf("\n");
  56.      }
  57.     printf("The matix after interchanging the two columns(in the original matrix) \n");
  58.     for (i = 0; i < m; ++i)
  59.     {
  60.         for (j = 0; j < n; ++j)
  61.             printf(" %d", array2[i][j]);
  62.         printf("\n");
  63.     }
  64. }

advertisement
$ cc pgm59.c
$ a.out
Enter the order of the matrix
2 2
Enter the co-efficents of the matrix
34 70
45 90
Enter the numbers of two rows to be exchanged
1 2
Enter the numbers of two columns to be exchanged
1 2
The given matrix is
 34 70
 45 90
The matix after interchanging the two rows(in the original matrix)
 45 90
 34 70
The matix after interchanging the two columns(in the original matrix)
 70 34
 90 45

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Matrix, go to C Programming Examples on Matrix. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.