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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int generateRandom(int LowerBound, int UpperBound)
{
double i;
i = 0;
i = rand()%(UpperBound-LowerBound);
i += LowerBound;
return i;
}
void setPrecision(int numDecimalPlaces)
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(numDecimalPlaces);
}
int computePercent(int numberRolled, int NumTimesRolled)
{
static_cast<double>(numberRolled);
double percent;
percent = numberRolled/NumTimesRolled;
static_cast<int>(percent);
return percent;
}
float avg(int num, int LowerBound, int UpperBound)
{
int a, above, below, at, rn;
at=0;
below =0;
above = 0;
double avg;
avg =0;
avg = ((UpperBound-LowerBound)/2)+LowerBound;
for(a=0;a<num;a++)
{
rn = generateRandom(LowerBound, UpperBound);
if(rn == avg) at++;
if(rn < avg) below++;
if(rn > avg) above++;
avg+=rn;
}
cout << "Percent less than midpoint = " << below/num*100 <<endl;
cout << "Percent equal to midpoint = " << at/num*100 << endl;
cout << "Percent greater than midpoint = " << above/num*100 << endl;
cout << "Midpoint = " << ceil(avg) << endl;
avg=avg/num;
return avg;
}
int main ()
{
int x, LowerBound, UpperBound, numDecimalPlaces, num;
double SomeRandomNumber,average;
cout << "Please insert an integer: ";
cin >> x;
cout << "Please insert a lower and upper bound: ";
cin >> LowerBound;
cin >> UpperBound;
srand(x);
SomeRandomNumber = generateRandom(LowerBound, UpperBound);
cout << SomeRandomNumber << endl;
cout << "Please enter the number of decimals: ";
cin >> numDecimalPlaces;
cout << "Please insert the number of random numbers: ";
cin >>num;
average =avg(num,LowerBound,UpperBound);
cout << "Average = " << average << endl;
return 0;
}
|