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

Monday, February 1, 2010

Write a program to print the upper and lower half triangle of a matrix.

#include (iostream.h)
#include (conio.h)

# define n 10

void main( )
{
int mat[n][n];

int d;

// Input elements
cout<<  "\n  Enter dimension of square matrix:";
cin >> d;

for(int i = 0; i < d ; i++)
for( int j = 0; j < d ; j++)
{cout<<"\n Enter elements for "<<  i+1 << "," << j+1 <"location :";
cin >> mat[i][j];
}

clrscr();

//Print the array

cout<<"\n The Original matrix : \n\n";
for( i = 0; i < d ; i++)

{for( j = 0; j < d ; j++)
   cout<<  mat[i][j]<<"\t";
cout<< "\n";
}

//upper half of left diagonal ----
cout<<"\n The Upper half of the matrix : \n\n";

for( i = 0; i < d ; i++)
{
 for( j = 0; j < d ; j++)
     { if(i < j)
         cout <<  mat [i][j] << " " ;
      else
         cout << " " << " ";
       }
 cout << "\n ";
}

//lower half of left diagonal -----

cout<<"\n The Lower half of the matrix : \n\n";



for( i = 0; i < d ; i++)
{
 for( j = 0; j < d ; j++)
     { if(i >  j)
         cout <<  mat [i][j] << " " ;
      else
         cout << " " << " ";
       }
 cout << "\n ";
}
getch ( );

}

No comments:

Post a Comment

Loading...
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.