# Multiplication of Diagonal Elements of a Matrix in C++

In this tutorial, we will learn how to find the product of diagonal elements of a matrix in C++.

A matrix is known as a two-dimensional array/vector. It is represented in the form of rows and columns. It is used in many practical situations, and for many general purposes. It is a grid used to store and display data in a structured. It is synonymously associated with the form of a table, containing horizontal rows and vertical columns.

In an array of C++, say ‘a’ we access an element from within it using subscripts a[i][j], where ‘i’ denotes the row number, and ‘j’ denotes the column number. In C++, the entire matrix can be traversed using the following code:

int a[10][10]; for(int i = 0; i < 10; ++i) { for( int j = 0; j < 10; ++j) cout<<a[i][j]<<'\t'; cout<<'\n'; }

This would display each element of the array in a grid form on the output screen. The same logic can be applied for the access and evaluation of elements.

## The Diagonals of a Matrix in C++

The diagonals of a matrix entail the elements starting from one corner of the matrix to the other, moving diagonally across both ends. The diagonals are of two kinds: The major diagonal and the minor diagonal. The major diagonal is given through:

1 0 0

0 1 0

0 0 1

It is found that the subscript of the row and the subscript of the column are found to be equal, then they are part of the major diagonal. i.e., if i ==j, then a[i][j] is part of the main diagonal.

Similarly, for the minor diagonal can be found by creating and observing the matrix analytically:

0 0 1

0 1 0

1 0 0

On careful observation, it is seen that the sum of the row subscript and the column subscript always add up to one less than the degree of the square matrix. i.e., i + j == n – 1, where ‘n’ is the degree of the n x n matrix.

## To Find the Product of the Diagonal Elements of a matrix in C++

To find the multiplication of the diagonal elements, we use the above results to create the following C++ code:

#include<iostream> int main() { int a[10][10], i , j; long product_major = 1, product_minor = 1; for(i = 0; i < 10; ++i) {for(j = 0; j < 10; ++j) {if(i == j) product_major = product_major * a[i][j]; } } for(i = 0; i < 10; ++i) {for(j = 0; j < 10; ++j) {if(i + j == (10 - 1)) product_minor = product_minor * a[i][j]; } } std::cout<<"The product of the Major Diagonal elements is: "<< product_major <<"\nThe product of the Minor Diagonal elements is: "<< product_minor; return 0; }

**Output:**

The product of the Major Diagonal elements is: 0 The product of the Minor Diagonal elements is: 0

Here, we traverse the matrix twice, once for each diagonal. Within the inner loop of the traversal, we apply the conditional statement to check whether the element belongs to the diagonal. If the condition is satisfied, the total product is multiplied by the element that the traversal is on at that moment. After repeating the process until the entire matrix traversed, the complete product of the diagonal elements is found.

The process is repeated for the other diagonal. Once both the products are found, they are printed out onto the output screen.

In this article, we have gone through the concepts of matrices, their traversal and finding the product of diagonal elements.

You may also like:

## Leave a Reply