Programming Assignment 4 Due March 6, 2015 at 11:59 PM Objectives

advertisement
Programming Assignment 4
Due March 6, 2015 at 11:59 PM
Objectives
This assignment will consist of writing a couple of small programs that involve practice writing and
calling functions.
Task
Write the following programs, each in a separate file. Filenames should be:
• seconds.cpp
• dice.cpp
(Note that the filenames are all lowercase)
Exercise 1
Filename: seconds.cpp
Write a function called Seconds that takes in three integer parameters (representing hours, minutes,
and seconds) and returns the number of seconds since the last time the clock “struck 12” (i.e. was
at 12:00:00 – AM or PM doesn’t matter since you’re not tracking this).
To test this function, write a main() routine (in the same file) that prompts the user to enter hours,
minutes, and seconds for two different clock times; then uses the Seconds function to calculate the
shortest amount of time in seconds between the two times (both of which are within one 12-hour
cycle of the clock); then print out the number of seconds since “striking 12” for both clocks, as well
as the number of seconds between the two clock times
Sample run 1: (user input underlined)
Input first clock time...
Hours: 6
Minutes: 45
Seconds: 30
Input second clock time...
Hours: 4
Minutes: 50
Seconds: 12
It’s been 24330 seconds since the first clock struck 12:00.
It’s been 17412 seconds since the second clock struck 12:00.
The two times are 6918 seconds apart.
Sample run 1: (user input underlined)
Input first clock time...
Hours: 12
1
Minutes: 43
Seconds: 16
Input second clock time...
Hours: 7
Minutes: 11
Seconds: 59
It’s been 2596 seconds since the first clock struck 12:00.
It’s been 25919 seconds since the second clock struck 12:00.
The two times are 23323 seconds apart.
Exercise 2
Filename: dice.cpp
Write a function called RollDice that returns the result from rolling two standard six-sided dice.
• The function will take no parameters, but it will return the resulting total of the dice roll as
an integer.
• Remember that the rolling of a die involves randomness, so you’ll need to use the random
number generation functions discussed in class (see note below).
• Note that rolling two 6-sided dice is NOT equivalent to picking a random number from 2
through 12. This is because when you roll 2 dice, there is a much larger chance of hitting
some totals than others. You need to roll two dice and add them together
To test this function, write a main() routine that does the following:
• Ask the user to enter how many times they want to roll the dice, and let them enter a value
(for this writeup, I’ll call it N).
• Call the RollDice function this many times (i.e. N times), and count how many times a total
of 2 (known as “Snake Eyes”) appears, as well as how many times a total of 7 appears.
• For each of these totals (2 and 7), print out how many times that total appeared, as well
as what percentage this is of the total number of rolls. Percentages should be printed to 2
decimal places.
You will need to check out this page on random number generation: http://www.cs.fsu.edu/ myers/c++/notes/rand.html. Note: When you seed the random number generator, go ahead and use
the time function, as illustrated in the class notes
Sample runs: (user input underlined)
Sample Run 1
How many times would you like to roll the two dice? 10000
Snake eyes (double 1s) appeared
268 times
2
2.68 % of the time
A roll of 7 appeared
1675 times
16.75 % of the time
Sample Run 2
How many times would you like to roll the two dice? 500000
Snake eyes (double 1s) appeared
13706 times
2.74 % of the time
A roll of 7 appeared
83300 times
16.66 % of the time
Sample Run 3
How many times would you like to roll the two dice? 500000
Snake eyes (double 1s) appeared
13921 times
2.78 % of the time
A roll of 7 appeared
83789 times
16.76 % of the time
Requirements
• No global variables, other than constants.
• The required tasks must be performed with the functions specified (not just with a single
main() routine).
– Note that each exercise requires the writing of a function, and a main routine to test
that function.
– Note that there is NO keyboard-input/screen-output specified in the functions themselves (i.e. “return” does not mean “print”).
• All input and output must be done with streams.
• You may use the iostream and iomanip libraries (the ones that have been discussed in class).
• For exercise 2, you’ll need the cstdlib and ctime libraries.
• When you write source code, it should be readable and well-documented.
• Your program should only use standard ANSI header files (make sure to follow the directions
exactly on the handout for creating Visual C++ projects, so that Windows-specific headers
like stdafx.h and conio.h are not placed into your file).
3
Submitting
Program submissions should be done through Blackboard under the Assignments tab. Make sure
you submit both .cpp files. Program submissions are due March 6 at 11:59 PM.
4
Download