# Diagonally Dominant Matrix in C++ Program

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this tutorial, we are going to write a program that helps us to find whether the given matrix is diagonally dominant or not.

The matrix is called a diagonally dominant matrix if the sum of elements in the matrix other than the diagonal element is less than the diagonal matrix. Let's see an example.

421
352
247

The above matrix is a diagonally dominant matrix. Because

4 > 2 + 1
5 ≥ 3 + 2
7 > 4 + 2

All the diagonal elements are greater than or equal to the sum of the non-diagonal elements in the same row.

Let's see the steps to solve the problem.

• Iterate over the rows and columns of the matrix.

• Find the sum of non-diagonal elements.

• Compare the sum of the non-diagonal elements with the diagonal element.

• If the sum of non-diagonal elements is greater than the diagonal element, then print "No".

• Print "Yes".

## Example

Let's see the code.

#include <bits/stdc++.h>
using namespace std;
#define N 3
bool isDiagonallyDominantMatrix(int matrix[N][N], int n) {
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
if (i != j) {
sum += abs(matrix[i][j]);
}
}
if (abs(matrix[i][i]) < sum) {
return false;
}
}
return true;
}
int main() {
// int matrix[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int matrix[N][N] = {{4, 2, 1}, {3, 5, 2}, {2, 4, 7}};
if (isDiagonallyDominantMatrix(matrix, 3)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}

## Output

If you run the above code, then you will get the following result.

Yes

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.