-
Notifications
You must be signed in to change notification settings - Fork 492
/
TraceAndNormal.cpp
66 lines (59 loc) Β· 1.3 KB
/
TraceAndNormal.cpp
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
#include<iostream>
#include<cstdlib>
#include<math.h>
using namespace std;
int main()
{
//Declaration
int mat[50][50],trace=0,i,j,a,b;
float normal;
cout<<"Enter the order of the matrix : \n";
cin>>a>>b;
// Condition for trace of a matrix
if(a!=b)
{
cout<<"invalid matrix!\n";
exit(0);
}
// Taking the input of elements of a matrix
cout<<"Enter the elements of the matrix : \n";
for(i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
cin>>mat[i][j];
}
}
// print matrix elements
cout<<"The matrix is :"<<endl;
for(int i=0; i<a; i++)
{
for(int j=0; j<b; j++)
cout<<mat[i][j]<<"\t";
cout<<endl;
}
//sum of all elements for finding the normal
for(int i=0; i<a; i++)
{
for(int j=0; j<b; j++)
{
normal = normal + mat[i][j];
}
}
normal = sqrt(normal);
// sum of diagonal elements of a matrix for finding trace of a matrix
for(i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
if(i==j)
{
trace = trace + mat[i][j];
}
}
}
cout<<endl;
cout<<"The normal of matrix is "<<normal<<endl;
cout<<"The trace of matrix is "<<trace;
cout<<endl;
}