**Swap rows and columns of matrix **

**Swap rows and columns of matrix**

Write a C Program to Swap rows and columns of matrix . Here’s simple Program to Interchange any two Rows and Columns in Matrix in C Programming Language.

*What is Matrix ?*

**Matrix representation** is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

*Two-dimensional Arrays : :*

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

type arrayName [ x ][ y ];

Where **type** can be any valid C data type and **arrayName** will be a valid C identifier.

Below is the source code for C Program to Swap rows and columns of matrix which is successfully compiled and run on Windows System to produce desired output as shown below :

**SOURCE CODE : :**

**SOURCE CODE : :**

/* C Program to Swap rows and columns of matrix */ #include <stdio.h> int main() { static int array1[10][10], array2[10][10]; int i, j, m, n, a, b, c, p, q, r; printf("Enter no. of rows :: "); scanf("%d", &m); printf("\nEnter no. of cols :: "); scanf("%d",&n); printf("\nEnter values to the matrix :: \n"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("\nEnter a[%d][%d] value :: ",i,j); scanf("%d", &array1[i][j]); array2[i][j] = array1[i][j]; } } printf("\nThe given matrix is \n\n"); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) { printf("\t%d", array1[i][j]); } printf("\n\n"); } printf("\nEnter row number to be exchanged : "); scanf("%d", &a); printf("\nEnter other row number to be exchanged with : "); scanf("%d",&b); for (i = 0; i < m; ++i) { /* first row has index is 0 */ c = array1[a - 1][i]; array1[a - 1][i] = array1[b - 1][i]; array1[b - 1][i] = c; } printf("\nEnter col number to be exchanged : "); scanf("%d", &p); printf("\nEnter other col number to be exchanged with : "); scanf("%d",&q); for (i = 0; i < n; ++i) { /* first column index is 0 */ r = array2[i][p - 1]; array2[i][p - 1] = array2[i][q - 1]; array2[i][q - 1] = r; } printf("\nThe matix after interchanging the two rows :: \n"); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) { printf("\t%d", array1[i][j]); } printf("\n\n"); } printf("\nThe matix after interchanging the two columns :: \n"); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) { printf("\t%d", array2[i][j]); } printf("\n\n"); } return 0; }

**Output : :**

**Output : :**

/* C Program to Swap rows and columns of matrix */ Enter no. of rows :: 3 Enter no. of cols :: 3 Enter values to the matrix :: Enter a[0][0] value :: 1 Enter a[0][1] value :: 2 Enter a[0][2] value :: 3 Enter a[1][0] value :: 4 Enter a[1][1] value :: 5 Enter a[1][2] value :: 6 Enter a[2][0] value :: 7 Enter a[2][1] value :: 8 Enter a[2][2] value :: 9 The given matrix is 1 2 3 4 5 6 7 8 9 Enter row number to be exchanged : 2 Enter other row number to be exchanged with : 3 Enter col number to be exchanged : 1 Enter other col number to be exchanged with : 2 The matix after interchanging the two rows :: 1 2 3 7 8 9 4 5 6 The matix after interchanging the two columns :: 2 1 3 5 4 6 8 7 9

Above is the source code for C Program to Swap rows and columns of matrix which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may * Contact Us* through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

**Thanks for reading the post….**

Is it posssible for the admin to post a second solution to the above problemabove code for swapping is understandable ; but hoping to get more easier way to to achieve the output

above code for swapping is understandable ; but hoping to get more easier way to to achieve the output