Repository navigation
Expand file tree
/
Copy pathSolution-01.cpp
More file actions
188 lines (142 loc) · 3.14 KB
/
Copy pathSolution-01.cpp
File metadata and controls
188 lines (142 loc) · 3.14 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Problem Statement:
* Write a program to fill a 3x3 matrix with random numbers, then print the sum of each column.
*
*/
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
using namespace std;
int RandomNumber(int From, int To)
{
return rand() % (To - From + 1) + From;
}
void FillMatrixWithRandomNumbers(int arr[3][3], short Rows, short Cols)
{
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
arr[i][j] = RandomNumber(1, 100);
}
}
}
void PrintMatrix(int arr[3][3], short Rows, short Cols)
{
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
cout << setw(3) << arr[i][j] << " ";
}
cout << endl;
}
}
void SumEachColInMatrix(int arr[3][3], short Rows, short Cols)
{
int Sum = 0;
for (int j = 0; j < Cols; j++)
{
for (int i = 0; i < Rows; i++)
{
Sum += arr[i][j];
}
cout << "Col " << j + 1 << " Sum = " << Sum << endl;
Sum = 0;
}
}
/*
int Sum = 0;
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
Sum += arr[i][j];
}
cout << "Col " << j + 1 << " Sum = " << Sum << endl;
Sum = 0;
}
j=0 j=1 j=2
↓ ↓ ↓
i=0 → 1 2 3
i=1 → 4 5 6
i=2 → 7 8 9
i = 0
j=0 → arr[0][0] = 1
j=1 → arr[0][1] = 2
j=2 → arr[0][2] = 3
↓
i = 1
j=0 → arr[1][0] = 4
j=1 → arr[1][1] = 5
j=2 → arr[1][2] = 6
↓
i = 2
j=0 → arr[2][0] = 7
j=1 → arr[2][1] = 8
j=2 → arr[2][2] = 9
1 → 2 → 3
4 → 5 → 6
7 → 8 → 9
❌ ده Row by Row
*/
/*
j=0 j=1 j=2
↓ ↓ ↓
i=0 → 1 2 3
i=1 → 4 5 6
i=2 → 7 8 9
j = 0
i=0 → arr[0][0] = 1
i=1 → arr[1][0] = 4
i=2 → arr[2][0] = 7
j = 1
i=0 → arr[0][1] = 2
i=1 → arr[1][1] = 5
i=2 → arr[2][1] = 8
j = 2
i=0 → arr[0][2] = 3
i=1 → arr[1][2] = 6
i=2 → arr[2][2] = 9
1 2 3
↓ ↓ ↓
4 5 6
↓ ↓ ↓
7 8 9
✅ ده Column by Column
*/
// int ColSum(int arr[3][3], short Rows, short ColNumber)
// {
// int Sum = 0;
// for (short i = 0; i < Rows; i++)
// {
// Sum += arr[i][ColNumber];
// }
// return Sum;
// }
// void PrintEachColSum(int arr[3][3], short Rows, short Cols)
// {
// cout << "\nThe following are the sum of each Col in the matrix:\n";
// for (int j = 0; j < Rows; j++)
// {
// cout << " Col " << j + 1 << " Sum = " << ColSum(arr, Rows, j) << endl;
// }
// }
/*
Sum += arr[i][ColNumber];
i = 0 => arr[0][0] + arr[1][0] + arr[2][0] = result Sum col 1
i = 1 => arr[0][1] + arr[1][1] + arr[2][1] = result Sum col 2
i = 2 => arr[0][2] + arr[1][2] + arr[2][2] = result Sum col 3
*/
int main()
{
srand((unsigned)time(NULL));
int arr[3][3];
FillMatrixWithRandomNumbers(arr, 3, 3);
cout << "\nThe following is a 3x3 random matrix:\n";
PrintMatrix(arr, 3, 3);
SumEachColInMatrix(arr,3,3);
// PrintEachColSum(arr, 3,3);
system("pause>0");
}