C++ Program to Find Norm and Trace of Matrix

C++ Program to Find Norm and Trace of Matrix

Write C++ Program to Find Norm and Trace of Matrix

// CPP Program to Find Norm and Trace of Matrix

// Normal is defined as the square root of the sum of the square of matrix elements
// Trace is defined as the sum of the diagonal element

#include <iostream>
#include <math.h>
#define max 100;

using namespace std;

int norm(int A[3][3],int n)
{
	int sum = 0, i, j;
	for(i = 0; i < n ; i++)
        {
	 	for(j = 0; j < n; j++)
	 	{
	 		sum += A[i][j] * A[i][j];
		}
	}
	return (sqrt(sum));
}

int trace(int A[3][3],int n)
{
	int sum = 0, i;
	for(i = 0; i < n ; i++)
        {
	 	sum = sum + A[i][i];
	}
    return sum;
}


int main()
{

	int i, j, A[3][3] = {{2, 6, 7},{6, 2, 8},{0, 1, 3}};
        cout << "Matrix A :--> \n";
	for(i = 0;i < 3; i++)
	{
		for( j = 0; j < 3; j++)
		{
			cout << A[i][j] << " ";
		}
		cout << endl;
	}

	cout << "\n\nNorm of Matrix :--> " << norm(A,3);
	cout << "\nTrace of Matrix :--> " << trace(A,3);

	return 0;
}

Output:

Matrix A :-->
2  6  7
6  2  8
0  1  3


Norm of Matrix :--> 14
Trace of Matrix :--> 7