Tuesday 19 February 2013

Trace-of-a-MATRIX




/*  Function to find TRACE of a matrix in C Language  */


/*
TRACE of a matrix is the sum of it's diagonal elements
*/

void findTrace(int arr[][10], int M, int N)  {

    int i, j , t = 0;

    //  loop calculating trace of matric
    for ( i = 0; i < M; i++)  {
       // inner loop
       for ( j = 0;j < N;j++)   {
           // equality checking for row equal to column i.e.i==j
           if ( i==j) {
              t = trace + arr[i][j];
           }
       }
    }

    printf("Trace of matrix is = %d \n", t);
}



/* ASSUME MATRIX TO BE: */



The trace of the above shown matrix is = 2 + 3 + 4
                                                 Trace = 9



Output:
Trace of matrix is = 9







No comments:

Post a Comment