A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

Trace of Square Matrix – C Program

Definition:

Trace of a Square Matrix is the Sum of principal diagonal elements.

To find the trace, the matrix must be Square Matrix. We can not find the trace of Non Square matrix.

Principal diagonal is also referred as primary diagonal or main diagonal.

Note: For square matrix – Number of Rows = Number of Columns.


Description:

i and j – Loop control variables.
r – Number of rows/columns of square matrix.
a – 2 Dimensional array to store matrix.
sum – sum of elements of primary diagonal.
To read matrix nested for loop is used. i: Outer for loop, j: Inner for loop.
Input : Square matrix.
Output : Sum of principal diagonal items.

Algorithm to find the trace of a square matrix:

STEP 1: START
STEP 2: SET the value of sum
              sum = 0
STEP 3: READ number of rows/columns of square matrix
             READ r
STEP 4: READ the elements of square matrix
             LOOP from i = 0 to r
             LOOP from j = 0 to r
             READ a(i)(j)  
STEP 5: CALCULATE the sum of principal diagonal elements
             LOOP from i = 0 to r
             sum = sum + a(i)(i)
STEP 6: END

Example : C Program to find the trace of square matrix

# include<stdio.h>
#include<conio.h>
void main()
{
    int a[10][10],r,i,j,sum=0;
    printf ("Enter the number of rows/columns of square matrix: ") ;
    scanf ("%d", &r);
    printf ("Enter the elements of matrix: \n");
    for( i=0; i<r;i++)
        for( j=0; j<r; j++)
            scanf ("%d", &a[i][j]);
    for( i=0; i<r; i++)
        sum = sum + a[i][i];
    printf ("Trace of the matrix: %d", sum);
    getch();
}

Output 1:

Enter the number of rows/columns of square matrix: 3
Enter the elements of matrix:
1 2 3
4 5 6
7 8 9
Trace of the matrix: 15

Output 2:

Enter the number of rows/columns of square matrix: 4
Enter the elements of matrix:
2 4 1 0
9 8 4 2
1 3 0 11
6 7 2 9
Trace of the matrix: 19

Example : C Program to find the trace of square matrix using Function.

# include<stdio.h>
#include<conio.h>
void trace(int r, int a[10][10])
{
    int i,sum=0;
    for( i=0; i<r; i++)
        sum = sum + a[i][i];
    printf ("Trace of the matrix: %d", sum);
}
void main()
{
    int a[10][10],r,i,j;
    printf ("Enter the number of rows/columns of square matrix: ") ;
    scanf ("%d", &r);
    printf ("Enter the elements of matrix: \n");
    for( i=0; i<r;i++)
        for( j=0; j<r; j++)
            scanf ("%d", &a[i][j]);
    trace(r,a);
    getch();
}

Output:

Enter the number of rows/columns of square matrix: 4
Enter the elements of matrix:
3 4 2 9
6 8 4 3
1 2 7 5
5 4 3 2
Trace of the matrix: 20

Explanation:

In the above program we are using trace() function to find the sum of principal diagonal elements.

From main() function elements of matrix ‘a’ are read and then matrix ‘a’ is passed to the trace() function as an argument. The trace() function then calculates the trace of matrix that is the sum of principal diagonal elements.

QUIZ:

1] Trace of a matrix can be found for.

  1. Non square matrix
  2. Square matrix
  3. Any matrix
  4. None

2] In square matrix.

  1. All elements are 1
  2. All elements are 0
  3. Number of rows = Number of columns
  4. All elements are square roots