include iostream include ctime using namespace std int getRandomArray

 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
#include <iostream>
#include <ctime>
using namespace std;
int* getRandomArray(int size);
int hasFiveOrdered(int* array, int size);
int main() {
int n = 0;
while (n < 1) {
cout << "Size : ";
cin >> n;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Wrong size" << endl;
}
system("cls");
int* random_array = getRandomArray(n);
cout << "Array has " << (hasFiveOrdered(random_array, n) ? "" : "no ") << "row of five ordered elements." << endl;
system("pause");
}
int* getRandomArray(int size) {
int* random_array = new int[size];
srand((unsigned int)time(0));
for (int i = 0; i < size; i++) {
random_array[i] = rand();
}
return random_array;
}
enum Order {ascending, descending};
int hasFiveOrdered(int* array, int size) {
int previous_element = array[0];
int ordered_count = 1;
Order order = (previous_element >= array[1]) ? ascending : descending;
for (int i = 1; i < size && ordered_count < 5; i++) {
if (order == ascending)
{
if (previous_element >= array[i]) {
ordered_count++;
previous_element = array[i];
}
else {
ordered_count = 1;
order = descending;
}
}
else if (order == descending)
{
if (previous_element <= array[i]) {
ordered_count++;
previous_element = array[i];
}
else {
ordered_count = 1;
order = ascending;
}
}
}
return (ordered_count == 5);
}