program to print upper & lower triangle of a matrix
Answers were Sorted based on User's Feedback
Answer / 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 |
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5][5],i,j;
clrscr();
printf("\n");
//upper triangle matrix
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
arr[i][j]=(i+j);
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(j<4-i)
{printf(" ");
}
else printf("%d",arr[i][j]);
}
printf("\n");
}
printf("\n");
//lower triangle matrix
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(j>=4-i)
{printf(" ");
}
else printf("%d",arr[i][j]);
}
printf("\n");
}
getch();
}
Is This Answer Correct ? | 6 Yes | 9 No |
can we declare a function inside the structure? ex: struct book { int pages; float price; int library(int,float); }b; is the above declaration correct? as it has function declaration?
What is memcpy() function?
what is the difference between char * const and const char *?
What's the best way to declare and define global variables?
Is struct oop?
what is a pointer
4 Answers Bank Of America, TCS,
What are the types of operators in c?
Who invented b language?
what is the differnce between programing langauge and tool? is sas is a programing langauge r tool?
A c program to display count values from 0 to 100 and flash each digit for a secong.reset the counter after it reaches 100.use for loop,. pls guys hepl me.. :(
What is meant by int main ()?
Find the O/p of the following struct node { char *name; int num; }; int main() { struct node s1={"Harry",1331}; struct node s2=s1; if(s1==s2) printf("Same"); else printf("Diff"); }