C program to accept a matrix of given order and interchange any two rows and columns in the original matrix

by Nideesh C on April 12, 2011 · 1 comment

in C Programming




/* C program to accept a matrix of given order and interchange any two rows and columns in the original matrix */

#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 co-efficient 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”);
}
}

/*———————————————————————–
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
————————————————————————-*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to read A (MxN), find the transpose of a given matrix and output both the input matrix and the transposed matrix.
  2. Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..
  3. C Program to check if a given matrix is an identity matrix
  4. C program to read two matrices A (MxN) and B(MxN) and perform addition OR subtraction of A and B. Find the trace of the resultant matrix. Output the given matrix, their sum or Differences and the trace.
  5. C Program to accept two matrices and check if they are equal

Leave a Comment

{ 1 trackback }

Previous post:

Next post: