-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathST.cpp
More file actions
88 lines (73 loc) · 1.96 KB
/
ST.cpp
File metadata and controls
88 lines (73 loc) · 1.96 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
#include "ST.h"
int compareElements(const void * _a, const void * _b)
{
if (*(int *)_a > *(int *)_b)
return 1;
else if (*(int *)_a < *(int *)_b)
return -1;
else
return 0;
}
runsTest::runsTest(Generator * _generator, int _sampleCount):Test(_generator, _sampleCount), setA(0), setB(0), runs(0), median(-1)
{
}
runsTest::~runsTest()
{
}
int runsTest::findMedian()
{
int tempArray[sampleCount];
for (int i = 0; i < sampleCount; i++)
tempArray[i] = samples[i];
qsort(tempArray, sampleCount, sizeof(int), compareElements);
median = tempArray[(sampleCount + 1) / 2];
return median;
}
void runsTest::countRuns()
{
bool overMedian = false;
setA = setB = runs = 0;
for (int i = 0; i < sampleCount; i++)
{
if ((samples[i] > median) && overMedian)
{
setA++;
}
else if ((samples[i] < median) && !overMedian)
{
setB++;
}
else if ((samples[i] > median) && !overMedian)
{
setA++;
runs++;
overMedian = true;
}
else if ((samples[i] < median) && overMedian)
{
setB++;
runs++;
overMedian = false;
}
}
}
void runsTest::displayResult(ostream & output)
{
output << "Runs test!" << endl << "Sample: " << endl;
for (int i = 0; i < sampleCount; i++)
{
output << samples[i] << "\t";
if ((i % 10 == 0) && (i > 0))
{
output << endl;
}
}
output << endl << "Median:\t" << median << endl << "Set A:\t" << setA << endl << "Set B:\t" << setB << endl << "Runs:\t" << runs << endl << "Check critical value table for k1, k2." << endl << "If k1 < " << runs << " or " << runs << " > k2 - sequence is random." << endl << endl;
}
void runsTest::runTest(ostream & output)
{
drawSamples();
findMedian();
countRuns();
displayResult(output);
}