program to print upper & lower triangle of a matrix

Answer Posted / medhane

#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 ( );

}

Is This Answer Correct ?    16 Yes 14 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain what is wrong with this statement? Myname = ?robin?;

1028


Tell me what is the purpose of 'register' keyword in c language?

620


What are the different properties of variable number of arguments?

669


Why pointers are used?

633


What does a derived class inherit from a base class a) Only the Public members of the base class b) Only the Protected members of the base class c) Both the Public and the Protected members of the base class d) .c file

669






What is dangling pointer in c?

626


Write the test cases for checking a variable having value in range -10.0 to +10.0?

1819


application attempts to perform an operation?

1496


main() { int i = 10; printf(" %d %d %d ", ++i, i++, ++i); }

637


write a c program for swapping two strings using pointer

2095


Write a program to print numbers from 1 to 100 without using loop in c?

643


What is #include stdio h?

688


Explain which of the following operators is incorrect and why? ( >=, <=, <>, ==)

611


What is a pointer in c plus plus?

698


Can static variables be declared in a header file?

619