-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex23.c++
92 lines (77 loc) · 1.33 KB
/
ex23.c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//@rofi
/**
23. Write a program to display the upper Triangle and
lower Triangle of a given square matrix using function.
*/
#include <iostream>
using namespace std;
//function to display a matrix
void display_matrix(int a[][10], int n)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
cout<<"\n";
}
//function to display upper triangular matrix
void upper_triangular(int a[][10], int n)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
//check for upper triangular
if(i<=j){
cout<<a[i][j]<<" ";
}else{
cout<<"0"<<" ";
}
}
cout<<"\n";
}
cout<<"\n";
}
//function to display lower triangular matrix
void lower_triangular(int a[][10], int n)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
//check for upper triangular
if(i>=j){
cout<<a[i][j]<<" ";
}else{
cout<<"0"<<" ";
}
}
cout<<"\n";
}
cout<<"\n";
}
int main()
{
//test case
int matrix[][10] = {
{1,2,3},
{2,4,5},
{3,5,6}
};
//Input matrix is
cout<<"Input matrix : \n";
display_matrix(matrix,3);
//function call
cout<<"upper triangular: \n";
upper_triangular(matrix,3);
cout<<"lower triangular: \n";
lower_triangular(matrix,3);
return 0;
}