-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
204 lines (169 loc) · 3.92 KB
/
main.cpp
File metadata and controls
204 lines (169 loc) · 3.92 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "stdafx.h"
#include <iostream>
#include <random>
#include <time.h>
#include <Windows.h>
using namespace std;
void randomize_array(int *array, int n)
{
srand(time(NULL) + rand() % 50);
for (int i = 0; i < n; i++)
{
array[i] = rand() % 2000 - 1000;
}
cout << "The array was filled with random numbers." << endl;
}
void show_array(int *array, int n)
{
for (int i = 0; i < n; i++)
{
cout << "array[" << i << "]" << ":\t" << array[i] << endl;
}
}
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int* copy_array(int *array, int n)
{
int *new_array = new int[n];
for (int i = 0; i < n; i++)
{
new_array[i] = array[i];
}
return new_array;
}
int* create_array(int &n)
{
cout << "Enter the number of elements in an integer array: ";
cin >> n;
while(n <= 0)
{
cout << "The number of array elements can not be less than one! Please repeat: ";
cin >> n;
}
int* array = new int[n];
for (int i = 0; i < n; i++) array[i] = 0;
cout << "Empty array on " << n << " cells created." << endl;
return array;
}
void show_menu()
{
printf("Select an action:\n\
1) Create an array.\n\
2) Output the contents of the array to the console.\n\
3) Fill the array with random values.\n\
4) Fill the all array with values from the keyboard.\n\
5) Replace one value in the array with one entered from the keyboard.\n\
6) Find the three least positive elements of an array.\n\
7) Exit.\n");
}
int main(int argc, char **argv)
{
//SetConsoleCP(65001); //Coding for Russian output in the console
//SetConsoleOutputCP(65001);
int* array = nullptr;
int* array_copy = nullptr;
int n;
int choice;
bool done = false;
do {
show_menu();
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
if (array != nullptr)
{
delete[] array;
array = nullptr;
}
array = create_array(n);
break;
case 2:
if (array != nullptr)
show_array(array, n);
else
cout << "First you need to create an array!" << endl;
break;
case 3:
if (array != nullptr)
randomize_array(array, n);
else
cout << "First you need to create an array!" << endl;
break;
case 4:
if (array != nullptr)
{
for (int i = 0; i < n; i++)
{
cout << "array[" << i << "]" << " will be: ";
cin >> array[i];
}
cout << "The array was completely filled." << endl;
}
else
cout << "First you need to create an array!" << endl;
break;
case 5:
int j,k;
if (array != nullptr)
{
cout << "Enter the number of the element you want to change and what for: ";
cin >> j >> k;
while (j < 0 || j >= n)
{
cout << "There is no such element of the array. Please repeat: ";
cin >> j >> k;
}
array[j] = k;
printf("%i an array element was assigned a value %i", j,k);
}
else
cout << "First you need to create an array!" << endl;
break;
case 6:
if (array != nullptr)
{
array_copy = copy_array(array, n);
int a, b, c;
for (int i = 0; i < 3; i++)
{
for (int j = n-1; j > 0; j--)
{
if ((array_copy[j] > 0) && ((array_copy[j] <= array_copy[j-1]) ||
(array_copy[j] > array_copy[j-1] && array_copy[j-1] <= 0)))
{
swap(array_copy[j], array_copy[j-1]);
}
}
}
if (array_copy[2] > 0)
{
for (int i = 0; i < n; i++)
{
if (array[i] == array_copy[0]) a = i;
if (array[i] == array_copy[1]) b = i;
if (array[i] == array_copy[2]) c = i;
}
printf("array[%i]: %i\narray[%i]: %i\narray[%i]: %i\n\n", a, array_copy[0], b, array_copy[1], c, array_copy[2]);
}
else
{
cout << "There are not enough positive elements in the array." << endl;
}
delete[] array_copy;
}
else
cout << "First you need to create an array!" << endl;
break;
case 7:
done = true;
break;
}
} while(!done);
return 0;
}