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

Points(10 overall)
Collaboration policy:
Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.

Lab Objectives

To gain experience in


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


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


Program 1

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 Review 1 and 2 above, you can use <= (less than or equal to), >=(greater than or equal to), <(strictly less than) and >(strictly greater than).


Program 2

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.


Program 3

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


Review 3: Simple loops with while statements

While statements are another way to teach the computer to think. In this case, the computer must decide whether to repeat certain statements or not. The following program demonstrates one use for the while statement.

This first example does not use a while statement:

/* PURPOSE:  Count number of digits needed to express an integer in base 10
             using multiple if statements
*/

#include <iostream>

using namespace std;

int main()
{  
   int input;

   cout << "Input an integer between 1 and 9999: ";
   cin >> input;

   int temp = input;
   int number_of_digits = 1;

   if (temp > 9)
   {  
      temp = temp / 10;
      number_of_digits++;
   }
      
   if (temp > 9)
   {  
      temp = temp / 10;
      number_of_digits++;
   }

   if (temp > 9)
   {  
      temp = temp / 10;
      number_of_digits++;
   }

   if (temp > 9)
   {  
      temp = temp / 10;
      number_of_digits++;
   }
   
   cout << input << " can be expressed in " 
        << number_of_digits << " digits" << "\n";
   
   return 0;
}

But having to write

   if (temp > 9)
   {   
       temp = temp / 10;
       number_of_digits++;
   }

four times, even using copy/paste, is clearly repetitive! It also only works for input <= 9999. One would like to have a way of testing that the input is still greater than 9, and executing the succeeding control block if it is. Replacing if with while does it.

/* PURPOSE:  Count number of digits needed to express an integer in base 10
             using a while loop
*/

#include <iostream>

using namespace std;

int main()
{  
   int input;
  
   cout << "Input an integer: ";
   cin >> input;
   int number_of_digits = 1;
   int temp = input;

   while (temp > 9)
   {  
      temp = temp / 10;
      number_of_digits++;
   }

   cout << input << " can be expressed in " 
        << number_of_digits << " digits" << "\n";   
}

Program 4

Write a program that asks the user for a positive integer. The program should then output all the integers between and including the number input by the user and -5.

For example, if the user enters the value 3, the output would be:

3, 2, 1, 0, -1, -2, -3, -4, -5


Program 5

Write a program that asks the user to enter as many positive integers as they want. When they are done, they should type the sentinal value, -1. Your program should then output the average of these numbers, not including the -1.

For example, if the user enters the values:

100 25 75 200 -1

the output should be:

Avg: 100