Possible Rounding Issues

I should be getting these results from my program. I'm not. I'm thinking I'm having rounding issues. The Italicized portion is the output, the normal font is the input.

please enter an integer seed for the random number generator: 127
please enter the lower and upper bounds for the random number: 5 25
random number = 12
please enter the number of decimal places you would like: 3
please enter the number of random numbers to generate: 10000
midpoint = 15.000
percent less than midpoint = 48
percent equal midpoint = 5
percent greater than midpoint = 47
average of 10000 numbers between 5 and 25 is 14.938


And this is my code so far.

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;
}


This is the outcome of my code:

please enter an integer seed for the random number generator: 127
please enter the lower and upper bounds for the random number: 5 25
random number = 12
please enter the number of decimal places you would like: 3
please enter the number of random numbers to generate: 10000
midpoint = 18
percent less than midpoint = 0
percent equal midpoint = 0
percent greater than midpoint = 0
average of 10000 numbers between 5 and 25 is 14.5591



All help is greatly appreciated.
It is the integer math.
1
2
3
4
5
6
// You need these calculations done as floating point number
// that why you multiply by 100.0 on the denominator which 
// causes the result to be floating point
cout << "Percent less than midpoint = " << (100.0*below)/num <<endl;
cout << "Percent equal to midpoint = " << (100.0*at)/num << endl;
cout << "Percent greater than midpoint = " << (100.0*above)/num << endl;

Also, not sure why you are doing avg+=rn;. I think you want another var to keep the running total in the loop such that you can average that at the end of the loop. If I do that I get.
$ ./a.out
Please insert an integer: 127
Please insert a lower and upper bound: 5 25
14
Please enter the number of decimals: 3
Please insert the number of random numbers: 10000
Percent less than midpoint = 50.76
Percent equal to midpoint = 4.98
Percent greater than midpoint = 44.26
Midpoint = 15
Average = 15

ive had a second look to that avg+rn and thats wrong.
you get 10.000 numbers. if you want to get average you should add all random numbers and divide it by the amount of numbers.
what you are doing is : (all numbers + 15) /10000
so erase line 37 to prevent this
or make another variable if you still want to use avg in line 41-43
that does also fix the problem that you keep using the avg variable (the 9999th time avg isnt 15 but like 14500)

heres the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
float avg(int num, int LowerBound, int UpperBound)
{
	int a, above=0, below=0, at=0, rn;
	double avg=0,mid;
	mid = ((UpperBound-LowerBound)/2)+LowerBound;
	for(a=0;a<num;a++)
	{
		rn = generateRandom(LowerBound, UpperBound);
		if(rn == mid) { at=at+1;}
		if(rn < mid) {below=below+1;}
		if(rn > mid) {above=above+1;}
		avg+=rn;
	}
	double percent_at,percent_above,percent_below;
	percent_below=(below/(num/100.00));
	percent_at=(at/(num/100.00));
	percent_above=(above/(num/100.00));
	cout << "Percent less than midpoint = " << percent_below << "%"<<endl;
	cout << "Percent equal to midpoint = " << percent_at <<"%"<< endl;
	cout << "Percent greater than midpoint = " << percent_above << "%"<<endl;
	avg=avg/num;
	cout << "Midpoint = " << ceil(avg) << endl;
	return avg;
}

this one works good.
Last edited on
Thanks!
Topic archived. No new replies allowed.