# Count rows/columns with sum equals to diagonal sum in C++

We are given a matrix which is a 2-D array having rows and columns and the task is to calculate the count of sum of all the rows and columns such that it is equal to the sum of either principal or secondary matrix.

Input

int arr[row][col] = {
{ 4, 1, 7 },
{ 10, 3, 5 },
{ 2, 2, 11}
}

Output − Count of rows/columns with sum equals to diagonal sum are &mins; 2

Explanation

sum of principal diagonal is: 4 + 3 + 11 = 18 and sum of secondary diagonal is: 7 + 3 + 2 = 12. Sum of rows are 4 + 1 + 7 = 12(TRUE), 10 + 3 + 5 = 18(TRUE), 2 + 2 + 11 = 15(FALSE) and sum of columns are: 4 + 10 + 2 = 16(FALSE), 1 + 3 + 2 = 6(FALSE), 7 + 5 + 11 = 23(FALSE). Therefore, the count of rows/columns matching with the sum of principal diagonal and secondary diagonal are − 2

Input

int arr[row][col] = {
{ 1, 2, 3 },
{ 4, 5, 2 },
{ 7, 9, 10}
}

Output − Count of rows/columns with sum equals to diagonal sum are − 2

Explanation

sum of principal diagonal is: 1 + 5 + 10 = 16 and sum of secondary diagonal is: 7 + 3 + 5 = 15. Sum of rows are 1 + 2 + 3 = 6(FALSE), 4 + 5 + 2 = 11(FALSE), 7 + 9 + 10 = 26(FALSE) and sum of columns are: 7 + 4 + 1 = 12(FALSE), 9 + 5 + 2 = 16(TRUE), 3 + 2 + 10 = 15(TRUE). Therefore, the count of rows/columns matching with the sum of principal diagonal and secondary diagonal are − 2

## Approach used in the below program is as follows

• Create a 2-D array to form a matrix of row size and column size

• Create variable for principal and secondary matrix. Also, a count variable to store the count

• Start loop FOR from i to 0 till col and j from col - 1 till col, increment the i and decrement the j

• Inside the loop, set principal as principal + matrix[i][i] and secondary as secondary + matrix[i][j]

• Start loop FOR from i to 0 till col

• Inside the loop, set row to 0 and col to 0 and inside the loop, start another loop FOR from j to 0 till col

• Inside the loops, set row as row + matrix[i][j]

• Inside the loop, start another loop FOR from j to 0 till col

Inside the loop, col to col + matrix[j][i]
• Inside the loop, check IF (row == principal) || (row == secondary) then increment the count by 1

• Inside the loop, check IF (col == principal) || (col == secondary) then increment the count by 1

• Return the count

• Print the result.

## Example

Live Demo

#include <iostream>
#define row 3
#define col 3
using namespace std;
int diagonal_sum(int matrix[row][col]){
int principal = 0;
int secondary = 0;
int r = 0;
int c = 0;
int count = 0;
int i = 0, j = 0;
for (i = 0, j = col - 1; i < col; i++, j--){
principal += matrix[i][i];
secondary += matrix[i][j];
}
for (int i = 0; i < col; i++){
r = 0;
c = 0;
for (int j = 0; j < col; j++){
r += matrix[i][j];
}
for (int j = 0; j < col; j++){
c += matrix[j][i];
}
if ((r == principal) || (r == secondary)){
count++;
}
if ((c == principal) || (c == secondary)){
count++;
}
}
return count;
}
int main(){
int matrix[row][col] = {
{ 4, 1, 7 },
{ 10, 3, 5 },
{ 2, 2, 11}};
cout<<"Count of rows/columns with sum equals to diagonal sum are: "<<diagonal_sum(matrix);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count of rows/columns with sum equals to diagonal sum are: 2