CS 010 - Introduction to Computer Science I
Lab 5.
Decision Making Structures and Loops

Points (10 overall)

  1. 2 pts: Attendance(at start and end of lab)
  2. 7 pts: Programs
  3. 1 pt:  Commenting and Style

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 with

  1. using if structures
  2. using if-else structures
  3. using while loops
  4. validating input

Commenting and Style

  1. Be sure to use good variable names.
  2. Use comments throughout your code to explain your code.
  3. Use good spacing and keep it consistent throughout your program.

 


Program 1: If Structures and Loops

Write a program that allows the user to enter as many integers as they want.  You will need to decide how they will tell you when they are done (remember that all integers are good input).  After entering the input, the program should then output the maximum and minimum values entered. 

 

Hints:

Read in the first value before entering the loop.  Make this value the initial value of both the max and min variables.


Program 2: If Structures and Loops

Write a program that allows the user to enter two integers.  Then print out all the values between (and including) these two integers.  Do not assume that the user will enter the smallest integer first.  For example, if the user enters -7 and 2, then the program would print:   -7, -6, -5, -4, -3, -2, -1, 0, 1, 2.  If the user enters 2 and then -7, your program would print the same values.  You should error check each value that is entered to make sure that they entered the correct type (use the cin.fail() function).  If the user types in bad input, your program should have them re-enter the input until it makes sense.

 


Bonus Program (1 pt):

Write a program that allows the user to enter a value between 1 and 5 inclusive.  Force them to re-enter if they enter an invalid input.  Then use a loop to print out the square of the given value, then the square of the result, and so on for 4 outputs.  For example, if the user entered 2, then the output would be the following;

 

Value    Square

2          4

4          16

16        256

256      65536