interchange any rows or columns of a matrix

Program to interchange any rows or columns of a matrix

#include < stdio.h>
#include < conio.h>
main()
{
int i,j,t,ch,a[10][10],r,c,r1,r2,c1,c2;
clrscr();
printf("\n\nEnter dimension of matrix : Rows : ");
scanf("%d",&r);
printf("columns: ");
scanf("%d",&c);
printf("enter the matrix:\n");
for(i=0;i < r;i++)
{
for(j=0;j < c;j++)
{
scanf("%d",&a[i][j]);
}}
printf("\n\n MENU\n1.Interchange rows\n2.interchange columns\n Enter your choice:");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter the no of rows:");
scanf("%d%d",&r1,&r2);
for(i=0;i < c;i++)
{
t=a[r1-1][i];
a[r1-1][i]=a[r2-1][i];
a[r2-1][i]=t;
}}
else if(ch==2)
{
printf("Enter the no of columns:");
scanf("%d%d",&c1,&c2);
for(i=0;i < r;i++)
{
t=a[i][c1-1];
a[i][c1-1]=a[i][c2-1];
a[i][c2-1]=t;
}}
else
printf("Invalid choice");
printf("\n\nMatrix is\n");
for(i=0;i < r;i++)
{
for(j=0;j < c;j++)
printf(" %d",a[i][j]);
printf("\n");
}
getch();
}

0 comments: