-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
162 lines (139 loc) · 4.19 KB
/
Source.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
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
void showArr(int arr[][6], const int SIZE_ROW, const int SIZE_COL);
void fillArr(int arr[][6], const int SIZE_ROW, const int SIZE_COL);
void searchMax(int arr[][6], const int SIZE_ROW, const int SIZE_COL, int *maxNumber, int *maxIndexForRow, int *maxIndexForCol);
void searchMin(int arr[][6], const int SIZE_ROW, const int SIZE_COL, int *pminNumber, int* pminIndexForRow, int *pminIndexForCol);
int main()
{
setlocale(LC_ALL, "ru");
srand(time(0));
cout << "1.2. íàïèñàòü ïðîãðàììó, êîòîðàÿ íàõîäèò ìàêñèìàëüíûé è ìèíèìàëüíûé ýëåìåíòû\n"
" äâóìåðíîì ìàññèâå è ìåíÿåò ìåñòàìè ñòðîêè ñ ýòèìè ýëåìåíòàìè. \n"
"Åñëè ìàêñèìàëüíûé è ìèíèìàëüíûé íàõîäÿòñÿ â îäíîé ñòðîêå - âûâîäèòñÿ ñîîòâåòñòâóþùåå óâåäîìëåíèå. \n\n";
const int SIZE_ROW = 5;
const int SIZE_COL = 6;
int arr[SIZE_ROW][SIZE_COL];
const int SIZE_BUFER_COL = 6;
int buferArr[SIZE_BUFER_COL];
int maxIndexForCol = 0, minIndexForRow = 0, maxIndexForRow = 0, minIndexForCol = 0, maxNumber = 0, minNumber = 0;
int*pminIndexForCol = &minIndexForCol;
int*pminIndexForRow = &minIndexForRow;
int*pminNumber = &minNumber;
int* pmaxIndexForCol = &maxIndexForCol;
int* pmaxIndexForRow = &maxIndexForRow;
int* pmaxNumber = &maxNumber;
fillArr(arr, SIZE_ROW, SIZE_COL);
showArr(arr, SIZE_ROW, SIZE_COL);
searchMax(arr, SIZE_ROW, SIZE_COL, &maxNumber, &maxIndexForRow, &maxIndexForCol);
cout << " Indecs MAX = " << *pmaxIndexForRow << "|" << *pmaxIndexForCol << endl
<< " MAX = " << maxNumber << endl<<endl;
*pminNumber =arr[0][0];
searchMin(arr, SIZE_ROW, SIZE_COL, &minNumber,&minIndexForRow,&minIndexForCol);
cout << " Indecs MIN = " << minIndexForRow << "|" << minIndexForCol << endl
<< " MIN = " << minNumber << endl<<endl;
if (minIndexForRow != maxIndexForRow)
{
for (int i = 0; i < SIZE_ROW; i++)
{
if (i == minIndexForRow)
{
for (int j = 0; j < SIZE_COL; j++)
{
buferArr[j] = arr[i][j];
}
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl<< endl
<< "\t\t String with MAX element :" << endl;
cout << " Indecs string with MAX : " << *pmaxIndexForRow << endl;
}
}
for (int i = 0; i < SIZE_ROW; i++)
{
if (i == maxIndexForRow)
{
for (int j = 0; j < SIZE_COL; j++)
{
arr[minIndexForRow][j] = arr[i][j];
cout << "\t" << arr[minIndexForRow][j] ;
}
cout << "\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl
<< "\t\t String with MIN element :" << endl;
cout << " Indecs string with MIN :" << minIndexForRow << endl;
}
}
for (int i = 0; i < SIZE_ROW; i++)
{
if (i == minIndexForRow)
{
for (int j = 0; j < SIZE_COL; j++)
{
arr[maxIndexForRow][j] = buferArr[j];
cout << "\t" << arr[maxIndexForRow][j] ;
}
cout << "\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl
<< "Swap strings in arr :"<<endl;
}
}
showArr(arr, SIZE_ROW, SIZE_COL);
}
else
{
cout << "MAX and MIN in a one string!" << endl;
}
system("pause");
return 0;
}
void showArr(int arr[][6], const int SIZE_ROW, const int SIZE_COL)
{
for (int i = 0; i < SIZE_ROW; i++)
{
for (int j = 0; j < SIZE_COL; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl<< endl;
}
}
void fillArr(int arr[][6], const int SIZE_ROW, const int SIZE_COL)
{
for (int i = 0; i < SIZE_ROW; i++)
{
for (int j = 0; j < SIZE_COL; j++)
{
arr[i][j] = rand() % 50;
}
}
}
void searchMin(int arr[][6], const int SIZE_ROW, const int SIZE_COL, int *pminNumber, int *pminIndexForRow , int *pminIndexForCol)
{
for (int i = 0; i < SIZE_ROW; i++)
{
for (int j = 0; j < SIZE_COL; j++)
{
if (arr[i][j] < *pminNumber)
{
*pminNumber = arr[i][j];
*pminIndexForRow = i;
*pminIndexForCol = j;
}
}
}
}
void searchMax(int arr[][6], const int SIZE_ROW, const int SIZE_COL, int *pmaxNumber, int *pmaxIndexForRow, int *pmaxIndexForCol)
{
*pmaxNumber = arr[0][0];
for (int i = 0; i < SIZE_ROW; i++)
{
for (int j = 0; j < SIZE_COL; j++)
{
if (arr[i][j] >*pmaxNumber)
{
*pmaxNumber = arr[i][j];
*pmaxIndexForRow = i;
*pmaxIndexForCol = j;
}
}
}
}