Program 36 - CSU1128 - CSE 2026 - Shoolini University

Program to find the trace of a matrix or sum of diagonals of a matrix entered by the user. Example: 2 2 : |1 2 3 4| i.e. diagnonal1 = 1 + 4 = 5

Explanation

The program calculates and prints the trace (sum of the main diagonal) and the sum of the secondary diagonal of a square matrix entered by the user.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Variable Declaration

The main() function is defined, and several variables are declared:

  • int d: Stores the size of the matrix (number of rows and columns).
  • int e[d][d]: Declares a 2D array with d rows and d columns to hold the matrix values.
  • int sum1 = 0: Initializes a variable to store the sum of the main diagonal.
  • int sum2 = 0: Initializes a variable to store the sum of the secondary diagonal.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter the size of the matrix and its elements:

  • printf("Enter the matrix size: ");: Prompts the user to enter the size of the matrix.
  • scanf("%d", &d);: Reads the size of the matrix and stores it in d.
  • int e[d][d];: Declares a 2D array with d rows and d columns.
  • printf("Enter the elements of the matrix:\n");: Prompts the user to enter the elements of the matrix.
  • for (int m = 0; m < d; m++): Outer loop to iterate over each row.
  • for (int j = 0; j < d; j++): Inner loop to iterate over each column.
  • scanf("%d", &e[m][j]);: Reads each element of the matrix and stores it in the array.

Diagonal Sums Calculation

The program calculates the sums of the main and secondary diagonals:

  • for (int o = 0; o < d; o++): Loop to iterate over each row (and column for a square matrix).
  • sum1 += e[o][o];: Adds the elements of the main diagonal (from top-left to bottom-right).
  • sum2 += e[o][d - o - 1];: Adds the elements of the secondary diagonal (from top-right to bottom-left).

Output Result

The program prints the sums of the main and secondary diagonals:

  • printf("Sum of main diagonal: %d\n", sum1);: Prints the sum of the main diagonal.
  • printf("Sum of secondary diagonal: %d\n", sum2);: Prints the sum of the secondary diagonal.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    printf("\n\n Program to find the trace of a matrix or sum of diagonals of a matrix entered by the user. Example: 2 2 : |1 2 3 4| i.e. diagnonal1 =  1 + 4 = 5  \n\n");

    // Read the matrix size from the user
    printf("Enter the matrix size: ");
    int d;
    scanf("%d", &d);

    // Declare a 2D array with d rows and d columns
    int e[d][d];

    // Read the elements of the matrix from the user
    printf("Enter the elements of the matrix:\n");
    for (int m = 0; m < d; m++)
    {
        for (int j = 0; j < d; j++)
        {
            scanf("%d", &e[m][j]);
        }
    }

    // Initialize the sums of the diagonals
    int sum1 = 0; // Sum of main diagonal
    int sum2 = 0; // Sum of secondary diagonal

    // Calculate the sums of the diagonals
    for (int o = 0; o < d; o++)
    {
        sum1 += e[o][o];         // Main diagonal
        sum2 += e[o][d - o - 1]; // Secondary diagonal
    }

    // Print the results
    printf("Sum of main diagonal: %d\n", sum1);
    printf("Sum of secondary diagonal: %d\n", sum2);

    return 0;
}
                    
                

Output

Program to find the trace of a matrix or sum of diagonals of a matrix entered by the user. Example: 2 2 : |1 2 3 4| i.e. diagnonal1 = 1 + 4 = 5

Enter the matrix size: 2 2
Enter the elements of the matrix: 4 3 2 6
Sum of main diagonal: 4
Sum of secondary diagonal: 7