CSCE 2004 - Laboratory Assignment 5

    The objective of this lab is to gain experience with iteration in C++ using the do-while loop and the for loop. This lab assignment also illustrates the use of nested loops. In general, we can have any kind of loop nested within any other kind of loop, but in this lab we will use for loops nested within for loops to illustrate nesting. The assignment has the following steps:

  1. Getting Started
  2. Refer to the instructions in previous labs to login to turing. Before we start with programming, we will experiment with some helpful Linux commands.

    The man command

    To learn more about any Linux command, you can read the on-line manual page for that command. To view this man page, type "man command-name". You have to hit return to go to next page of output, and hit 'q' to exit the man page. The key things to figure out are "what are the input/output parameters for the command" and "what are the flags that control how the command works". We will see examples of both when we look at the man pages for the following Linux commands.

    The mkdir command

    If you type "man mkdir" you will see that there are a number of options you can specify when you create a directory. Read the man page to see what the "-p" flag means. Now execute "mkdir -p 2004/labs/lab5". Did the command do what you expected?

    The ls command

    If you run "man ls" you will see that there are dozens of options for this command. For example, if you execute "ls -l" you will see a long listing with file ownership and protection information, and the date/time the file was last modified. Using "ls -ltr" will sort the files by time, with the most recently changed files at the bottom of the list. Try a few variations of the "ls" command to see what happens.

    The indent command

    Not all programmers are careful formatting their code when they are writing and debugging software. Fortunately the "indent" program was created to read and format C++ programs to make them look better. If you run "man indent" you will see that there are literally dozens of formatting options.

    Make a copy of your hw1.cpp or hw2.cpp in your lab5 directory, and run "indent hw.cpp" and use nano to see what your program looks like. Do you like it this way? Now see what happens if you use "indent -i3 -bli0 -npsl -npcs hw.cpp". Which do you like better? Experiment with several other indent options to see what they do. Once you find a combination you like, you should type your code to match this style, and run indent as needed to clean up code when you get messy.

  3. Do-While Loops
  4. The do-while loop was invented to give us a slightly different way to execute a block of code repeatedly. As you may recall the syntax of the while loop is given by:

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

    The do-while loop moves the logical expression after the block of code to be repeated. The do-while syntax is shown below:

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

    With a do-while, the block of code is executed before the logical expression is evaluated. If the logical expression is TRUE, the block of code is executed again, and the process repeats until the expression is FALSE. With a do-while it is possible to execute he block of code once even if the logical expression is false. As we will see, this program logic is useful for checking user input.

  5. Comparing while Loops and do-while Loops
  6. To understand the difference between while and do-while loops, we will experiment with two very simple programs.

    Step 1: Copy and paste the two programs given below into loop1.cpp and loop2.cpp respectively. Then use "g++ loop1.cpp -o loop1" and "g++ loop2.cpp -o loop2" to compile them. When you run loop1 and loop2 what is output?

    Step 2: Now edit the declaration of the variable "int i=1" and change it to "int i=4". Recompile and run both programs. How many times did the loops execute when you ran the programs?

    // loop1.cpp code
    #include <iostream>
    using namespace std;
    
    int main()
    {
       cout << "starting loop1\n"; 
       int i=1;
       do 
       {
          cout << "loop1\n";
          i++;
       }
       while(i < 4);
       return 0;
    }
    
    // loop2.cpp code
    #include <iostream>
    using namespace std;
    
    int main()
    {
       cout << "starting loop2\n"; 
       int i=1;
       while(i < 4)
       {
          cout << "loop2\n";
          i++;
       }
       return 0;
    }
    

    Copy and paste your program output below.

  7. Simple do-while Loop Program
  8. An example for the do-while loop is the program given below which prompts the user to enter an odd number. If the number entered is not odd, the user will be asked to input another number again. Create a file called loop3.cpp, and use "copy" and "paste" to copy the code into this file. Use g++ to compile this program. Now run the program and type in "12 2 7" and see what happens.

    // loop3.cpp code
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int num = 0;
       do
       {
          cout << "Enter an odd number ";
          cin >> num;
       }
       while((num % 2) != 1);
       cout << "num = " << num << endl;
       return 0;
    }
    

    Copy and paste your program output below.

  9. Modifying do-while Loops
  10. By looking at loop3.cpp, you may notice that "(num % 2) == 1" is used to check for odd numbers. Similarly, "(num % 2) == 0" can be used to check for even numbers. Create a new program called loop4.cpp based on loop3.cpp that will prompt the user to input TWO integers where the first integer is odd and the second integer is even. Use g++ to compile and debug this program. Test your program by typing different input values until the do-while loop terminates.

    Copy and paste your loop4.cpp program below.

    Copy and paste your program output below.

  11. For Loops
  12. The for loop was invented as a compact alternative to the while loop. In this case, the initialization statement, the logical expression, and the statement to update variables used in the logical expression are all placed on one line. This is followed by the block of code to be repeated. The C++ syntax of the while loop is:

    for (initialize variables; logical expression ; update variables) 
    {
       // block of statements to be repeated 
    }
    

    The order in which parts of the for loop are executed is as follows:

    1) initialize variables
    2) logical expression
    3) block of statements
    4) update variables

    In this case, steps 3 and 4 only take place if the expression 2 is TRUE. After step 4, the code repeats starting at step 2. This process continues until the logical expression becomes FALSE.

  13. Using for Loops
  14. Consider the simple for loop below. This is a compact version of the counting while loop we saw in the previous lab. Use "cut" and "paste" to copy this code into loop5.cpp, and compile and run this program. What do you see? Now modify the program to print out the values from 0 to 15.

    // loop5.cpp code
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int number = 10;
       for (int count = 1; count < number; count = count + 1)
          cout << count << endl;
       return 0;
    }
    

    Copy and paste your loop5.cpp program below.

  15. Nested for Loops
  16. Sometimes it is useful to put one for loop inside another for loop. We call these nested loops. In this case, the code in the entire inner loop is executed each time the outer loop is executed. Hence, if the outer loop executes m times, and the inner loop executes n times, the block of code in the inner loop will be executed m*n times. Consider the following program:

    // loop6.cpp code
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int size = 5;
       for (int outer = 1; outer <= size; outer = outer + 1)
       {
          cout << "outer = " << outer << endl;
          for (int inner = 1; inner <= size; inner = inner + 1)
             cout << "   inner = " << inner << endl;
       }
       return 0;
    }
    

    Step 1: Use "cut" and "paste" to copy this code into loop6.cpp, and compile and run this program. You should see a long list of numbers printed out.

    Step 2: Now modify your loop6.cpp program so all the values printed in the inner loop appear on one line, so you get the following:

    outer = 1
    1 2 3 4 5 
    outer = 2
    1 2 3 4 5 
    outer = 3
    1 2 3 4 5 
    outer = 4
    1 2 3 4 5 
    outer = 5
    1 2 3 4 5
    

    Step 3: Next, modify the program to output the product of the outer and inner variables inside the inner loop. This should produce an output like the following:

    1 2 3 4 5 
    2 4 6 8 10 
    3 6 9 12 15 
    4 8 12 16 20 
    5 10 15 20 25
    

    Step 4: Finally, modify your program to output a 12x12 table of products like the one you used in elementary school to learn multiplication. To make the output look nice, you could try using "\t" (tab) between the values you print out.

    Copy and paste your loop6.cpp program below.

    Copy and paste your program output below.

  17. Submit Work
  18. 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: