Lab 7: Loopy Exercises

Objectives

After completing this lab, you should be able to:

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 lab07 subdirectory of your repository.

Do Over!

To date, you've written a number of programs that perform varying sorts of computations (some involving input from the user). Each of these programs, after computing whatever it is they compute and producing whatever output it is they're meant to produce, simply quit. Instead of having our programs quit immediately, it's fairly common to want them to ask if we'd like to perform the computation over again, to save us the hassle of having to rerun the program.

This is a scenario where a loop really comes in handy.

Exercise 1

For your first exercise, you'll go back and modify two of your previous programs so that, upon completion of each run, they ask the user if he/she would like to start over again.

The two programs you'll be inserting this logic into are the Random Mad Lib and revisited Quadratic Formula program from lab 5. First, you'll copy your old code over by running the following commands from within the "lab07" directory:

cp ../lab05/lab5ex2.rb lab7ex1a.rb
cp ../lab05/lab5ex3.rb lab7ex1b.rb

Now you'll modify the files "lab7ex1a.rb" and "lab7ex1b.rb" that contain, respectively, your Random Mad Lib and Quadratic Formula programs.

The following is a sample session with a properly modified Quadratic Formula program:

Enter coefficient a:
1
Enter coefficient b:
2
Enter coefficient c:
2
The discriminant is less than zero; there are no real roots!

Do you want to enter another set of coefficients?
y

Enter coefficient a:
0
Enter coefficient b:
1
Enter coefficient c:
1
The solution to the linear equation is x=-1.0

Do you want to enter another set of coefficients?
n

Your modified Random Mad Libs program should be written so that it gives the user two options after generating a story: (1) to enter a new set of words to generate another story, or (2) to generate another story using the same set of words.

The following is a sample session with a properly modified Random Mad Libs program (note that the number of words is reduced here for brevity -- your program should read at least 5 words, as before):

Enter a name:
Joan
Enter a country:
Sweden
Enter an adjective:
bouncy

Here's your mad lib:

In Sweden there lived a maiden faire,
A bouncy gal by the name of Joan O'Hare!

Enter a new set of words, generate another story, or quit? (n/g/q)
g

Here's your mad lib:

Joan loves to travel.
In particular, Joan loves bouncy plane rides.
Last year, Joan flew on a particularly bouncy plane to lovely Sweden!

Enter a new set of words, generate another story, or quit? (n/g/q)
n

Enter a name:
Michael
Enter a country:
Brazil
Enter an adjective:
fast

Here's your mad lib:

Michael loves to travel.
In particular, Michael loves fast plane rides.
Last year, Michael flew on a particularly fast plane to lovely Brazil!

Enter a new set of words, generate another story, or quit? (n/g/q)
q

Thanks. Bye!

Exercise 2: A Guessing Game

Here's a fun idea for a game -- I'll think of a secret number between 0 and 9999, and you try and guess it.

To make it even more fun, I'll give you a hint each time you guess (if you guess wrong), letting you know whether the number I'm thinking of is greater than or less than your guess.

Even more fun -- I'll only give you 10 tries to guess! If you guess my number within 10 tries, you win. If not, I win! (Oh, and either way, I'll give you the option of playing again)

Fun!

But really, I haven't the time to sit here and think of numbers and give you hints --- much better if you'd just write a program to play the game yourself. So ... off you go, then.

(Last bit of advice -- your program should randomly generate a secret number in the given range each time, so the game is a bit more interesting)

Save your program in lab7ex2.rb and play it for your TA.

Exercise 3: Computing Factorials

Last up, here's an opportunity for you to think about and practice coming up with an invariant for a loop-based computation.

For this exercise, you'll write a program that reads in an integer number and computes the factorial of the number. The factorial of some non-zero, positive number N is simply N × (N-1) × (N-2) × ... × 2 × 1. The factorial of 0, by definition, is 1. The factorial of any negative number is undefined.

Your program should compute the factorial in a loop, and should only compute the factorial for a number when it is ≥ 0 -- for any other value it should print out "Factorial undefined". Either way, the program should ask the user if he/she wishes to compute another factorial before quitting.

If you feel up to it, try coming up with the loop invariant and termination conditions before starting to write your code. We'll talk about it in class next week.

Save your program in lab7ex3.rb.