Noob Help

closed account (2z7XjE8b)
thanks
Last edited on
Try this code -maybe you can finish it:
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
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

int main() 
{
  int anum, bnum, play, i, num_equal = 0, num_first_smaller = 0, num_first_greater = 0;

  srand((unsigned)time(0));

  while(true)
  {
    cout << "Enter the number of times to play (-1 to quit) : " ;
    cin >> play ;
    if (play == -1)
      break;

    for (i = 1; i <= play; i++)
    {
      anum = 1 + rand() % 100;
      bnum = 1 + rand() % 100;

      if (anum == bnum)
        num_equal++;
      else if (anum < bnum)
        num_first_smaller++;
      else
        num_first_greater++;
    }

    //TO DO
    // calc the percentages and display the result 
  }
  system("pause");
  return EXIT_SUCCESS;
}
closed account (2z7XjE8b)
thx
Last edited on
It's because of integer division -

1
2
3
percentMore = float(num_first_greater/play*100);
percentLess = float(num_first_smaller/play*100);
percentEqual = float(num_equal/play*100);


simply change play to a double -

1
2
int anum, bnum,  i, num_equal = 0, num_first_smaller = 0, num_first_greater = 0;
double play;

Topic archived. No new replies allowed.