
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find the Normal and Trace
2-Dimensional arrays or matrices are very much useful in several applications. Matrices have rows and columns and store numbers inside them. In C++ also we can define 2D matrices using multi-dimensional arrays. In this article, we will see how to calculate the Normal and the Trace of a given matrix using C++.
The Normal is the square root of the sum of all elements present in the matrix. And the trace is the sum of elements present in the main diagonal. Let us see the algorithm and C++ code representation.
Matrix Normal
$\begin{bmatrix} 5 & 1& 8\newline 4 & 3& 9\newline 2& 7& 3\ \end{bmatrix},$
Sum of all elements: (5 + 1 + 8 + 4 + 3 + 9 + 2 + 7 + 3) = 42 Normal: (Square root of the sum of all elements) = √42 = 6.48
In the above example, we have taken one 3 x 3 matrix, here we get the sum of all elements, then perform the square root of it. Let us see the algorithm, for a better understanding.
Algorithm
- read matrix M as input
- consider M has n rows and n columns
- sum := 0
- for i ranging from 1 to n, do
- for j ranging from 1 to n, do
- sum := sum + M[ i ][ j ]
- end for
- for j ranging from 1 to n, do
- end for
- res := square root of the sum
- return res
Example
#include <iostream> #include <cmath> #define N 5 using namespace std; float solve( int M[ N ][ N ] ){ int sum = 0; for ( int i = 0; i < N; i++ ) { for ( int j = 0; j < N; j++ ) { sum = sum + M[ i ][ j ]; } } return sqrt( sum ); } int main(){ int mat1[ N ][ N ] = { {5, 8, 74, 21, 69}, {48, 2, 98, 6, 63}, {85, 12, 10, 6, 9}, {6, 12, 18, 32, 5}, {8, 45, 74, 69, 1}, }; cout << "Normal of the first matrix is: " << solve( mat1 ) << endl; int mat2[ N ][ N ] = { {6, 8, 35, 21, 87}, {99, 2, 36, 326, 25}, {15, 215, 3, 157, 8}, {96, 115, 17, 5, 3}, {56, 4, 78, 5, 10}, }; cout << "Normal of the second matrix is: " << solve( mat2 ) << endl; }
Output
Normal of the first matrix is: 28.0357 Normal of the second matrix is: 37.8418
Matrix Trace
$\begin{bmatrix} 5 & 1& 8\newline 4 & 3& 9\newline 2& 7& 3\ \end{bmatrix},$
Sum of all elements in main diagonal: (5 + 3 + 3) = 11 which is the trace of given matrix
In the above example, we have taken one 3 x 3 matrix, here we get the sum of all elements in the major diagonal. The sum is the trace of the matrix. Let us see the algorithm, for a better understanding.
Algorithm
- read matrix M as input
- consider M has n rows and n columns
- sum := 0
- for i ranging from 1 to n, do
- sum := sum + M[ i ][ i ]
- end for
- return sum
Example
#include <iostream> #include <cmath> #define N 5 using namespace std; float solve( int M[ N ][ N ] ){ int sum = 0; for ( int i = 0; i < N; i++ ) { sum = sum + M[ i ][ i ]; } return sum; } int main(){ int mat1[ N ][ N ] = { {5, 8, 74, 21, 69}, {48, 2, 98, 6, 63}, {85, 12, 10, 6, 9}, {6, 12, 18, 32, 5}, {8, 45, 74, 69, 1}, }; cout << "Trace of the first matrix is: " << solve( mat1 ) << endl; int mat2[ N ][ N ] = { {6, 8, 35, 21, 87}, {99, 2, 36, 326, 25}, {15, 215, 3, 157, 8}, {96, 115, 17, 5, 3}, {56, 4, 78, 5, 10}, }; cout << "Trace of the second matrix is: " << solve( mat2 ) << endl; }
Output
Trace of the first matrix is: 50 Trace of the second matrix is: 26
Conclusion
The normal and trace are matrix operations. To perform these two operations, we need a square matrix (for trace square is needed). The normal is just the square root of the sum of all elements present in the matrix, and the trace is the sum of elements present in the major diagonal of the matrix. The matrix can be represented using 2D arrays in C++. Here we have taken two examples of the matrices with 5 rows and 5 columns (a total of 25 elements). Accessing the matrix needs looping statements with index manipulation. For the normal calculation, we need to go through each element, so two nested loops are needed. And the complexity of this program is O(n2). For trace, as we need to see only the major diagonal, the row and column indices will be the same. So only one for loop is sufficient. It can be calculated in O(n) time.
- Related Articles
- Python Program to Find the Normal and Trace
- C++ Program To Find the Trace and Normal of a given Matrix
- Golang Program To Find The Trace And Normal Of A Matrix
- Java Program To Find the Trace and Normal of a given Matrix
- Golang Program To Find the Trace and Normal of a given Matrix
- Swift Program to Find the Trace and Normal of a given Matrix
- Python Program To Find the Trace and Normal of a given Matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++ Program
- Java Program to Convert a Stack Trace to a String
- Find normal at a given point on the curve in C++
- Trace or track Python statement execution (trace)
- How to find the proportion using normal distribution in R?
- How to rethrow InnerException without losing stack trace in C#?
- C++ Program to Find Quotient and Remainder
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++
