Sum of Diagonal Elements of Matrix in C Language

Sum-of-Diagonal-Elements-of-Matrix-in-C-Language

Program Description:

write a Program to calculate the Sum of Diagonal Elements of Matrix in C Programming Language. The program should accept a matrix from the user and calculate the sum of the main diagonal elements of the array.

Diagonal Elements in Matrix:

We have a matrix X with dimensions m x n. Where each element is xij, Then X[i][j] is a diagonal element if i == j

Let’s look at an example.

Matrix X:

The diagonal Elements of the Matrix X are:

100 500 900

The Sum of the diagonal elements is 1500

Here is the graphical representation of the Diagonal elements of a Matrix

sum-of-diagonal-elements-of-matrix

Prerequisites:

Let’s look at a C Program to calculate the Sum of Diagonal Elements of a Matrix.

Sum of Diagonal Elements of Matrix in C Program Explanation:

  1. Create two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns.(Change this number if you want to use large arrays)
  2. Start the Program by declaring a Matrix (2D Arrays) named X[ROWS][COLUMNS]. The row size and column size of the matrices are ROWS and COLUMNS respectively.
  3. Prompt the user to provide the desired Row size and Column Size, and update them in the variables rows and columns respectively.
  4. Check if the array size is going out of present limits ( 1-100) using the (rows >= ROWS || rows <= 0 || columns >= COLUMNS || columns <= 0) condition, And display an error message if the row/column sizeis beyond the limits.
  5. Take the user input for Matrix-X, We need to create two For Loops, One For Loop will iterate over the rows, and the Second For Loop will iterate over the columns
  6. Create the First For Loop ( Outer Loop), Start the iteration from i=0, and go till the i<rows (which is equal to for(i=0; i<rows; i++) ). At Each iteration of the Outer Loop,
    • Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<columns (i.e for(j=0; j<columns; j++) ). At each iteration of the Inner Loop
      • Prompt the user to provide the Matrix or 2D Array element and Read the element using the scanf function and Update the X[i][j] element.
      • Repeat the above step for all elements in the 2D array or matrix
  7. Initialize the variable sum to hold the sum of diagonal elements. initialize sum with zero(0)
  8. To Calculate the Sum of Diagonal Elements of the Matrix-X, We need to go through the array and print all elements where the element row index is equal to the column index. So create another two loops to iterate over the matrix.
  9. Create Outer For Loop to iterate over rows – for(i=0; i<rows; i++)
    • Create Inner For loop to iterate over columns – for(j=0; j<columns; j++), At Each Iteration,
      • Add the X[i][j] to sum where i == j.
  10. Once the above Step-9 is finished, The sum variable contains the sum of the diagonal elements of the matrix. And display the results on the console.

Program to Sum of Diagonal Elements of Matrix in C language:

Here is the program to calculate the sum of Diagonal Elements of the matrix in C Programming language

Program Output:

Compile and Run the program

sum-of-diagonal-elements-of-matrix-in-c-program-output

In the above program, The Sum of the main diagonal elements is 1500 (i.e 100, 500, and 900)

Let’s look at a few more examples.

As excepted the program is displaying the desired results.

If the user enters an Invalid size, Program displays an error message Invalid Rows or Columns size, Please Try again

Sum of Diagonal Elements of Matrix using user defined Functions in C:

Here is the modified version of the sum of diagonal elements of matrix program using the user defined functions.

We have created two user defined functions(Except main() function) in the above program

  1. readMatrix() function is used to read the elements from the user and update the given matrix.
  2. The sumOfDiagonal() function will calculate the sum of the main diagonal elements and returns the results.

Program Output:

Let’s compile and run the program.

program-sum-of-main-diagonal-elements-of-2darray-using-functions

As we can see from the above output, The program is excepted results.

Related Matrix Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Sum of Diagonal Elements of Matrix in C Language […]

Leave a Reply