Description
Problem Set
For each of the following problems, create a program to solve the problem. Divide the
program into separate, but cooperating, functions. Do not write the complete program
as one big ”chunk” of statements in main().
Problem 1: The Story Generator
In the game Mad-libs, players are asked for parts of speech, such as noun, adjective,
or adverb, and those words are plugged into a template sentence to generate a
sometimes- funny story.
Menu Function
Write a function called menu that is called by main when your program starts.
Inside your menu function, you should first ask the user which story they want to
play. Your question should look like:
“Which story would you like to play? Enter the number of the
story (1, 2, or 3) or type q to quit: ”
If the user types q your program should print “good bye” and exit the function.
If the user types in an invalid input, for example “17” or “slkerjqoe”, you should print
“Valid choice not selected.”
If the user types in a valid story number, the menu function should call the
corresponding story function (story1, story2, or story3).
The user should be allowed to play the game as many times as they like. After
printing the new story, your program should ask again which story they would like to
play.
“Which story would you like to play? Enter the number of the
story (1, 2, or 3) or type q to quit: ”
CSCI 1300 – Assignment 3 Due Sunday, September 24 at 6:00 PM
Story Functions
Write 3 functions named story1, story2, and story3 that each allow the user to play
a simple game of Mad-libs. Each function should store the corresponding story
template and ask the user for the missing word types to fill in the template.
For a story template that looks like this:
is a who lives in .
The story function should ask for inputs in the following format:
Then the story function should print the new sentence with the user entered inputs in
place of the word-type placeholders.
Pat is a student who lives in Boulder.
Use the following template for story1:
In the book War of the , the main character is an
anonymous who records the arrival of the s
in .
Ask for the following inputs for story1:
“Enter a plural noun: ”
“Enter an occupation: ”
“Enter an animal name: ”
“Enter a place: ”
Use the following template for story2:
, I’ve got a feeling we’re not in <STATE/COUNTRY>
anymore.
Ask for the following inputs for story2:
“Enter a name: ”
“Enter a state/country: ”
CSCI 1300 – Assignment 3 Due Sunday, September 24 at 6:00 PM
Use the following template for story3:
Hello. My name is . You killed my .
Prepare to .
Ask for the following inputs for story3:
“Enter a first name: ”
“Enter a relative: ”
“Enter a verb: ”
IMPORTANT NOTE: User-input to play further must be obtained inside the menu
function, not within your story1, story2, story3 and functions. That is, the loop that
enables the game to be played indefinitely must not be within your story functions –
COG will not accept it.
Note: If a function does not take a parameter, you must specify the parameters as
void.
e.g. story1(void)
Problem 2: Wind Chill
The wind chill is the still-air temperature that would have the same cooling effect on
exposed human skin as a given combination of temperature and wind speed.
“Windchill.” Merriam-Webster.com. Merriam-Webster, n.d. Web. 5 Sept. 2017.
For this problem, we will be recreating part of this wind chill calculator provided by the
National Weather Service (NWS).
Use this formula for wind chill for your calculations.
CSCI 1300 – Assignment 3 Due Sunday, September 24 at 6:00 PM
Part A
Write a function windChillCalculator to determine the wind chill. Your main function
should take in user inputs for T (air temperature) and V (wind speed) and pass these
values as parameters to your function. After your function calculates the wind chill it
should return it to main.
To test your code, for T = 30.0 and V = 5.0, you should get a Wind Chill of 24.7268.
Within your main function, print the following cout statement:
cout << “The wind chill is ” << wind_chill << ” degrees F.” <<
endl;
HINT: Look at the pow() function in the C++ math.h library
Part B
Once you have the wind chill calculation working, create another function
printWindChill. This function should call windChillCalculator and print all the wind
chill values for a fixed temperature and variable wind speeds. You will need to write a
loop in this function to iterate over the different wind speeds.
The function should receive T (air temperature), low_wind_speed, high_wind_speed,
and wind_speed_step as input parameters.
For Example: Let the input parameters be T = 30.0, low_wind_speed = 5.0,
high_wind_speed = 8.0, and wind_speed_step = 1.0, your function should print out the
wind chill for a temperature of 30 °F and wind speeds starting from 5 mph and ending
at 8 mph, incrementing by 1 mph. (5 mph, 6 mph, 7 mph, and 8 mph)
Use the following cout statement to output the values from within the function:
cout << “The wind chill is ” << wind_chill << ” degrees F for
an airtemperature of ” << T << ” degrees F” << ” and a wind
speed of ” << V << ” mph.” << endl;
CSCI 1300 – Assignment 3 Due Sunday, September 24 at 6:00 PM
Points to remember when running your code in COG:
These are some things in which COG is very rigid:
1. Students *must* include *void* for the input parameter of functions that don’t
take input, rather than leaving it blank.
For example,
*correct* int some_function (void) {}
*incorrect* int some_function () {}
2. Whenever you are prompting the user for input from *within* a user-defined
function, they must end the prompt with a colon (:).
For example,
// Inside some_function()
*correct* cout << “Please enter a value for x: “;
*incorrect* cout << “Please enter a value for x “;
3. Answers printed from *within* a user-defined function must end with a period.
// Inside some_function()
*correct* cout << “The answer is 42.”;
*incorrect* cout << “The answer is 42!”;
*incorrect* cout << “The answer is 42”;
4. In addition to #3, answers printed from *within* the user-defined function must
exactly follow the cout string template given in the writeup.
Challenge Problems: for those who want more coding practice
These problems are for your practice only, DO NOT submit to COG, do not submit to
Moodle. You can show your results to the CAs, TAs, and the instructor to validate that
you have achieved the correct results.
Challenge #1: Create a function that can search a story for a placeholder (‘<’ description ‘>’) and prompt the user for the replacement. For example, in the story
below, search the string to find the first placeholder “” and prompt the
CSCI 1300 – Assignment 3 Due Sunday, September 24 at 6:00 PM
user with “Enter PLURAL NOUN:” to get the response from the user. Place the
response into the story in place of the placeholder. Repeat this process until all the
placeholders have been replaced.
In the book of the , the main character is an
anonymous who records the arrival of the s
in .
Challenge #2: Create a program to generate the chart below: