Write a C++ Program to Find the Sum of Opposite Diagonal of a Matrix with an example. Within the C++ for loop, we are adding the value of all the opposite diagonal values of the given matrix.
#include<iostream> using namespace std; int main() { int i, j, rows, columns, sum = 0; cout << "\nPlease Enter the Matrix rows and Columns = "; cin >> i >> j; int sumOppDgnalArr[i][j]; cout << "\nPlease Enter the Matrix Items\n"; for(rows = 0; rows < i; rows++) { for(columns = 0; columns < i; columns++) { cin >> sumOppDgnalArr[rows][columns]; } } for(rows = 0; rows < i; rows++) { sum = sum + sumOppDgnalArr[rows][i - rows - 1]; } cout << "\nThe Sum of Opposite Diagonal Elements of a Matrix = " << sum; return 0; }
i = 3, rows = 0
sum = sum + sumOppDgnalArr[0][3 – 0 – 1] => sum + sumOppDgnalArr[0][2]
sum = 0 + 30
i = 3, rows = 1, and sum = 30
sum = sum + sumOppDgnalArr[1][1]
sum = 30 + 90 = 120
i = 3, rows = 2, and sum = 120
sum = sum + sumOppDgnalArr[2][0]
sum = 120 + 70 = 190
C++ Program to Find Sum of Opposite Diagonal of a Matrix Example 2
In this C++ example, we added two extra cout statements to show you the iteration number, row value, and sum at each iteration.
#include<iostream> using namespace std; int main() { int i, j, rows, columns, sum = 0; cout << "\nPlease Enter the Matrix rows and Columns = "; cin >> i >> j; int sumOppDgnalArr[i][j]; cout << "\nPlease Enter the Matrix Items\n"; for(rows = 0; rows < i; rows++) { for(columns = 0; columns < i; columns++) { cin >> sumOppDgnalArr[rows][columns]; } } for(rows = 0; rows < i; rows++) { cout << "\nIteration = " << rows + 1 << ", Row Number = " << rows << " and Sum = " << sum; sum = sum + sumOppDgnalArr[rows][i - rows - 1]; cout << "\nsumOppDgnalArr["<<rows<<"]["<< i - rows - 1 <<"] = " << sumOppDgnalArr[rows][i - rows - 1] << " and sum + sumOppDgnalArr["<<rows<<"]["<< i - rows - 1 <<"] = " << sum << endl; } cout << "\nThe Sum of Opposite Diagonal Elements of a Matrix = " << sum; return 0; }
Please Enter the Matrix rows and Columns = 2 2
Please Enter the Matrix Items
11 22
99 122
Iteration = 1, Row Number = 0 and Sum = 0
sumOppDgnalArr[0][1] = 22 and sum + sumOppDgnalArr[0][1] = 22
Iteration = 2, Row Number = 1 and Sum = 22
sumOppDgnalArr[1][0] = 99 and sum + sumOppDgnalArr[1][0] = 121
The Sum of Opposite Diagonal Elements of a Matrix = 121
It is another approach to write a C++ Program to calculate the Sum of Matrix Opposite Diagonal.
#include<iostream> using namespace std; int main() { int i, j, rows, columns, sum = 0; cout << "\nPlease Enter the Matrix rows and Columns = "; cin >> i >> j; int sumOppDgnalArr[i][j]; cout << "\nPlease Enter the Matrix Items\n"; for(rows = 0; rows < i; rows++) { for(columns = 0; columns < i; columns++) { cin >> sumOppDgnalArr[rows][columns]; } } for(rows = 0; rows < i; rows++) { for(columns = 0;columns < j; columns++) { if(rows + columns == ((i + 1) - 2)) { sum = sum + sumOppDgnalArr[rows][columns]; } } } cout << "\nThe Sum of Opposite Diagonal Elements of a Matrix = " << sum; return 0; }
Please Enter the Matrix rows and Columns = 3 3
Please Enter the Matrix Items
11 22 33
44 55 65
99 10 122
The Sum of Opposite Diagonal Elements of a Matrix = 187