Lab 5: Input and Conditional control flow
Objectives
After completing this lab, you should be able to:
- Obtain input with
gets
- Use a variety of number and string methods
- Convert between numeric and string values
- Generate and use random numbers
Update your repository
As always, start by running the following command in your repository to pull the most recent changes:
git pull
Logistics
All files referenced in this lab will be found in the lab05
subdirectory of your repository.
Mad Libs
Remember the game you played in grade school where you come up with words in different categories (e.g., "color", "person's name", "adjective", "number"), and then your friend reads you a story using those words? If you haven't played it before, the game's (official) name is "Mad Libs", and it's a harmless bit of fun. In this lab you'll be writing a program that lets you play said game with the computer.
When you run your program, it should prompt the player for at least 5 different words (in at least 3 different categories) and then print out a story. You have complete creative freedom over the categories of words and the story.
Here's what a run through a finished program might look like:
Please enter a person's name:
Michael
Please enter another name:
Jackie
Please enter a color:
blue
Please enter a body part:
nose
Please enter a country:
Thailand
Please enter an adjective:
crazy
Please enter an animal:
cat
Thanks! Here's your mad lib:
There once was a boy named Michael
who was born, sadly, with a blue nose.
He left his home country of Thailand
with his crazy, blue-eyed cat
named Jackie to escape to IIT.
Exercise 1: Mad Lib
Write a program that, as described above, plays a game of Mad Lib with the user. Your program, when run, should prompt the user for at least 5 different words in at least 3 different categories and should subsequently print out a unique story making use of those words.
Save your completed program in the file named "lab5ex1.rb", and demonstrate it to your TA.
Exercise 2: Random Mad Lib
Before starting this exercise, make a duplicate of your solution to the previous exercise with the following command:
cp lab5ex1.rb lab5ex2.rb
You'll be using the file lab5ex2.rb as a base for this exercise, which simply builds on your previous solution.
This time, to make things more interesting, you'll modify your mad lib program so that when run it still reads in the same set of words, but following that it prints out one of (a minimum of) three different stories using those words. The specific story it picks is random.
To generate a random number between 0 and some number N, you can do something like:
story_id = rand(5)
The above line generates a random integer between 0 and 4 (inclusive) and places it in the variable story_id
.
Update your program in "lab5ex2.rb" with your new stories and randomizing code and demonstrate it to your TA.
Exercise 3: The Quadratic Formula, Revisited
Now that we've learned how to obtain input from the user and how to run sections of our programs conditionally, we're going to go back and make the quadratic formula program you wrote for the last lab a little smarter (and more useful).
Start by copying your solution from the last lab into the "lab05" directory. If you're already in the "lab05" directory, you can accomplish this with the following command:
cp ../lab04/lab4ex2.rb lab5ex3.rb
You will be updating your program so that it works in the following way:
- the three coefficients a, b, and c will be read from the user
- if the coefficient a is zero, the program will solve the linear equation and print out the single root
- if the discriminant is less than zero, the program will print out a message to that effect, and will not compute any roots
- if, on the other hand, the discriminant is greater or equal to zero, the program will compute and print out both roots
The following are some sample runs of a finished program:
First, with a=0
Enter coefficient a:
0
Enter coefficient b:
1
Enter coefficient c:
2
The solution to the linear equation is x=-2.0
Next, with a negative discriminant
Enter coefficient a:
1
Enter coefficient b:
2
Enter coefficient c:
3
The discriminant is less than zero; there are no real roots!
And with a positive discriminant
Enter coefficient a:
1
Enter coefficient b:
1
Enter coefficient c:
-2
The two roots are as follows:
x1=1.0
x2=-2.0
Show your program to your TA when ready.