CS 010 - Introduction to Computer Science I
Lab 9.
For 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 for loops
  2. using nested for loops
  3. continued use of functions

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: For Loops and Functions

Re-write your program 2 from lab 5.  This was the specifications from lab 5.

 

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.

 

Now make the following adjustments:

    1. Write a function that deals with the inputting of the two values.
    2. Write a function that outputs the values between (and including) two integers given as parameters.  The function will assume that the first parameter is the smallest and the second parameter is the largest.  Use a for loop to output these values.
    3. Write your main so that you use the given functions.

 


Program 2: Nested For Loops

Write a program that prints a multiplication table for the values 1-12.  You should use nested for loops to solve this problem. While you need to output the table of values, you do not need to make it look exactly as follows with all the lines and spacing. Just make it look presentable.

 

         1    2    3    4    5    6    7    8    9   10   11   12
     ------------------------------------------------------------
  1 |    1    2    3    4    5    6    7    8    9   10   11   12
  2 |    2    4    6    8   10   12   14   16   18   20   22   24
  3 |    3    6    9   12   15   18   21   24   27   30   33   36
  4 |    4    8   12   16   20   24   28   32   36   40   44   48
  5 |    5   10   15   20   25   30   35   40   45   50   55   60
  6 |    6   12   18   24   30   36   42   48   54   60   66   72
  7 |    7   14   21   28   35   42   49   56   63   70   77   84
  8 |    8   16   24   32   40   48   56   64   72   80   88   96
  9 |    9   18   27   36   45   54   63   72   81   90   99  108
 10 |   10   20   30   40   50   60   70   80   90  100  110  120
 11 |   11   22   33   44   55   66   77   88   99  110  121  132
 12 |   12   24   36   48   60   72   84   96  108  120  132  144


Bonus Program (1 pt):

Write a function that prints an isosceles triangle of *s.  It should take one parameter specifying the base of the triangle (the function can assume it will receive an odd value).  For example, if the base of the triangle was 7, the following would be printed.

 



        *
       ***
      *****
     *******

 

You should use nested for loops to solve this problem.

 

Write a short main that allows the user to input the base of the triangle and then print the triangle.