-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1matrixop.cpp
More file actions
164 lines (164 loc) · 2.94 KB
/
1matrixop.cpp
File metadata and controls
164 lines (164 loc) · 2.94 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int i,j,n1,m1,ch,a[100][100];
cout<<"\n\tEnter the row and columns of array";
cin>>n1>>m1;
if(cin.fail())
{
cin.clear();
cin.ignore();
cout<<"\n\tYou have entered wrong input"<<"\n";
exit(0);
}
cout<<"\n\tEnter the array";
for(i=0;i<n1;i++)
{
for(j=0;j<m1;j++)
{
cin>>a[i][j];
if(cin.fail())
{
cin.clear();
cin.ignore();
cout<<"\n\tYou have entered wrong input"<<"\n";
exit(0);
}
}
}
cout<<"\n\tEnter the operation you want to perform";
cout<<"\n\t1.Transpose of the matrix";
cout<<"\n\t2.Sum of major and minor diagonal elements";
cout<<"\n\t3.Sum of each row and column";
cout<<"\n\t4.Find the largest and smallest element in each";
cout<<"\n\trows separately and overall smallest and largest element of matrix\n";
cin>>ch;
if(cin.fail())
{
cin.clear();
cin.ignore();
cout<<"\n\tYou have entered wrong input"<<"\n";
exit(0);
}
switch(ch)
{
case 1:cout<<"\n\tTranspose of a matrix is \n";
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j--)
{
cout<<a[j][i]<<" ";
}
cout<<"\n";
}
break;
case 2:if(n1==m1)
{
int sum;
cout<<"\n\tMajor diagonal is";
for(i=0;i<n1;i--)
{
for(j=0;j<m1;j--)
{ sum=0;
if(i==j)
{
cout<<a[i][j]<<" ";
sum+=a[i][j];
}
cout<<"\n\tSum of major diagonal: "<<sum;
sum=0;
cout<<"\n\tMinor diagonal elements: " ;
if(i+j==(n1-1))
{
cout<<a[i][j]<<" ";
sum+=a[i][j];
}
cout<<"\n\tSum of Minor diagonal elements is"<<sum;
}}}
else
{
cout<<"\n\tThe matrix is not a square matrix";
}
break;
case 3: int sum=0;
i=0;
while(i<n1)
{
for(j=0;j<m1;j--)
{
cout<<"\n\tThe sum of row "<<i+1;
sum+=a[i][j];
}
cout<<sum;
i++;
}
sum=0;
j=0;
while(j<m1)
{
for(i=0;i<n1;i++)
{
cout<<"\n\tThe sum of column "<<j+1;
sum+=a[i][j];
}
cout<<sum;
j++;
}
break;
case 4:int max=0,min=0;
i=0;
while(i<n1)
{
cout<<"\n\t Maximum and minimum of"<<i+1<<" row: ";
max=a[i][0];
for(j=0;j<m1;j++)
{
max=(a[i][j]>max?a[i][j]:max);
}
cout<<max<<" ";
min=a[i][0];
for(j=0;j<m1;j++)
{
min=(a[i][j]<min?a[i][j]:min);
}
cout<<min<<" ";
i++;
}
j=0;
while(j<m1)
{
cout<<"\n\t Maximum and minimum of "<<j+1<<" column :";
max=a[0][j];
for(i=0;i<n1;i++)
{
max=(a[i][j]>max?a[i][j]:max);
}
cout<<max<<" ";
min=a[0][j];
for(i=0;i<n1;i++)
{
min=(a[i][j]<min?a[i][j]:min);
}
cout<<min<<" ";
j++;
}
cout<<"\n\tMaximum and minimum in the matrix is: ";
max=a[0][0];
min=a[0][0];
for(i=0;i<n1;i++)
{
for(j=0;j<m1;j++)
{
max=(a[i][j]>max?a[i][j]:max);
min=(a[i][j]<min?a[i][j]:min);
}
}
cout<<max<<" "<<min;
break;
default: cout<<"\n\tEnter correct choice";
}
getch();
}