-
Notifications
You must be signed in to change notification settings - Fork 0
/
DZ.cpp
187 lines (177 loc) · 5.01 KB
/
DZ.cpp
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
#include <iostream>
#include <Windows.h>
using namespace std;
const int N = 7, M = 3;
void fillMatrix(int array[N][M], int a, int b);
void printMatrix(int array[N][M]);
void findNegative(int array[N][M]);
int matrixSum(int array[N][M]);
void countEvenNumbers(int array[N][M]);
void maxElement(int array[N][M]);
void minElement(int array[N][M]);
void masSum(int array[N][M], int mas[N]);
int main()
{
int mas[N][M];
int arr[N];
srand(8);
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
while (true)
{
int num;
cout << "\t\t\t\tMenu:" << endl << endl;
cout << "\t1 - ²í³ö³àë³çàö³ÿ äâîâèì³ðíîãî ìàñèâó." << endl;
cout << "\t2 - Çàì³íèòè âñ³ â³ä’ºìí³ åëåìåíòè ìàòðèö³ íà 0." << endl;
cout << "\t3 - Çíàéòè ñóìó âñ³õ åëåìåíò³â ìàòðèö³." << endl;
cout << "\t4 - ϳäðàõóâàòè ê³ëüê³ñòü ïàðíèõ ÷èñåë â êîæíîìó ñòîâïö³." << endl;
cout << "\t5 - Âèâåñòè íàéá³ëüøèé åëåìåíò ìàòðèö³ òà éîãî ³íäåêñè." << endl;
cout << "\t6 - Âèâåñòè íàéìåíøèé åëåìåíò ìàòðèö³ òà éîãî ³íäåêñè." << endl;
cout << "\t7 - Óòâîðèòè îäíîâèì³ðíèé ìàñèâ ç ñóì åëåìåíò³â êîæíîãî ðÿäêà." << endl;
cout << "\t0 - Âèõ³ä." << endl;
cout << endl << "\tÂâåä³òü ñâ³é âèá³ð: ";
cin >> num;
system("cls");
switch (num)
{
case 1:
fillMatrix(mas, -10, 10);
printMatrix(mas);
system("pause");
break;
case 2:
cout << "Ïî÷àòêîâà ìàòðèöÿ: " << endl;
printMatrix(mas);
cout << endl << "Ìàòðèöÿ áåç â³ä'ºìíèõ åëåìåíò³â: " << endl;
findNegative(mas);
printMatrix(mas);
fillMatrix(mas, -10, 10);
system("pause");
break;
case 3:
printMatrix(mas);
cout << endl << endl << "Ñóìà åëåìåíò³â ìàòðèö³ äîð³âíþº: " << matrixSum(mas) << endl;
system("pause");
break;
case 4:
printMatrix(mas);
countEvenNumbers(mas);
system("pause");
break;
case 5:
printMatrix(mas);
maxElement(mas);
system("pause");
break;
case 6:
printMatrix(mas);
minElement(mas);
system("pause");
break;
case 7:
printMatrix(mas);
masSum(mas, arr);
system("pause");
break;
case 0:
exit(0);
break;
default:cout << "Error" << endl;
system("pause");
break;
}
system("cls");
}
}
void fillMatrix(int array[N][M], int a, int b) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
array[i][j] = a + rand() % (b - a + 1);
}
}
}
void printMatrix(int array[N][M]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cout << array[i][j] << "\t";
}
cout << endl;
}
}
void findNegative(int array[N][M]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (array[i][j] < 0)
array[i][j] = 0;
}
}
}
int matrixSum(int array[N][M]) {
int Sum = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
Sum += array[i][j];
}
}
return Sum;
}
void countEvenNumbers(int array[N][M]) {
int count = 0;
for (int j = 0; j < M; j++) { // çîâí³øí³é öèêë ïî ñòîâïöÿì
for (int i = 0; i < N; i++) { // âíóòð³øí³é öèêë ïî ðÿäêàì
if (array[i][j] % 2 == 0)
{
count++;
}
}
cout << "ʳëüê³ñòü ïàðíèõ ÷èñåë â " << j + 1 << " còîâïö³: " << count << endl;
count = 0; // ë³÷èëüíèê ïàðíèõ åëåìåíò³â â ñòîâïö³
}
}
void maxElement(int array[N][M]) {
int max, max_i, max_j;
max = array[0][0]; // ïðèéìåìî çà íàéá³ëüøèé ïåðøèé åëåìåíò ìàñèâó
max_i = 0; // ³íäåêñè ìàêñèìàëüíîãî åëåìåíòó
max_j = 0;
for (int i = 0; i < N; i++) { // çîâí³øí³é öèêë ïî ðÿäêàì
for (int j = 0; j < M; j++) { // âíóòð³øí³é öèêë ïî ñòîâïöÿì
if (array[i][j] > max) { // ÿêùî åëåìåíò á³ëüøå ìàêñèìóìó, òî
max = array[i][j]; // â³í ñòຠíàéá³ëüøèì
max_i = i; // çàïàì'ÿòîâóºìî éîãî ³íäåêñè
max_j = j;
}
}
}
cout << " Ìàêñèìàëüíèé åëåìåíò: " << max << endl;
cout << " ³í çíàõîäèòüñÿ ó " << max_i + 1 << " ðÿäêó òà " << max_j + 1 << " ñòîâïö³" << endl;
}
void minElement(int array[N][M]) {
int min, min_i, min_j;
for (int i = 0; i < N; i++) { // çîâí³øí³é öèêë ïî ðÿäêàì
min = array[i][0]; // ïðèéìåìî çà íàéá³ëüøèé ïåðøèé åëåìåíò i-ãî ðÿäêó
min_i = i; // ³íäåêñè ì³í³ìàëüíîãî åëåìåíòó
min_j = 0;
for (int j = 1; j < M; j++) { // âíóòð³øí³é öèêë ïî ñòîâïöÿì
if (array[i][j] < min) { // ÿêùî åëåìåíò ìåíøå ì³í³ìàëüíîãî, òî
min = array[i][j]; // â³í ñòຠì³í³ìàëüíèì
min_i = i; // çàïàì'ÿòîâóºìî éîãî ³íäåêñè
min_j = j;
}
} // âèâîäèìî ì³í³ìàëüíèé åëåìåíò ðÿäêà òà éîãî ³íäåêñ:
cout << " ̳í³ìàëüíèé åëåìåíò " << i + 1 << " ðÿäêà äîð³âíþº " << min << endl;
cout << " ³í çíàõîäèòüñÿ ó " << min_j + 1 << " ñòîâïö³\n\n";
}
}
void masSum(int array[N][M], int mas[N]) {
int Sum;
cout << "Ìàñèâ ç ñóì ðÿäê³â: ";
for (int i = 0; i < N; i++) { // çîâí³øí³é öèêë ïî ðÿäêàì
Sum = 0;
for (int j = 0; j < M; j++) { // âíóòð³øí³é öèêë ïî ñòîâïöÿì
Sum += array[i][j]; // øóêàºìî ñóìó åëåìåíò³â ðÿäêà
}
mas[i] = Sum; // äîäàºìî â îäíîâèì³ðíèé ìàñèâ ñóìó â³äïîâ³äíîãî ðÿäêà
cout << mas[i] << " ";
}
cout << endl;
}