CSCE 2004 - Laboratory Assignment 3

The objective of this laboratory assignment is to work through several small C++ programs to give students exposure to program debugging techniques, if-else statements, and switch statements. This assignment has the following steps:

Submitting Assignment

Download the text file called Lab Assignment 3 Solutions. Complete the details asked for at the top of the text file. For the pre-lab, enter your choice (e.g. A, B, C or D) next to each question. For the in-lab, when asked to copy and paste your code and output, do so into the correct section in the text file.

Pre-lab Assignment

The pre-lab must be completed before coming to the lab session.

    Question 1: What is the error in following block of code:

       if (x = 100)
          cout << "x is 100";
    A. There is no semicolon at the end of first line.
    B. The equals to operator is incorrect.
    C. Variable x should not be inside quotation marks.
    D. None of the above.

    Question 2: Consider the following two pieces of code and choose the best answer.

    Code 1:

       switch (a)
       {
          case  1:
             cout <<"a is 100";
             break;
          case 2:
             cout <<"a is 200";
             break;
          default:
             cout <<"The value of a is unknown";
       }
    Code 2:
       if (a==100)
       {
          cout <<"a is 100";
       }
       else if (a==200)
       {
          cout << "a is 200";
       }
       else
       {
          cout <<"The value of a is unknown";
       }
    A. Both Code 1 and Code 2 have the same behaviour.
    B. Both Code 1 and Code 2 produce different effects.
    C. Code 1 and Code 2 sometimes produce the same results.
    D. None of the above.

    Question 3: Examine the following block of code and determine what happens when y=2?

       switch (y)
       {
          case 1:
          case 2:
          case 3:
             cout<< "y is 3";
             break;
          default:
             cout<<"The value of y is unknown";
             break;
       }
    A. Will display 'y is 3'.
    B. Will display 'The value of y is unknown'.
    C. Program jumps to the end of switch statement since there is nothing to do for x=2.
    D. None of the above.

    Question 4: Examine the following block of code and determine what happens when x=65?

       if (x < 25)
          cout<< "Red";
       else if (x < 50)
          cout<< "Blue";
       else if (x < 75)
          cout<< "Green";
       else
          cout<< "Purple";
    A. Will display 'Purple'.
    B. Will display 'Green'.
    C. Program jumps to the end of the nested if-else statement since there is nothing to do for x=65.
    D. None of the above.

In-Lab Assignment

The in-lab must be completed during the lab. Make sure you have uploaded and submitted the Lab Assignment 3 Solutions text file before the end of the lab.

  1. Program Debugging
  2. There are numerous opportunities for error in any program. Look at the following program. What do you think it is supposed to do?

    #include <iostream>
    using namespace std;
    
    int main()
    {
          const double HEIGHT = 3.0;  /* inches *\
          double CubeVolume = HEIGHT * HEIGHT * HEIGHT;
          double SurfaceArea = 8 * HEIGHT
    
          cout << "Volume = " CubeVolume "\n";
          cout << "Surface area = << SurfaceArea << "\n";
    
          return 0;
    }
    

    Create a C++ program called Cube.cpp, and use "cut" and "paste" to copy the text above into this file. What errors do you get when you try to compile the program? Before you start correcting these errors, edit the file to add comments describing the purpose of the program, the author, and date at the top of Cube.cpp.

    Now, start entering your corrections to make the compile errors go away. As a general rule, it is best to start with the first error message and recompile until that error is corrected. For each correction, make a comment near the code you corrected so you can remember what you did. These comments are very important when you work on joint programming projects so everyone can keep track of changes to the code by reading the comments.

    Once the compilation errors are gone, check that the program is doing the right thing. Is it correctly calculating the values? Are there any logic errors?

    Copy the corrected code and its output into the Solutions File As Part 1.

  3. Conditional Statements
  4. Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.

    In C++ we have three conditional statements:

    If Statement

    You should use the if statement if you want to execute some code if a condition is true.

    
       if (condition)
       {
          code to be executed if condition is true    
       }
    

    If - Else Statement

    You should use the if-else statement if you want to execute one piece of code if a condition is true, and a different piece of code if the condition is false.

    
       if (condition)
       {
          code to be executed if condition is true    
       }
       else
       {
          code to be executed if condition is false    
       }
    

    Nested If - Else Statement

    The If- Else conditional statement is used to execute one of the two alternatives. Sometimes, your program will need to execute more than one alternative. In such cases Nested If-Else statements are used.

    
       if (Condition1)
       {
          Statement1
       }
       else if (Condition2)
       {
          Statement2
       }
       .
       .
       .
       else if (Condition(n-1))
       {
          Statement(n-1)
       }
       else
       {
           Statementn
       }
    

    The runtime system checks Condition1. If Condition1 is true, it will execute Statement1. If Condition1 is false, then the computer will check Condition2. If Condition2 is true, it will execute Statement2. Whenever the computer finds a condition to be true, it will execute its corresponding statement. If that condition is false, the compiler will check the subsequent condition. This means you can include as many conditions as you see fit using the else if statement. If after examining all the known possible conditions you still think that there might be an unexpected condition, you can use the optional else.

  5. Write a Program using If - Else statement
  6. Write a small program based on the above syntax that accepts the time of the day from the user and, based on the time, prints a greeting. The time should be read in as an integer, the hour (from 0 - 23).
    Print "Good Afternoon" for hours from 12 - 4.
    Print "Good Evening" for hours between 5 - 11 or 0 - 5.
    Print "Good Morning for hours from 6 - 11.
    You do not need to do error checking.

    Optional: Extend the program to include error checking for hours outside 0 - 23. Also, consider having the user enter minutes. You could also do the time in am and pm instead of the 24 hour military time.

    Copy the corrected code and its output into the Solutions File As Part 2.

  7. Switch Conditional Statement
  8. You should use the switch statement if you want to select one of many blocks of code to be executed based on the value stored in a single variable. Pay particular attention to format the indentation so that the code is readable.

    
       switch ( variable)
       {
          case value1:
             code to be executed if variable == value1;
             break;
          case value2:
             code to be executed if variable == value2;
             break;
          case value3:
             code to be executed if variable == value3;
             break;
          default:
             code to be executed if variable does not match any of the values;    
             break;  // optional
       }
    

    First we have a variable whose value we want to use to select different actions. The contents of this variable is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. We use break statement to prevent the code from running into the next case automatically. Since the switch statement ends right after the default case, the break is not needed.

  9. Write a Program
  10. Write a program based on the above syntax that accepts the number of the day of the week and displays the name of the corresponding day. Assume Day 1 is Monday, Day 2 is Tuesday, and so on. For example if you have entered 6 as your day number, the program should display "The day is Saturday." as the output. Think of the design for this program. Discuss it with the person sitting next to you or with your TA.

    Copy the corrected code and its output into the Solutions File As Part 3.