Spare a Square Problem. Need help to figure it out.

1) Use a function that generates and returns a random number between [0,1]
which can be used to determine whether a patron is a big-chooser or littlechooser.
double rand_uniform();
Inside this function use the rand() function and the RAND_MAX constant to
generate random numbers and scale them appropriately.

2)Use a function that performs a single simulation of two-rolls being used until
one becomes empty. It should take the initial value, N, and the probability of
big-choosers, p, as input arguments, perform the simulation and then return
the number of squares left on the non-empty roll once the other roll is
empty.
int single_roll_sim(int N, double p);

3) Write code to call single_roll_sim() 100,000 times and calculate the
average of the remaining squares.

4) Display the result of your program using the exact syntax:
Average number of squares left for N=N, p=p is avg.

Can you help me to figure out how to do steps 2 and 3. I am not sure what i am doing wrong. When I run it, it kicks out on lines 48 and 49.
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*********************************************************
 * File name : tp.cpp
 * Purpose   : Simulates the toilet-paper problem
 * Notes     : None
 *********************************************************/

#include <iostream>   // Basic I/O => cin, cout, etc.
#include <cstdlib>    // Other helpful functions => rand(), RANDMAX
#include <ctime>      // for srand function

using namespace std;

// Prototype/declaration of a function that returns a
// uniform random number between [0,1]
double rand_uniform();

// Prototype/declaration of a function that will perform a single
// simulation of two rolls of paper and users
// Returns the number of squares left on the non-empty roll
int single_roll_sim(int N, double p);

int main(int argc, char *argv[])
{
  srand(time(0));                // different seeds 
  const int num_sims = 1000000;  // number of trials for our simulation

  int N;                // initial number of squares on each roll
  int Avg; 

  double p;             // probability of a big-chooser
  double r;             // randomly generated number from 0 to 1

  int total_squares;    // for each trial we'll keeping a running sum
                        // of the squares left on the non-empty roll

   cout << "Please, enter the initial number of squares on each roll, N: ";
   cin >> N;

   cout << "Please, enter the probability of a big-chooser, p: ";
   cin >> p;

  // Do some error checking to make sure the user
  // entered the correct number of arguments to the program
  if(argc < 3){
    cout << "Usage: " << argv[0] << " N p" << endl;
    cout << "\tN = number of squares on each roll to start" << endl;
    cout << "\tp = probability of a big-chooser" << endl;
    return 1;
  }
  
  N = atoi(argv[1]);   // convert the argument to an integer
  p = atof(argv[2]);   // convert the argument to a floating point

  // Add your code here
  //int[] rolls = int[2];

     for ( int i = 0; i < num_sims; i++)
     {
        total_squares += single_roll_sim(N, p);
     }
     Avg = total_squares / num_sims;

  cout << "Int. # of Squares " << "Prob. of Big Chooser " << "Result" << endl;
  cout << N << " " << p << " " << Avg << endl;

  return 0;
}

// return the number of squares on the non-empty roll
int single_roll_sim(int N, double p)
{ 
 int roll1 []= {N};
 int roll2 [] = {N};
 double r;	

   while ( roll1[0]!=0 || roll1[0]!=0 )
   { 
      r = rand_uniform();

          if ( r<= p){
               roll1[0]=roll1[0]-1;
	  }
          else{
               roll1[0]=roll1[0]-1;
	  }

   }

	if (roll1[0] != 0){
		return roll1[0];
	}
	else{
		return roll2[0];
	}
}

// returns a uniformly-distributed number in the range [0,1]
double rand_uniform()
{
  double r = rand() * 1.0/RAND_MAX; //Random number between 0 and 1
  return r;
}
Topic archived. No new replies allowed.