You will be working in pairs during labs. Pairs will be randomly selected and will be announced by the TA. Each week you will have a new random partner. 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
#include <iostream>
using namespace std;
int main()
{
int junk_food;
cout << "Do you want ding-dongs or ho-hos:"
<< "(1 for ding-dongs, 0 for ho-hos)"
<< endl;
cin >> junk_food;
if ( junk_food == 1)
{
cout << "Have some ding-dongs!" << endl;
}
if ( junk_food == 0)
{
cout << "Have some ho-ho's!" << endl;
}
return 0;
}
In this case the computer actually made a decision as to what to print based on the user input.
Notice that the structure of an if statement is fairly close to English. However take particular notice of the == operator. It's different from the = operator which assigns values. The == operator simply checks whether two values are equal. Sometimes it's read "is equal". Compile and run this program.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password;
cout << "What's your password?" << endl;
cin >> password;
//the password can't have spaces since we are not using getline()
if (password == "blood_sucker")
{
cout << "You typed the correct password. "
<< "Welcome to Super Evil Enterprizes!" << endl;
}
else
{
cout << "You are human. We will destroy you!!" << endl;
}
return 0;
}
Change the above to some other kind of conditions and show the TA. Note that using if-else statements can simplify the code since you don't have to have two if statements.
Write a program that outputs whether a student passed or failed an exam. If the exam score is 60 or higher, the student passed. If the score is below 60, the student failed. The exam score should be input by the user and an appropriate message sent to the screen.
HINT: Instead of the == used in Exercise 1 and 2 above, you can use <= (less than or equal to), >=(greater than or equal to), <(strictly less than) and >(strictly greater than).
Write a program that asks the user to choose where they want to go. Let there be two choices, the moon and mars. If they chose mars, ask them for their weight and then output their weight times (377/1000). If they chose the moon ask them for their weight and output their weight times (1/6). Test this with a weight of 100. Remember to be careful about integer vs. floating-point division. Have the TA check the results and your code.
Write a graphics program that asks the user to click on 2 points within the window. Then draw a rectangle with these points being opposite corners of each other. If the user chooses points that have either the same x coordinate or the same y coordinate (this would cause a horizontal or vertical line instead of a rectangle), output an error message and exit the program, otherwise, draw the rectangle.
For example, if the user clicks on points with the coordinates (1,2) and (4,0), then the 4 corners (Points) of the rectangle would be (1,2), (1,0), (4,0), and (4,2).