-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress_test_template.cpp
66 lines (59 loc) · 1.87 KB
/
stress_test_template.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
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
int recursive_alg(vector<int> &a, int left, int right)
{
// write code here
}
int naive_alg(vector<int> &a, int left, int right)
{
// write code here
}
int main()
{
cout << "Enter a max array size: ";
int max_s;
cin >> max_s;
cout << "Enter a max number of distinct values: ";
int max_v;
cin >> max_v;
bool all_clear = true;
int size;
int vals;
srand(time(NULL));
while (all_clear) // generates a random array of size no greater than max_s
{ // and no more than max_v distinct values, and then applies both algorithms to the array
do
{
size = rand() % max_s + 1;
vals = rand() % max_v + 1;
} while (size == 0 || vals == 0); // can't have arrays of size 0 or with 0 distinct elements
vector<int> a(size);
int is_maj_elt = rand() % 2;
if (is_maj_elt) // then there should be a maj_elt
{
int is_maj_elt = rand() % vals;
for (int i = 0; i < size; i++)
{
is_maj_elt = rand() % 3; // nonzero 2 out of 3 times - should give a majority element
if(is_maj_elt) // and yes, this is a three-sided coin ;)
a[i] = maj;
else
a[i] = rand() % vals;
}
}
else // then there needs not be a maj_elt
for (int i = 0; i < size; i++)
a[i] = rand() % vals;
for (int i = 0; i < size; i++)
cout << a[i] << ' ';
cout << endl;
int naive = naive_alg(a, 0, size);
int recursive = recursive_alg(a, 0, size);
cout << naive << ' ' << recursive << endl;
all_clear = (naive == recursive);
}
cout << "Outputs differ" << endl;
}