C Program For Swapping of 2 Matrices of same size

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],i,j,t,m,n,p,q;
clrscr();
printf("Enter the no.of rows and columns of 1st matrix: \n");
scanf("%d%d",&m,&n);
printf("Enter the no.of rows and columns of 2nd matrix: \n");
scanf("%d%d",&p,&q);
if((m!=p)||(n!=q))
{
printf("Swapping of given matrices is not possible");
getch();
exit(0);
}
printf("Enter %d * %d = %d elements: \n",m,n,m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

 printf("Enter %d * %d = %d elements: \n",m,n,m*n);
 for(i=0;i<p;i++)
 for(j=0;j<p;j++)
 scanf("%d",&b[i][j]);

printf("Original Matrices entered are: \n");
printf("1st Matrix\n");
for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
      {
  printf("%d",a[i][j]);
      }
     printf("\n");
   }
 printf("2nd Matrix\n");
 for(i=0;i<p;i++)
   {
  for(j=0;j<p;j++)
   {
   printf("%d",b[i][j]);
  }
  printf("\n");
    }
  for(i=0;i<m;i++)
  {
  for(j=0;j<n;j++)
  {
  t=a[i][j];
  a[i][j]=b[i][j];
  b[i][j]=t;
  }
  }
printf("Matrices after swapping:\n");
printf("1st Matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
 printf("2nd Matrix\n");
 for(i=0;i<p;i++)
 {
 for(j=0;j<p;j++)
 {
 printf("%d",b[i][j]);
 }
 printf("\n");
 }

getch();
}
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment