Program to find sum of elements in each row and each column of the given matrix and print the greatest of the same

Program to find the sum of elements in each row and each column of the given matrix and print the greatest of the same is discussed here. Input the matrix and find the sum of each row and each column and display the row having the greatest sum and also the column having the greatest sum.

For example, consider the matrix

mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

Sum of row 1 = 1 + 2 + 3 = 6
Sum of row 2 = 4 + 5 + 6 = 15
Sum of row 3 = 7 + 8 + 9 = 24

Row 3 is having the greatest sum.

Sum of column 1 = 1 + 4 + 7 = 12
Sum of column 2 = 2 + 5 + 8 = 15
Sum of column 3 =  3 + 6 + 9 = 18

Column 3 is having the greatest sum.

 Algorithm to find the sum of elements in each row and each column of the given matrix

  • Input the number of rows and columns of the matrix.
  • Input the matrix elements.
  • Traverse the matrix and find the sum of each row and each column and display them.
  • Find the row having the greatest sum and display it.
  • Find the column having the greatest sum and display it.

Program to find the sum of elements in each row and each column of the given matrix and print the greatest of the same is given below.

/* C program to find the sum of elements in each row and each column of the given matrix and print the greatest of the same */#include
#include

int main()
{
int m, n, row, col, sum = 0, row_ind = 0, col_ind = 0;
scanf(“%d %d”,&m,&n);
int row_arr[m];
int i, j;
int mat[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat[i][j]);
}

int z = 0;

printf(“Sum of rows is “);
for(row=0; row<m; row++)
{
sum = 0;
for(col=0; col<n; col++)
{
sum += mat[row][col];
}

printf(“%d “,sum);

row_arr[z++] = sum;
}
int temp_row = row_arr[0];
for(i=1;i<m;i++)
{
if(temp_row < row_arr[i])
{
temp_row = row_arr[i];
row_ind = i;
}

}
printf(“\nRow %d has maximum sum “, row_ind + 1);

printf(“\nSum of columns is “);
sum = 0;
int y = 0;
int col_arr[n];
for (i = 0; i < n; ++i)
{
sum = 0;
for (j = 0; j < m; ++j)
{
sum = sum + mat[j][i];
}
printf(“%d “,sum);

col_arr[y++] = sum;

}

int temp_col = col_arr[0];
for(i=1;i<n;i++)
{
if(temp_col < col_arr[i])
{
temp_col = col_arr[i];
col_ind = i;
}

}
printf(“\nColumn %d has maximum sum “, col_ind + 1);

return 0;
}

/* C++ program to find the sum of elements in each row and each column of the given matrix and print the greatest of the same */
#include
using namespace std;
#include

int main()
{
int m, n, row, col, sum = 0, row_ind = 0, col_ind = 0;
cout << “\nEnter the order of the matrix : “;
cin >> m >> n;
int row_arr[m];
int i, j;
int mat[m][n];
cout << “\nInput the matrix elements : \n”;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat[i][j];
}

int z = 0;

cout << “\nSum of rows is “;
for(row=0; row<m; row++)
{
sum = 0;
for(col=0; col<n; col++)
{
sum += mat[row][col];
}

cout << sum << ” “;

row_arr[z++] = sum;
}
cout << endl;
int temp_row = row_arr[0];
for(i=1;i<m;i++)
{
if(temp_row < row_arr[i])
{
temp_row = row_arr[i];
row_ind = i;
}

}
cout << “\nRow ” << row_ind + 1 << ” has maximum sum “;
cout << endl;
cout << “\nSum of columns is “;
sum = 0;
int y = 0;
int col_arr[n];
for (i = 0; i < n; ++i)
{
sum = 0;
for (j = 0; j < m; ++j)
{
sum = sum + mat[j][i];
}
cout << sum << ” “;

col_arr[y++] = sum;

}

int temp_col = col_arr[0];
for(i=1;i<n;i++)
{
if(temp_col < col_arr[i])
{
temp_col = col_arr[i];
col_ind = i;
}

}
cout << endl;
cout << “\nColumn ” << col_ind + 1 << ” has maximum sum “;
cout << endl;
return 0;
}

/* Java program to find the sum of elements in each row and each column of the given matrix and print the greatest of the same */

import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int m, n, row, col, sum = 0, row_ind = 0, col_ind = 0;
m = sc.nextInt();
n = sc.nextInt();
int[] row_arr = new int[m];
int i, j;
int[][] mat = new int[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
mat[i][j] = sc.nextInt();
}

int z = 0;

System.out.print(“Sum of rows is “);
for(row=0; row<m; row++)
{
sum = 0;
for(col=0; col<n; col++)
{
sum += mat[row][col];
}

System.out.print(sum + ” “);

row_arr[z++] = sum;
}
int temp_row = row_arr[0];
for(i=1;i<m;i++)
{
if(temp_row < row_arr[i])
{
temp_row = row_arr[i];
row_ind = i;
}

}
System.out.print(“\nRow ” + ++row_ind + ” has maximum sum “);

System.out.print(“\nSum of columns is “);
sum = 0;
int y = 0;
int[] col_arr = new int[n];
for (i = 0; i < n; ++i)
{
sum = 0;
for (j = 0; j < m; ++j)
{
sum = sum + mat[j][i];
}
System.out.print(sum + ” “);

col_arr[y++] = sum;

}

int temp_col = col_arr[0];
for(i=1;i<n;i++)
{
if(temp_col < col_arr[i])
{
temp_col = col_arr[i];
col_ind = i;
}

}
System.out.print(“\nColumn ” + ++col_ind + ” has maximum sum “);
}

}

# Python program to find thesum of elements in each row and each column of the given matrix and print the greatest of the same

mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum = 0
row_ind = 0
col_ind = 0;
m = 3
n = 3
row_arr = []
z = 0
print(“Sum of rows is “, end = “”)
for row in range(0, m):
sum = 0
for col in range(0, n):
sum = sum + mat[row][col]
print(sum, end = ” “)
z = z + 1
row_arr.append(sum)

temp_row = row_arr[0]
for i in range(1, m):
if(temp_row < row_arr[i]):
temp_row = row_arr[i]
row_ind = i
print(“Row”, row_ind+1,”has maximum sum”)
print(“Sum of columns is “, end = “”)

sum = 0
y = 0
col_arr = []
for i in range(0, n):
sum = 0
for j in range(0, m):
sum = sum + mat[j][i]
print(sum, end = ” “)
y = y + 1
col_arr.append(sum)

temp_col = col_arr[0]
for i in range(1, n):
if(temp_col < col_arr[i]):
temp_col = col_arr[i]
col_ind = i
print(“Column”, col_ind+1,”has maximum sum”)

Output:

find the sum of elements in each row and each column of the given matrix