-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5.cpp
More file actions
52 lines (44 loc) · 1.22 KB
/
Copy pathQ5.cpp
File metadata and controls
52 lines (44 loc) · 1.22 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
52
//Write a program to find sum of every row and every column in a two-dimensional array.
#include <iostream>
using namespace std;
int main() {
int rows, cols;
// Input matrix dimensions
cout << "Enter rows and columns: ";
cin >> rows >> cols;
int arr[20][20];
// Input elements
cout << "Enter the matrix elements:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
// Display matrix
cout << "\nMatrix:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
// Row sums
cout << "\nRow sums:\n";
for (int i = 0; i < rows; i++) {
int sum = 0;
for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
cout << "Row " << i + 1 << ": " << sum << endl;
}
// Column sums
cout << "\nColumn sums:\n";
for (int j = 0; j < cols; j++) {
int sum = 0;
for (int i = 0; i < rows; i++) {
sum += arr[i][j];
}
cout << "Column " << j + 1 << ": " << sum << endl;
}
return 0;
}