-- Template for COMP1100 2002 Assignment 2 -- 22-03-2002 AVP -- This module is submitted as part of the assessment for -- COMP1100. -- Its completion is substantially my own work. -- (signed) Joseph Curtis 3952239 -- wed01-03.a -- class DICE_STATS -- This is the main program class. It is a program to roll three dice -- a number of times and then print the various statistics out to -- the screen. creation make feature -- the three (distinguishable) dice greendie, blackdie, whitedie : ARRAY[INTEGER] -- comparison of green and black dice green_gt_black : INTEGER green_eq_black : INTEGER green_lt_black : INTEGER count : INTEGER -- number of throws in the experiment max_count : INTEGER is 200 three_dice : ARRAY[INTEGER] -- value of three-dice throw frequency : ARRAY[INTEGER] -- distribution of three-dice values triples : INTEGER -- number of triples thrown all_different : INTEGER -- number of throws with all dice different high_freq : INTEGER make is -- make creates the data and carries out the three-dice -- experiment simulation. -- It counts the number of triples that occurred and the number of -- times the three dice are different. -- The values on the green and black dice are compared. -- The number of occurrances of each possible value of a -- three-dice throw is determined and reported. local do create_data compute_three_dice report_triples report_three_different compare_green_black compute_frequency frequency_graph end --make create_data is -- This procedure generates the dice rolls using a random seed -- received from the user and then prints them out to the -- screen in a table. local i : INTEGER val : INTEGER rand : STD_RAND seed : INTEGER do get_valid_count -- Call to the get_valid_count routine which gets -- the number of dice rolls performed. !!greendie.make(1, count) !!blackdie.make(1, count) !!whitedie.make(1, count) -- Receives the random seed from the user io.put_string("Enter random seed (positive integer) : ") io.read_integer; seed := io.last_integer -- Seeds the random number generator with the integer received !!rand.with_seed(seed) from i := 1 until i > count loop rand.next; val := rand.last_integer(6) greendie.put(val, i) rand.next; val := rand.last_integer(6) blackdie.put(val, i) rand.next; val := rand.last_integer(6) whitedie.put(val, i) i := i + 1 end -- loop -- Writes the table of dice rolls to the screen io.put_new_line io.put_string("Dice Data :"); io.put_new_line; io.put_new_line io.put_string(" I Green Black White"); io.put_new_line io.put_string("------------------------"); io.put_new_line from i := 1 until i > count loop io.put_integer_format(i, 4); io.put_character(':') io.put_integer_format(greendie.item(i), 6) io.put_integer_format(blackdie.item(i), 6) io.put_integer_format(whitedie.item(i), 6) io.put_new_line i := i + 1 end -- loop io.put_new_line end -- create_data get_valid_count is -- Receives the number of dice rolls to perform from the user -- and makes sure it is a valid number of rolls. local do io.put_string("Enter number of throws of dice (1-") io.put_integer(max_count); io.put_string(") : ") io.read_integer count := io.last_integer -- io.newline -- ignore this ensure valid_count: count > 0 and count <= max_count end -- get_valid_count print_array(a : ARRAY[INTEGER]) is -- This procedure may be used to print the contents of an -- integer array. For each item in the array, the index and -- the value stored is printed out. To print the contents of -- an array my_array, the procedure call is print_array(my_array). local i : INTEGER do from i := a.lower until i > a.upper loop io.put_integer_format(i, 4); io.put_character(':') io.put_integer_format(a.item(i), 6) io.put_new_line i := i + 1 end -- loop end -- print_array compute_three_dice is -- This procedure computes the sum of the three dice -- rolls and stores it into an array. This array is -- then printed out to the screen using the print_array -- procedure. local i : INTEGER s : INTEGER do !!three_dice.make(1, count) io.put_new_line io.put_string(" I Value") ; io.put_new_line io.put_string("-------------") ; io.put_new_line from i := 1 s :=0 until i > count loop s := greendie.item(i) + blackdie.item(i) + whitedie.item(i) three_dice.put(s, i) i := i + 1 end print_array(three_dice) io.put_new_line end -- compute_three_dice report_triples is -- Compares the values for the three dice to check if they are -- all the same, if so it increments the triple count. The -- number of triples is then displayed to the screen. local i : INTEGER do io.put_string("Triples came up ") from i :=1 until i > count loop if greendie.item(i) = blackdie.item(i) and blackdie.item(i) = whitedie.item(i) then triples := triples + 1 end -- if i := i + 1 end -- loop io.put_integer(triples) io.put_string(" times.") io.put_new_line;io.put_new_line end -- report_triples report_three_different is -- This procedure checks the values of the three die and if -- they are all different it increments the three_different -- count. This number is then printed out to the screen. local i : INTEGER do io.put_string("The dice were all different ") from i := 1 until i > count loop if greendie.item(i) /= blackdie.item(i) and blackdie.item(i) /= whitedie.item(i) and greendie.item(i) /= whitedie.item(i) then all_different := all_different + 1 end i := i + 1 end io.put_integer(all_different) io.put_string(" times.") io.put_new_line;io.put_new_line end -- report_three_different compare_green_black is -- This procedure compares the green and black dice for each -- roll and reports firstly the number of times that green was -- greater than black. Then the number of times that the green -- die was equal to black and finally the number of times that -- green was less than black. local i : INTEGER ggb : INTEGER geb : INTEGER glb : INTEGER do io.put_string("green > black : ") from i := 1 ggb :=0 until i > count loop if greendie.item(i) > blackdie.item(i) then ggb := ggb + 1 end i := i + 1 end io.put_integer(ggb) io.put_string(" times.") io.put_new_line io.put_string("green = black : ") from i := 1 geb :=0 until i > count loop if greendie.item(i) = blackdie.item(i) then geb := geb + 1 end i := i + 1 end io.put_integer(geb) io.put_string(" times.") io.put_new_line io.put_string("green < black : ") glb := count - geb - ggb io.put_integer(glb) io.put_string(" times.") io.put_new_line;io.put_new_line end -- compare_green_black compute_frequency is -- This procedure reads the values from the three_dice array -- and then for every roll increments the corresponding value -- in the frequency array and therefore creates a frequency -- distribution. Then the frequency array is printed out to -- the screen using the print_array procedure. local i : INTEGER do !!frequency.make(3, 18) io.put_string("Value Frequency");io.put_new_line io.put_string("----------------");io.put_new_line from i := 1 until i > count loop frequency.put(frequency.item(three_dice.item(i)) + 1, three_dice.item(i)) i := i + 1 end from i := 3 until i > 18 loop if frequency.item(i) > high_freq then high_freq := frequency.item(i) end i := i + 1 end print_array(frequency) io.put_new_line end -- compute_frequency frequency_graph is -- This is the optional procedure for printing out a -- frequency histogram for the values in the frequency array. local i : INTEGER l : INTEGER axis_int : INTEGER axis_doub : DOUBLE no_of_axis_labels : INTEGER do --io.put_string("> ") --io.read_integer io.put_string("Frequency Histogram");io.put_new_line io.put_string("-------------------") io.put_new_line;io.put_new_line io.put_string(" 0 ") no_of_axis_labels := 5 from i :=1 until i > no_of_axis_labels loop axis_doub := high_freq*(i/no_of_axis_labels) axis_int := axis_doub.rounded --if high_freq*((i-1)/10) < 10 then -- io.put_string(" ") --else -- io.put_string(" ") --end io.put_integer_format(axis_int, 50//no_of_axis_labels) i := i + 1 end io.put_new_line io.put_string(" ") from i:=1 until i>50 loop io.put_character('-') i:=i+1 end io.put_new_line from i := 3 until i > 18 loop io.put_integer_format(i,4) io.put_string(" | ") from l := 1 until l > (frequency.item(i)*50)//high_freq loop io.put_character('*') l := l+1 end --io.put_string(" : ") --io.put_integer(frequency.item(i)) io.put_new_line i := i+1 end io.put_new_line end -- frequency_graph end -- class DICE_STATS