Incorrect Percentage in Answers

Apr 27, 2015 at 6:36am
Develop a function Lottery that simulate a lottery drawing using balls numbered from 1 to 10. Assume that three balls are drawn at random. Allow the user to enter the number of lottery drawings to simulate. Calculate and print the percentage of the time the result contain three even numbers in the simulation. (hint: argument type: int, return data type: void)

Whenever I run the program, my percentages all return as zero and I can't tell what I'm doing wrong

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
  int ball_drawings(int&);
		{

			int draw, even(0), seven(0), one_two_three(0); // Declare Variables
			double percent_of_even, percent_of_seven, percent_123;
			int maxpos = 1;
			int ct = 1;


			// Input From User
			cout << "How many lottery drawings will be simulated? => ";
			cin >> draw;//number of lottery drawing
			cout << ' ' << endl;


			// Randomize numbers
			srand(draw);

			// Continue Drawing requested Amount

			while (ct <= draw)
			{

				for (int k = 1; k <= 3; k++)
				{
					cout << rand() % 10 + 1 << ' ';

					if (rand() % 10 + 1 == 7)
					{
						seven++;
					}
					else
					{
						if (rand() % 10 + 1 == 2 || rand() % 10 + 1 == 4 || rand() % 10 + 1 == 6 || rand() % 10 + 1 == 8 || rand() % 10 + 1 == 10)
						{
							even++;
						}
						else
						{
							if (rand() % 10 + 1 == 1 && rand() % 10 + 1 == 2 && rand() % 10 + 1 == 3)
							{
								one_two_three++;
							}
						}
					}
				}
				cout << ' ' << endl;

				maxpos = ct;

				ct++;
			}


			percent_of_even = (even / (draw * 3))*(100.0);
			percent_of_seven = (seven / (draw * 3))*(100.0);
			percent_123 = (one_two_three / (draw * 3))*(100.0);


			// Output to Screen
			cout << ' ' << endl;
			cout << "The three balls were even " << percent_of_even << "% of the time." << endl;
			cout << "The number seven appeared on one of the three balls " << percent_of_seven << "% of the time." << endl;
			cout << "The numbers 1, 2, and 3 occured on the balls " << percent_123 << "% of the time." << endl;
			cout << ' ' << endl;
Apr 27, 2015 at 7:22am
You're doing integer division:

percent_of_even = (even / (draw * 3.0)) * 100.0;

Introduce the presence of floating point numbers a little earlier in the calculation.
Last edited on Apr 27, 2015 at 7:22am
Apr 27, 2015 at 5:45pm
Thanks, that worked.
Last edited on Apr 27, 2015 at 6:22pm
Topic archived. No new replies allowed.