CSCE 2004 - Laboratory Assignment 4

    The objective of this lab is to gain experience with iteration in C++ using the while statement. The assignment has the following steps:

  1. Getting Started
  2. Refer to the instructions in previous labs to login to turing, create and move into a subdirectory for lab4, start nano editor, and run the C++ compiler as needed to create, compile and execute the programs discussed below.

  3. While Loops
  4. We often end up performing the same operation over and over in a program. Although "cut" and "paste" editors make it easy to duplicate portions of a program, repeating code wastes space and more importantly can lead to problems when code needs to be modified. You can introduce bugs by forgetting to modify one of the copies of the code. This problem can be avoided using the while loop. The C++ syntax of the while loop is:

    // initialization statement
    while ( logical expression  )
    {
       // block of code to be repeated
       // update variables in logical expression
    }
    

    The while loop starts out like an if-else statement. The logical expression is evaluated, and if it is TRUE the following block of code is executed. Then the program goes back up to the logical expression and it is evaluated again. If it is still TRUE, the block of code is executed again. This cycle continues until the logical expression becomes FALSE. Then the program will jump to the code following the while loop and start executing statements.

  5. Running While Loops
  6. Create a file called "loop1.cpp" in your labs/lab4 directory, and use "cut" and "paste" to copy the program below into this file. Use g++ to compile the program. Run it type in "10" to see what it prints. Hopefully you will see some numbers. Try a few other values to see what happens.

    Hit control-C to kill a program that is running for a long time.

    #include <iostream>
    using namespace std;
    int main()
    {
       // Get number from user
       int number = 0;
       cout << "Enter a number: ";
       cin >> number;
    
       // Print some numbers
       cout << "Here are some numbers\n";
       int count = 1;
       while (count < number)
       {
          cout << count << endl;
          count = count + 1;
       }
       return 0;
    }
    

    Copy and paste a sample of your program output below.

  7. Modifying While Loops
  8. The purpose of this experiment is to show you that changes to the increment operation, changes to the logical expression, and changes to the loop calculations can create a wide variety of different program effects.

    Step 1: Copy your "loop1.cpp" program to create "loop2.cpp". Modify "loop2.cpp" to change the "count = count + 1" line to add 2 to the count variable. Recompile and run your program to see what is output. How many times does the loop execute this time?

    Step 2: Next, edit the program and change the logical expression so it says "(count < number * 2)" and recompile. When you run the program what does it output now?

    Step 3: Finally, edit the cout statement inside the loop so you print out "count / 2". When you recompile and run the program you should see a familiar output.

    Copy and paste your loop2.cpp program below.

    Copy and paste a sample of your program output below.

  9. Calculations with While Loops
  10. Step 1: Copy your "loop1.cpp" program to create "loop3.cpp". You will now modify "loop3.cpp" to calculate the total of all of the numbers that are being printed out. To do this, you will need to create and initialize a "total" variable outside the while loop. Then you need to update this variable inside the loop.

    Step 2: To show that your code is working, change the "cout" statement inside the loop to print out the count and the total. When your program is working, test it with a variety of input values. You should see something like this:

    Enter a number: 10
    Here are some numbers
    1 1
    2 3
    3 6
    4 10
    ...
    

    Copy and paste your loop3.cpp program below.

    Copy and paste a sample of your program output below.

  11. Using Loops to Process Input Data
  12. As you have seen, a while loop performs the sequence of operations inside the {} brackets multiple times, and stops when the logical expression is false. This tool can be used to solve a wide variety of problems.

    For example, consider calculating a restaurant's average daily sales. One way to do this would be to read a sequence of daily sales amounts and then divide by the number of days to get the average. The C++ program below uses this approach.

    #include <iostream>
    using namespace std;
    int main()
    {
       // Declare and initialize variables
       int DailySales = 0;
       int TotalSales = 0;
       float AverageSales = 0.0;
       int NumDays = 0;
    
       // Read daily sales amount
       cout << "Enter the amount of today's sales in whole dollar values, or -1 to quit: ";
       cin >> DailySales;
    
       // While loop to total up the daily sales
       while (DailySales != -1)
       {
          // ADD code here to update the total sales
    
          // ADD code here to update the number of days whose data you have
    
          // Read daily sales amount
          cout << "Enter the amount of today's sales in whole dollar values, or -1 to quit: ";
          cin >> DailySales;
       }
    
       // Calculations after while loop
       cout << "Your total sales after " << NumDays << " days is " << TotalSales << "\n";
    
       // ADD code here to calculate the average daily sales
       
       // ADD code here to output the average daily sales
    
       return 0;
    }
    

    Step 1: Create a file called sales.cpp, and use "cut" and "paste" to copy the program above into this file. Use g++ to compile and run this program. You will notice that this code uses a sentinel value of -1 to stop the while loop. This is one way to process a group of input values when you do not know the number of input values to process in advance.

    Step 2: As you can see, there are several comments showing where code must be added to complete this program. Follow these instructions, and compile and debug the code as needed. When you are finished, test the program by processing N values where you can easily verify the results by hand. Congratulations, you have finished the lab!

    Copy and paste your sales.cpp program below.

    Copy and paste a sample of your program output below.

  13. Submit Work
  14. This lab assignment will be submitted electronically to the TAs once you fill in the fields below and click on the "submit" button. You do NOT need to print a copy of this document to hand in.

    Your UAID number:
    Your website PASSWORD: