CSCE 2004 - Laboratory Assignment 5

The objective of this lab is to understand more about performing iterations in C++. We have worked with the for loop and the while loop. Now we familiarize ourselves with the do-while loop. This assignment also illustrates the use of nested loops. Though in general one can inter-change the types of loops nested (i.e one can have an inner while-loop and an outer for-loop), in this lab we use for loops nested within another for loop.

 

 

Pre-Lab Assignment

 

What will the following program segments display?

1)     int Sum = 0;

for (int count = 0; count < 6; count++)

    Sum = Sum + count;

           cout << (count + count);

 

A.   11.

B.    12.

C.    10

D.   15

 2) for (int value = -5; value < -5; value++)

          cout << value;

 

A.   None.

B.    -25

C.    0

D.   10

 

3) int x = 0;

    int i=0;

    while ( i <= 3)

        x= x + i;

    i++;

 

A.   None.

B.    6.

C.    8.

D.   10

 

In-Lab Assignment

 

1.     do-while loops

The general format of the do-while loop statement is

do

    statement

while(Boolean expression);

  

 

The do-while loop puts the Boolean expression after the body of loop, therefore, the body will always be executed at least once. If the Boolean expression is TRUE, the loop will continue and is useful for selected applications. To understand the difference between while and do-while execute the programs given below. copy and paste the two programs given below and run them.

 

        /* loop1.cpp code */

        #include <iostream>

        using namespace std;

        int main()

        {

               int i;

               i=4;

                do

                {

                  cout << "great\n";

                }while(i < 4);

                return 0;

         }

   

        /* loop2.cpp code */

        #include <iostream>

        using namespace std;

        int main()

        {

               int i;

               i=4;

               while(i < 4)

                  cout << "great\n";

        return (0);

        }

 

What did your programs output? Try explaining the possible reason for difference in results between the two programs.

 

Copy the output of the two programs the reasons of the difference into the solutions file as Part1.

 

2.     Simple do-while loop program

An example for the do-while loop is the program given below which scan an odd number. This code prompts the user to enter an odd number. If the number entered is not odd, the user will be required to input another number again.

 

 /* loop3.cpp code */

 #include <iostream>

 using namespace std;

 

        int main()

        {

               int num;

               do

                {

                  cout << "Enter an odd number ";

                  cin >> num;

                } while((num%2)!=1);

           return 0;

         }

 

 

Copy the program and past it to NetBeans.

 

Copy and paste the output to the solutions file as Part 2.

 

3.     Modifying do-while loops

By looking at loop3.cpp, you may notice that [(Odd number) % 2 = 1 ] and [(even number) % 2 = 0]. Create a new program that will scan pairs of integers until it reaches a pair in which the first integer evenly divides the second. Use g++ to compile and debug this program.

 

Copy and paste the code and the output to the solutions file as Part 3.

  

4.     Nested Loops

As you have seen, for loop performs the sequence of operations inside the { } brackets multiple times, and stops when the Boolean expression is false. Sometimes it is helpful to have one for-loop operates inside another for loop. The syntax of the nested for loop is given below:

 

   for (initializations 1;Boolean condition 1;actions 1)

   {

      for (initializations 2;Boolean condition 2;actions 2)

      {

         block of code

      }

   }

 

If we use counting loops above, and the outer loop executes m times, and the inner loop executes n times, the block of code will be executed m*n times. Keeping this in mind, see if you can write a small program to generate the following output:

 

0 2 4 6 8 10

0 2 4 6 8

0 2 4 6

0 2 4

0 2

0

  

Copy and paste the code and the output to the solutions file as Part 4.

 

5.      Introduction to Functions

 

A. The following simple code is a simple introduction about using functions in c++. The function Sum takes two values and returns the sum of them. Copy and paste the program in to NetBeans and run it.

 

#include <iostream>

#include<iomanip>

 

using namespace std;

 

double Sum (double A, double B)

{

    return A+B;

}

 

int main()

{

    double Total = 0.0, Num1 = 0.0 , Num2 =0.0;

   

    cout << "Please, enter two values to sum them up \n";

    cin >> Num1>>Num2;

    Total = Sum(Num1, Num2);

    cout << "The Sum of the two values are: " << fixed

         << setprecision(2) << Total << endl;

   

    return 0;

}

 

Copy and paste the code and the output to the solutions file as Part 5A.

 

B. Modify the previous program by adding a function that returns the multiplication of three numbers.

 

Copy and paste the code and the output to the solutions file as Part 5B.

 

6.      (BONUS 2 points) More Nested Loops - Enumeration

The following program is another illustration of nested for-loops and their utility. This program performs a simple and useful task called enumeration. To enumerate means to list all members of a particular set. For example, if the given set contains X and Y, we can enumerate four different two-character strings: XX, XY, YX, YY. This case is illustrated by the following program.

 

/*-------------------------------------------------------------------

* Program that performs the task of enumeration. To enumerate means to

* list all members belonging to a predefined set. Given a set that

* contains X and Y the following program enumerates four different

* two character sets XX, XY, YX and YY.

 

*-------------------------------------------------------------------*/

 

#include <iostream>

using namespace std;

int main()

{

        char index1, index2; /* For-loop counters */

        int counter = 0; /* Overall loop counter */

        /* Outer for-loop */

        for (index1 = 'X'; index1 <= 'Y'; index1++)

        {

               /* inner for-loop */

               for (index2 = 'X'; index2 <= 'Y'; index2++)

               {

                        counter++ ; 

                        cout << index1 << index2 << "\n";

               } /* end of inner for-loop */

        } /* end of outer for-loop */

        cout << "\nThere are " << counter << "different strings\n";

        return 0;

}

  

Extend this program to enumerate all THREE character strings using THREE different characters i.e. X, Y and Z (i.e. XXX, XXY, ... ZZZ). How many such strings are generated?

Copy and paste the code and the output to the solutions file as Part 6.