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
|
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
void getParameters(int& seed, int& n, int& lower, int& upper);
// get the value for seed, number of random integers, lower and upper bound for those random integers.
int getNum(int lower, int upper);
// Generate a random integer between the lower and upper bound and return the integer.
void update(int num, double& sum1, double& sum2);
// update sum of values and sum of value squared
void output(double avg, double std);
//Output the two numbers.
int average(int n, int sum);
//Calculates the average of a sum of n integers
void average(int n, double sum1, double& avg);
//Calculates the average of the random values
void standardDeviation(double avg, int n, double sum1, double sum2, double& std);
//Calculates the standard deviation
void resetAll(double& avg, double& sum1,double& sum2, double& std, int& count, int& lower, int& upper, int& n, int& seed);
//Resets all variables so the program may be run again
int seed;
int main(){
cout.setf(ios::fixed | ios::showpoint);
cout.precision(2);
int count = 0, lower, upper, n;
double avg, sum1, sum2, std;
char repeat = 'y';
do {
srand(seed);
getParameters(seed, n, lower, upper);
cout << "The numbers generated are: " << "\n";
do {
update(getNum(lower, upper), sum1, sum2);
}
while(++count < n);
average(n, sum1, avg);
standardDeviation(avg, n, sum1, sum2, std);
output(avg, std);
resetAll(avg, sum1, sum2, std, count, lower ,upper ,n, seed);
cout << "\n" << "Would you like to continue? (y or n): ";
cin >> repeat; }
while(repeat == 'y' || repeat == 'Y');
}
void getParameters(int& seed, int& n, int& lower, int& upper) {
cout << "\n" << "Seed for the random number generator: ";
cin >> seed;
cout << "\n" << "The size of the sequence: ";
cin >> n;
cout << "\n" << "Lower and upper bound for the random numers: ";
cin >> lower;
cout << " ";
cin >> upper;}
int getNum(int lower, int upper){
int value = ((rand() % (lower - upper + 1)) + lower);
cout << value << " ";
return value; }
void update(int num, double& sum1, double& sum2) {
sum1 = num + sum1;
sum2 = (num * num) + sum2; }
void average(int n, double sum1, double& avg) {
avg = (sum1 / n); }
void standardDeviation(double avg, int n, double sum1, double sum2, double& std) {
std = sqrt((sum2 -(2*avg*sum1) + (n*avg*avg))/n); }
void output(double avg, double std) {
cout << "\n" << "Average = " << avg
<< " Standard deviation = " << std; }
void resetAll(double& avg, double& sum1,double& sum2, double& std, int& count, int& lower, int& upper, int& n, int& seed) {
count = 0, lower = 0, upper = 0, n = 0;
avg = 0, sum1 = 0, sum2 = 0, std = 0, seed = 0;}
|