Lab3

Home Up

 

 Lab1 ] Lab2 ] [ Lab3 ] lab4 ] lab5 ] lab6 ] Lab7 ] Lab8 ] Lab9 ] Lab10 ]

CP107 LAB 3

 

LAB OBJECTIVES:

Formulating algorithms, use of

use if/else and if statements  

use switch statements

use while and do while repetition structures

For loop, repetition - using counter controlled repetition

break and continue statements

 

PRE-LAB ASSIGNMENTS and LAB ASSIGNMENTS.

NOTE: Pre-Labs are not graded, lab assignments are graded.

PRE-LAB 1)   Open and run the nestloop program.  If single stepping, you may want to change each cout statement to cerr.

// nestloop.cpp
// demonstrates nested for loops
#include <iostream>

using namespace std; 

void main(void)
{
    int i,j;
    cout << "BEGIN\n";
    for(i = 1; i <= 3; i++)
    {
        cout << " Outer loop: i = " << i << '\n';
        for(j = 1; j <= 4; j++)
            cout << "     Inner loop: j = " << j << '\n';
    } // end of outer for
    cout << "END\n";
}  // end of program

 

PRE-LAB 2)         Open and run fill.cpp.

// fill.cpp
// demonstrates nested for loops
#include <iostream>
using namespace std;

void main(void)
{
    int col, row ;
    cerr << endl << endl ;
    for (row = 1; row <=12; row++)
    {
        cerr << endl ;
        for ( col = 1; col <=20 ; col++)
            cerr << "*" ;
    } // end of outer for
    cerr << endl << endl ;
}

Keep in mind that the initialization and conditional statements in each loop can be modified to make other patterns appear as output.  Try single stepping (step over with a watch window for local variables) through the outer and inner loops while observing how the output is created.  Try changing the values of 12 or 20 and observe the results.  The inner initialization (col = 1;) can use the current value of row (for example, col = row  + 1 ; ) as well as the conditional statement ( col <= 20 ) can be changed to include the value of row in the comparison. (for example, col < row – 2; )  Experiment with changing either the initialization and/or the conditional test on the inner loop and observe the results.

 

PRE-LAB 3)            Open the binary.cpp file and run it.       

// binary.cpp

// Using nested for loops, display binary values from zero to fifteen

#include<iostream>
using namespace std;

void main(void)
{

#include<iostream>
using namespace std;

void main(void)
{
#include<iostream>
using namespace std;

void main(void)
{
  int i,j,k,l;
  int sum = 0;

  cout << "i j k l\n\n";
  for (i = 0; i <= 1; i++)
  {
    for (j = 0; j <= 1; j++)
    {   
        for (k = 0; k <= 1; k++)
        {
            for (l = 0; l <= 1; l++)
            {
              cout << i << ' ' << j << ' ' << k << ' ' << l << "\t \t" << sum++ << '\n';
            }
        }
    }
  }

}

PRE-LAB 4)   Open and run continue.cpp

// continue.cpp
// demonstrates continue and break statements
#include <iostream>
using namespace std;

void main(void)
{
   
int i;
    for(i = 1; i <= 10; i++)
    {
        if (i = = 5)
            continue;
   
         cout << i << '\n';
        if ( i = = 9)
        {
            cout << "time to break" << endl;
            break;
        } // end of if
    } // end of for

}  // end of program

 

PRE-LAB 5)  Open and run the reps program.  Enter some invalid data to cause the nested loop to repeat.

// reps.cpp  for loop, do while and switch case demo

#include <iostream>
using namespace std;
const char ERROR = '\0';

void main(void)
{
    int num_reps;
    int i;         // counter used by for loop
    int democrats = 0, republicans = 0, independents = 0;
    int reforms = 0 , libertarians = 0 ;
    char party;
    cout << "\nHow many U.S. Representatives does your state have? ";
    cin  >> num_reps;  // ask user for number of representatives
    cout << "Enter the party affiliation for each Representative.\n";
    cout << "Enter D for Democrat, R for Republican,\n";
    cout << "F for reform, L for Libertarian,\n";
    cout << "and I for independents or other parties.\n";
    for (i = 1; i <= num_reps; i++)
    {
        do
        {
          cout << "Party of representative #" << i << ": ";
          cin  >> party;
              switch(party)
              {
                  case 'D':
                  case 'd':         // if democrat,
                      democrats++;     // increment democrats counter
                      break;
                  case 'R':
                  case 'r':         // if republican,
                      republicans++;   // increment republicans counter
                      break;
                  case 'I':
                  case 'i':         // if independent
                      independents++;  // increment independents counter
                      break;
                  case 'F':
                  case 'f' :        // if reform
                     reforms++ ;   // increment counter
                     break;
                  case 'L':
                  case 'l':        // if libertarian
                      libertarians++;  // increment counter
                      break;
                  default:
                     cout << "Invalid entry. Enter D,R,F,L or I. \n";
   
                              party = ERROR;
                     break;
             } // end of switch structure
        } while (party = = ERROR); // loop again if invalid choice is made
    } // end of for loop
    cout << "\nState rep party representation is: \n" << democrats << " Democrat, \n"
        << republicans << " Republican,\n" << independents
        << " independent, \n" << reforms << " Reform, and \n"  << libertarians <<
            "Libertarian \n\n" ;

} // end of program

 

LAB ASSIGNMENT: 2 Problems

 

1.         Write a program that will produce the following output on screen:

            X                                                                     X

                        X                                             X

                                    X                     X

                                                X

                                    X                     X

                        X                                             X

            X                                                                     X

 

Assume that the first row begins at the left edge of the screen and the last X in the first row is separated from the first X by 5 spaces.  You may NOT use tabs or gotoxy for positioning.  You MUST use a for loop or nested for loops.  The only legal cout statements that you can use to produce the big X are:

                        cout  <<  'X' ;

or

                        cout  <<  '  ;           // one space surrounded by quotes

or

                        cout  <<  endl ;

If you are not sure of the specs, please ask.

Submit your program as  bigx.cpp

 

Lab 2.    
A mail order house sells five different products whose retail prices are:
1-- $2.89,   2 -- $4.50,   3 -- $9.98,   4 -- $4.49 and product 5 -- $6.87.  Write a program that reads a series of pairs of number as follows:

a) Product
b) Quantity  sold for one day

Your program should use a switch statement to help determine the retail price for each product. Your program should calculate and display the total retail value of all products sold.

Submit your program as  sales.cpp

Use a while loop with a sentinel value for loop. Use a switch statement for decision making.
Keep reading a pair of numbers. If the first number is NOT -1 (sentinel) value, then read the second value. First value read is the product, second value read is the number for quantities sold. In your switch/case, you should calculate that sales of the product.

Example:

    case 1:
        TotalSalesProduct1 = TotalSalesProduct1  + (2.89 * quantitySold);

 

 

 

 

Copyright (c) Yusuf Family Website 2001