CS 010 - Introduction to Computer Science I
Lab 4. Branches



Points(10 overall)

Collaboration policy:

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.


Lab Objectives

To gain experience in


Exercise 1. Teaching computers to think: if statements

C++ and computers in general can do more than just crunch numbers and draw pictures as we've been doing so far. We can ask the computer to do some primitive thinking for us using if statements. Here is an example:
#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.

After this works, modify the cout statements and the variable name to reflect some other choice. There are many choices one can make so you can try to be creative. Your TA may note the funniest/most interesting/wackiest choices and share them with the class(keeping your name anonymous).

After your program runs, take some time to reflect on this moment. By using the if statement(no matter how wacky!) you have actually written a program that can act based on a logical condition. This simple ability is at the heart of what makes computers work. Notice, since we can set the logical condition to anything we want, that computers are still helpless and stupid without us! For instance, we can switch things around so that if one selects hohos we give them ding-dongs. The stupid computer can't tell the difference(ha-ha-ha!). It just follows orders. For extra practice try changing the program around so that hohos are given if ding-dongs are selected and ding-dongs are given if hohos are selected.


Exercise 2: if-else

Sometimes when we use if statements we want the computer to do one thing given a certain condition and another thing for all other conditions. To make this easy, C++ has the if-else statement. For instance:
#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.


Exercise 3

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).


Exercise 4

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.


Exercise 5

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).