Exercise 7 Imagine a tug of war contest

  • Slides: 2
Download presentation
Exercise 7 Imagine a tug of war contest between two teams, team “A” and

Exercise 7 Imagine a tug of war contest between two teams, team “A” and team “B”. The two teams pull on the rope until one of them has moved the marker the distance required to win. rope marker Team A Team B distance to win Every second the rope moves one unit of distance one way or the other. The probability that the rope will move in team A’s direction in a given second is equal to initial_probability + (t * probability_increment) where “init_probability” is the probability that the rope will move in team A’s direction in the first second, “t” is the amount of time that has passed (in seconds), and “probability_increment” is a factor which reflects the endurance of the two teams. A positive factor means that team A gets stronger with time, while a negative factor means the opposite. You are to write a program which, after reading in all of the necessary information (number of tugs to be simulated, initial probability, probability increment, and the distance to win) outputs both team A’s chances of winning and the expected length of the tug of war (in seconds). Running the sample program should give the idea. As with the sample program, your program should loop, allowing multiple scenarios to be investigated. 1 91. 166 Copyright © 2002, Department of Systems and Computer Engineering, Carleton University

As well as giving you a chance to play with simulation, this assignment is

As well as giving you a chance to play with simulation, this assignment is intended to give you an opportunity to explore handling input errors. Like the sample program, your program should survive (and behave reasonably) no matter what is typed in. You may find that hole. cpp (see the sample programs directory on the web site) serves a useful model. Input checking must include making sure that the initial probability is between 0 and 1, that the absolute value of the probability increment is not greater than 0. 01, that the number of trials is positive, and the distance to win is greater than 0. Notes: 1/. No matter how long the tug of war goes on for, the probability of the rope moving in team A’s direction can never exceed 1 or be less than 0. Keep this in mind when applying the formula. 2/. Suppose that the probability of some event happening is X (0 <= X <= 1). If we are running a simulation, and want to determine whether this event has occurred, we need only pick a random value between 0 and 1 (0 <= value < 1). If the random value is less than X, the event has occurred. Otherwise it hasn’t. Imagine, for example, that X is 0. 25 (the event has a one in four chance of happening). If we pick a large number of random values, we will get values between 0 and 0. 2499999 one quarter of the time, and values in between 0. 25 and 0. 99999 three quarters of the time. The event will therefore happen (on average) every fourth “roll of the dice”, which is correct. 2 91. 166 Copyright © 2002, Department of Systems and Computer Engineering, Carleton University