In this example , we will add the diagonal matrix
#include <iostream>
using namespace std;
const int MAX = 100;
void Diagonal_Sum(int add[][MAX], int n){
int principal = 0, secondary = 0;
for(int i=0; i<n; i++){
for( int j=0; j<n; j++){
if(i == j){
principal += add[i][j];
}
if((i+j) == (n-1)){
secondry += add[i][j];
}
}
}
cout<<"print principal sum : "<<principal<<endl;
cout<<"print Secondary sum : "<<secondary<<endl;
}
int main(){
int a[][MAX] = {{1,2,3,4},
{1,4,7,8},
{3,5,6,8},
{7,5,9,2}};
Diagonal_Sum(a, 4);
return 0;
}
Output:
Principal diagonal sum: 13
Secondary diagonal sum: 23
Explanation:
Step 1: Including Libraries and Defining Constants
#include <iostream>
using namespace std;
const int MAX = 100;
#include <iostream>
: This library is included to allow input/output functions likecout
for printing.using namespace std;
: This avoids the need to prefix standard C++ functions likecout
withstd::
.const int MAX = 100;
: This defines a constant valueMAX
representing the maximum number of columns in the 2D array. This value is used as the second dimension size of the array.
Step2: Diagonal_Sum Function
void Diagonal_Sum(int add[][MAX], int n){
int principal = 0, secondary = 0;
- Function Definition: The function
Diagonal_Sum
takes a 2D arrayadd
and an integern
representing the number of rows and columns (since it’s a square matrix). principal
andsecondary
variables: These variables store the sum of the principal and secondary diagonals, respectively. They are initialized to 0.
Nested Loops to Traverse the Array
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
- The two
for
loops iterate over the elements of the square matrix. The outer loop runs for the rows (i
), and the inner loop runs for the columns (j
). i
represents the row index, andj
represents the column index.
Summing the Principal Diagonal
if(i == j){
principal += add[i][j];
}
- The principal diagonal consists of the elements where the row index
i
is equal to the column indexj
(e.g., elements likea[0][0]
,a[1][1]
,a[2][2]
, etc.). - The condition
if(i == j)
checks if the current element is on the principal diagonal, and if true, it adds the value toprincipal
.
Summing the Secondary Diagonal
if((i + j) == (n - 1)){
secondary += add[i][j];
}
- The secondary diagonal consists of the elements where the sum of the row index
i
and the column indexj
is equal ton - 1
(e.g.,a[0][3]
,a[1][2]
,a[2][1]
, etc.). - The condition
if((i + j) == (n - 1))
checks if the current element is on the secondary diagonal, and if true, it adds the value tosecondary
.
Printing the Results
cout << "Principal diagonal sum: " << principal << endl;
cout << "Secondary diagonal sum: " << secondary << endl;
- After the loops finish, the function prints the sums of the principal and secondary diagonals.
Step 3: Main Function
The main
function sets up the 2D array and calls the Diagonal_Sum
function.
int main() {
int a[][MAX] = {{1, 2, 3, 4},
{1, 4, 7, 8},
{3, 5, 6, 8},
{7, 5, 9, 2}};
- The 2D array
a
is defined with 4 rows and 4 columns (a 4×4 square matrix). The second dimension is set toMAX
, but only the first 4 columns are initialized with values. - The array looks like this:
{1, 2, 3, 4},
{1, 4, 7, 8},
{3, 5, 6, 8},
{7, 5, 9, 2}
Calling the Diagonal_Sum Function
Diagonal_Sum(a, 4);
return 0;
}
- The
Diagonal_Sum
function is called with the arraya
and the number4
, which represents the size of the matrix (4×4). - The function will calculate and print the sum of the principal and secondary diagonals.
Output of the code:
- Principal diagonal: 1 (a[0][0]) + 4 (a[1][1]) + 6 (a[2][2]) + 2 (a[3][3]) = 13
- Secondary diagonal: 4 (a[0][3]) + 7 (a[1][2]) + 5 (a[2][1]) + 7 (a[3][0]) = 23