Programmer

Friday, 8 June 2018

C Program to Accept a Matrix of Order M*N & Interchange the Diagonals.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
void main()
{
int a[10][10],i,j,n,temp;
system("cls");
cout<<"\n\nEnter Number of Rows And Columns :";
cin>>n;
randomize();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=random(20)+5;
}
}
cout<<"Before Interchange Diagonal "<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<setw(4)<<a[i][j];
}
cout<<endl;
}
cout<<"\n\nAfter Interchange Diagonal "<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j==n-1 || i==j)
{
temp=a[i][j];
a[i][j]=a[i][n-i-1];
a[i][n-i-1]=temp;
}
}
}
textcolor(YELLOW);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j==n-1 || i==j)
cprintf("%4d",a[i][j]);
else
cout<<setw(4)<<a[i][j];
}
cout<<endl;
}
getch();
}

/*
Enter Number of Rows And Columns :5
Before Interchange Diagonal
  22  15  24   6  13
  22  20  10   5   8
  22  11   5  11   9
  19   8  17  21   9
  23   5  12  12  14


After Interchange Diagonal
  13  15  24   6  22
  22   5  10  20   8
  22  11   5  11   9
  19  21  17   8   9
  14   5  12  12  23

*/

No comments:

Post a Comment

Program To Print Particular Line From File.

#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { ifstream fin; int count=0,num,l=1; char ...