Lab 4 - Looping and Nested Loops

Write your programs in your Odin 2010/4 folder.

Part 1
Name this program gotozero.cpp

Use a while-loop in this program.

Ask the user for a number.
Your program will count from the number to zero.
Display all the counting numbers as you go.

sample program run...
Lab-4 gotozero program. Enter a number: 4 4 3 2 1 0
After you get your program working, you may add some code so that it will handle negative input, and still count to zero. sample program run...
Lab-4 gotozero program. Enter a number: -3 -3 -2 -1 0
For this, you should establish a stepping variable that is 1 or -1. int step; Use the ternary operator to set the step value. This will be similar to defining the "sign" of a number as we learned in class. If the user enters a positive number, the step is -1. if the user enters a negative number, the step is 1. This way, the same code can be used to move up or down toward zero.

Part 2

Name this program highest.cpp

Write a program that asks the user to enter numbers.

Keep track of the total, and also the highest number entered.

Whenever the user enters a number, check to see if it is the
largest number so far entered. If it is the largest, then output
a message showing the largest number and the accumulated sum of all
numbers entered.

Use the number zero as a sentinel value to end the program.

Program output will look similar to below...
Lab-4 highest program

Enter a number: 5

   5 is your highest number so far!
   5 is the total

Enter a number: 2
Enter a number: 1
Enter a number: 9

   9 is your highest number so far!
   17 is the total

Enter a number: 20
Enter a number: 0

Program is ending...

   20 was your highest number.
   37 was the total.

Your program should handle negative numbers also. Because zero is used as a sentinel or signal to end, the program should not attempt to handle negative numbers. It will work, but it will see the zero as a number and also a sentinel, which makes the output confusing. Because a user might enter many numbers before entering zero, this is a good opportunity for you to use a file for user input. 1. put some numbers in a file 2. run your program like this: ./a.out < my_file

Part 3

Name this program mtable.cpp

Use nested loops to print out a multiplication table.

Ask the user for the dimensions of the table.
Input of 4 would yield a 4x4 table, and look like this...

  1  2  3  4
  2  4  6  8
  3  6  9 12
  4  8 12 16

Use the setw() function to help align the columns of data. Documentation is here.
Additional header file #include <iomanip>

Examples:

cout << setw(4);
cout << setw(4) << i*j;

Set the width to a value, such as 4, for the values inside your multiplication table.
Also use setw() to align row and column headings. 

Other cout modifiers
Program output will look similar to below...

Lab-4 multiplication table.

Enter the table dimension (square): 4

           1   2   3   4
        ----------------
   1   |   1   2   3   4
   2   |   2   4   6   8
   3   |   3   6   9  12
   4   |   4   8  12  16


Lab-4 multiplication table.

Enter the table dimension (square): 7

           1   2   3   4   5   6   7
        ----------------------------
   1   |   1   2   3   4   5   6   7
   2   |   2   4   6   8  10  12  14
   3   |   3   6   9  12  15  18  21
   4   |   4   8  12  16  20  24  28
   5   |   5  10  15  20  25  30  35
   6   |   6  12  18  24  30  36  42
   7   |   7  14  21  28  35  42  49


What to turn in...
Gordon will find your programs on Odin during the lab.
Your programs must compile and run to receive credit.
What you don't finish in lab becomes homework.