Lab 7: Loops and Arrays
Contents
Description
The purpose of this lab is to make you comfortable working with loops and arrays. By the end of the lab, you should know how to:
- use a while loop to interact with a user
- use a do-while loop to interact with a user
- use a for loop to interact with a user
- create static and dynamic arrays
- use a for loop to iterate through an array
Tasks
Complete each of the following parts in a separate file and have the professor check your work before the end of lab.
Task 1: While loops
We've seen how while loops work. If you forget, check out the Loops page. The code below asks the user to enter a valid option. If the option is not valid, an error message is displayed and the user is prompted to enter a valid option. Copy the code into a file named lab7-task1.cpp. Fill out the header, then compile it and run it on the command line.
Now, here are three different scenarios that I've tried on the command line:
$ ./lab7-task1.cpp Please choose to [c]ontinue or [q]uit: c Great, you chose to continue!
$ ./lab7-task1.cpp Please choose to [c]ontinue or [q]uit: k Sorry, the option you selected (k) is not valid. Please choose [c]ontinue or [q]uit: q Exiting...
$ ./lab7-task1.cpp Please choose to [c]ontinue or [q]uit: k Sorry, the option you selected (k) is not valid. Please choose [c]ontinue or [q]uit: r Great, you chose to continue!
In the first one, I've entered a valid option from the get go. I selected to continue, and indeed it was processed as expected. In the second one, I gave an invalid option at first: k
isn't an option. I'm warned about it, enter the valid option q
, and the program exits as expected. In the third case, you can see that I've entered an invalid option both times! However, the code only checks for validity after the first input. The second option, r
, is not q
, so the program doesn't exit. Instead, since it made it past the check for q
, we assumed the user must have meant to continue.
Looping can help! Modify the program by inserting a while loop that will keep displaying an error message as long as an invalid option has been given. Only a small piece of the code should be involved in the while loops. Once you have completed the task, call the professor over.
Task 2: Do-while loops
The code below asks the user to input two numbers, which the program then adds together and outputs the answer. Copy this into a new file called lab7-task2.cpp. Fill out the header, then compile it and then run it on the command line.
You should see output that looks like the following:
$ ./lab7-task2 Please enter two numbers to add: 5.6 9 Sum: 14.6
Your job is to modify the program so that it keeps prompting the user for input until he or she wants to stop. Use a do-while loop to accomplish this. You will need to add an extra prompt to check if the user wants to stop or not (kind of like what we did in Task 1 above).
Task 3: For loops
The third kind of loop we've learned about are for loops. These are primarily used to iterate over a range of values. They are generally a good fit any time you want to do something a pre-specified number of times. Consider the code below—it's the same code we had before. Copy it into a file named lab7-task3.cpp and fill out the header.
Your task is to modify this code so that the user is prompted exactly 10 times via a for loop. Note that you do not need to add any extra prompts like we did in Task 2.
Task 4: Arrays
We've seen that arrays can be very helpful in allowing us to store lots of values without having to create a separate variable for each. Consider the code below. It asks the user for the names of their closest friends. Right now, each friend's name is given their own variable, and there are only five of them. Copy this code to a file named lab7-task4.cpp. Then compile and run it on the command line.
Running this should produce something along the lines of the following:
$ ./lab7-task4 Please enter the name of one of your favorite friends: Jackie Please enter the name of one of your favorite friends: Pete Please enter the name of one of your favorite friends: Katie Please enter the name of one of your favorite friends: Tina Please enter the name of one of your favorite friends: Dan Your favorite friends are: Jackie, Pete, Katie, Tina, Dan.
The code is very repetitive and we are limited to only five friends. In this task, you have two jobs:
- instead of using five variables, one for each friend, create an array of strings that will hold five values; be sure to store the size of the array (5 in this case) in a global constant, just above main:
const int FRIEND_COUNT = 5;
- instead of prompting the user five times, use a for loop that will iterate over each element of the array, prompting the user for each one
If you are unsure about using arrays, take a look at Lab 6 and at this reading on arrays. The latter is especially helpful for the second job in the list above.
Task 5: Free for all
Using what we've done so far, come up with a new program. Store it in a file called lab7-task5.cpp. Your program can do anything you want, but must demonstrate your ability to use loops or arrays (your program must have at least one loop or must use arrays). Some ideas are: update the guessing game we did back in Lab 3 to use loops and modify the code we saw to initialize Student structs at the bottom of the reading on structs to use arrays.
(Back to top)