Unit Matrix : An unit matrix is a diagonal matrix whose elements in the diagonal are all ones.
eg
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
{
int mat[10][10];
cout<< "\n Enter dimension of square matrix;";
int n;
cin>>n;
cout<< "\n Enter the elements for the matrix :";
for(int i=0; i< n ; i++)
for (int j = 0; j< n ; j++)
{ cout<<"\n Element for positon : " << i+1 << " , " << j+1 << ":"; cin>> mat[i][j];
}
int flag = 0;
for(int i=0; i< n ; i++)
for (int j = 0; j< n ; j++)
{ if(i == j)
if(mat[i][j] ! = 1 )
{ flag = 1;
break; }
if(i != j)
if(mat[i][j] ! = 0)
{ flag = 1; break; }
}
if(flag == 0)
cout<< "\n An UNIT Matrix .";
else
cout<<"\n Not an UNIT matrix.";
cout<<"\n\n Matrix :";
for(int i=0; i< n ; i++)
{
for (int j = 0; j< n ; j++)
cout<< mat[i][j] << " ";
cout<< endl;
}
getch();
}
eg
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
#include <iostream.h>
#include <conio.h>
void main(){
int mat[10][10];
cout<< "\n Enter dimension of square matrix;";
int n;
cin>>n;
cout<< "\n Enter the elements for the matrix :";
for(int i=0; i< n ; i++)
for (int j = 0; j< n ; j++)
{ cout<<"\n Element for positon : " << i+1 << " , " << j+1 << ":"; cin>> mat[i][j];
}
int flag = 0;
for(int i=0; i< n ; i++)
for (int j = 0; j< n ; j++)
{ if(i == j)
if(mat[i][j] ! = 1 )
{ flag = 1;
break; }
if(i != j)
if(mat[i][j] ! = 0)
{ flag = 1; break; }
}
if(flag == 0)
cout<< "\n An UNIT Matrix .";
else
cout<<"\n Not an UNIT matrix.";
cout<<"\n\n Matrix :";
for(int i=0; i< n ; i++)
{
for (int j = 0; j< n ; j++)
cout<< mat[i][j] << " ";
cout<< endl;
}
getch();
}
No comments:
Post a Comment