-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThomas5.cpp
77 lines (68 loc) · 1.93 KB
/
Thomas5.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
// Assignment 5 - COSC 1410
// TA: Can Cao
// Author: Micah Thomas
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
bool repeat();
void getInput(int &seed, int &size, int &lower, int &upper);
void getStats(double &mean, double &std, int size, int lower, int upper);
int getNum(int lower, int upper);
void getZ(double mean, double std);
int main() {
int seed, size, lower, upper;
double mean, std;
getInput(seed, size, lower, upper);
srand(seed);
getStats(mean, std, size, lower, upper);
cout << fixed << setprecision(5);
cout << "\nMean: " << mean << " Std-deviation: " << std << endl;
while (repeat()) {
getZ(mean, std);
}
}
void getInput(int &seed, int &size, int &lower, int &upper) {
cout << "=======================================================\n";
cout << "Seed for the random number generator: ";
cin >> seed;
cout << "Size of the sequence: ";
cin >> size;
cout << "Lower and upper bound for the random number: ";
cin >> lower >> upper;
cout << "=======================================================\n";
}
void getStats(double &mean, double &std, int size, int lower, int upper) {
double sum = 0, square_sum = 0;
cout << "\nNumbers Generated:\n";
for (int i = 0, current; i < size; i++) {
current = getNum(lower, upper);
cout << current << " ";
sum += current;
square_sum += pow(current, 2);
}
mean = sum / size;
std = (square_sum - 2 * mean * sum + pow(mean, 2) * size) / size;
std = sqrt(std);
}
int getNum(int lower, int upper) {
return (rand() % upper + lower);
}
void getZ(double mean, double std) {
double x, z;
cout << "Enter the value to calculate the z-score: ";
cin >> x;
z = (x - mean) / std;
cout << "Z-Score: for " << x << " is: " << z;
}
bool repeat() {
char ans;
cout << "\nDo you want to continue? (Y/N): ";
cin >> ans;
cout << "\n";
if (tolower(ans) == 'n')
return false;
else
return true;
}