"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Wednesday, February 10, 2010

Check A Matrix is 'Diagonal matrix' or not.

Diagonal Matrix : All matrix elements are zero except the left diagonal elements.
eg.
1 0 0

0 2 0
0 0 9



#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] == 0)
{
flag = 1;
break;
}


if(i != j)
if(mat[i][j] ! = 0)
{
flag = 1;
break;
}
}

if(flag == 0)
cout<< "\n A Diagonal Matrix .";
else
cout<<"\n Not a diagonal 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();
}

2 comments:

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.