You will be working in pairs during labs. Pairs will be selected and announced by the TA. You will discuss the programs with your partner, but you will still be typing in your own code to show to the TA. You can help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc.
To gain experience in
Often computers are used to do very repetitive tasks we humans find tedious. One way to program the computer to do the same task multiple times is through a loop structure. In this lab we will work with a C++ loop structure called the while loop.
For example, if we wanted the computer to output the numbers 1 through 10, each number on its own line, we could use one cout statement without too much difficulty.
cout << "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";
However, this would not be too much fun if we wanted to output the numbers 1 through 100 or 1000. Instead, let's try using the while structure.
#include <iostream>
using namespace std;
int main()
{
const int LOOP_BEGIN = 1;
const int LOOP_END = 10;
int count = LOOP_BEGIN;
while (count <= LOOP_END)
{
cout << count << endl;
count++;
}
return 0;
}
It is important to understand the order of operations of this while structure. Here are the steps that take place when execution of a C++ program reaches a while strucuture:
int count = LOOP_BEGIN;
(count <= LOOP_END)
{
cout << count << endl;
count++;
}
Repeat step 2Now this seems like a lot more work than the previous cout statement to output the numbers 1 through 10. But let's use this program to output other numbers that would be very tedious to do with just the cout statement.
count++;and only this statement that will output just the even numbers from 10 - 100.
Another type of while loop is one that is ended by a sentinal value. A sentinal is like a guard. When the conditional expression "sees" the sentinal value it ends the loop.
For example, if we wanted a program that read in last names of everyone in the class and gave everyone an A except for students with a last name starting with a Q we could write it like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const string SENTINAL = "quit";
string last_name;
// get first student's last name
cout << "Enter each student's last name: (type quit when done)"
<< endl;
cin >> last_name;
// continue until the user enters the sentinal value
while (last_name != SENTINAL)
{
// if last name starts with Q, output F, otherwise output A
if (last_name.substr(0,1) == "Q" or last_name.substr(0,1) == "q")
{
cout << last_name << ": F\n";
}
else
{
cout << last_name << ": A\n";
}
//get next student's last name
cin >> last_name;
}
cout << "\nAll Done. Hope you got an A!\n";
return 0;
}
Now, write your own program, but instead of reading in names, you will read in scores of an exam (0 to 100 possible points). We want this program to work for any number of students, so the user will have to type in a sentinal value when they run out of scores to enter. Given the number of points possible, a negative number might be a good choice for a sentinal value. Instead of outputting something within this loop, you will keep track of how many A's, B's, C's, D's and F's there are. Then output this when the user is done entering scores (after the while loop is done). Use the scale 90 - 100 is an A, 80 - 89 is a B, 70 - 79 is a C, 60 - 69 is a D and 0 - 59 is an F.
Hint: You will need an int variable for each letter grade. Increment the appropriate variable within a branch of an else/if structure.
Write a graphics application that asks the user for a number between 1 and 10 and then outputs that many circles in the center of the window. Each circle should have a different radius. The first circle will have a radius of 1, the second a radius of 2, the third a radius of 3, and so on up to the number of circles specified by the user.
HINT: Use a loop with the body of the loop just declaring and drawing one circle. Your loop ending condition will now be the number entered by the user. You would still initialize the count to 1. When declaring the circle make sure you use the value of the count variable for the radius instead of a literal.
Write a program that reads in 2 integers from the user. Then output the equation of the sum of all integers between and including the values read in by the user. You should ask the user to enter a larger value for the second number. If they do not, allow them to keep entering values until it is.
So, if the user enters 5 and 10, your program should output:
5 + 6 + 7 + 8 + 9 + 10 = 45
If the user enters 10 and 5, you should ask them to reenter the second value until it is larger than 10. Then output the equation.
(Hint: After verifying the correct input, output the first number before entering the loop. Then in the body of the loop, output the " + " and the next number. That way, when the loop is done, you won't have an extra "+" at the end.)