Thursday, 5 March 2015

Cpp Program to calculate trace of a matrix


This is a Cpp Program which calculates the trace of a matrix.

Generally trace of a square matrix is calculated as the sum of all the diagonal elements in a matrix.




Cpp Program to find trace of a matrix


PROGRAM :
#include <iostream>
#include<math.h>

using namespace std;

int main()
{
    int sum=0,rows,columns,i,j;
    float normal;
    cout <<" Enter order of matrix : \t";
    cin >> rows;
    cin >> columns;
    int matrix[rows][columns];
    cout << "Enter elements of the matrix \n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
        {
            cout << "Enter element m"<<i+1<<j+1<<" :\t";
            cin >> matrix[i][j];
        }
    }
    cout << "\n\n MATRIX is :\n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
            cout <<"\t" << matrix[i][j] ;
        cout << "\n";

    }
    cout << "\n\nIts NORMAL is:\t";
    for(j=0; j<columns; j++)
    {
        for(i=0; i<rows; i++)
            sum  = sum +pow( matrix[i][j] , 2 );

    }
    normal = sqrt(sum);
    cout << normal;
    return 0;
}


OUTPUT : 
Cpp Program to find trace of a matrix