-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.cpp
More file actions
51 lines (42 loc) · 1.48 KB
/
23.cpp
File metadata and controls
51 lines (42 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// C++ program to find the sum of elements present in all the rows and columns of a matrix separately.
#include <iostream>
#define SIZE 10
using std::cin, std::cout;
int main()
{
// declaring the required variables.
int matrix[SIZE][SIZE], rowCount, columnCount, i, j, rowSum, columnSum;
// entering the values for the variables from the console.
cout << "Enter the order of the matrix: ";
cin >> rowCount >> columnCount;
cout << "Enter the elements of the matrix:\n";
for(i = 0; i < rowCount; ++i)
{
for(j = 0; j < columnCount; ++j)
{
cin >> matrix[i][j];
}
}
// calculating the sum of elements present in all the rows and printing the same.
for(i = 0; i < rowCount; ++i)
{
rowSum = 0;
for(j = 0; j < columnCount; ++j)
{
rowSum += matrix[i][j];
}
// printing the calculated sum to the console.
cout << "Sum of the elements present in Row-" << (i + 1) << " is: " << rowSum << "\n";
}
// calculating the sum of elements present in all the columns and printing the same.
for(i = 0; i < columnCount; ++i)
{
columnSum = 0;
for(j = 0; j < rowCount; ++j)
{
columnSum += matrix[j][i];
}
// printing the calculated sum to the console.
cout << "Sum of the elements present in Column-" << (i + 1) << " is: " << columnSum << "\n";
}
}