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:

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

  3. Program Debugging
  4. 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;
    }
    

    Step 1: 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.

    Step 2: 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.

    Step 3: Once the compilation errors are gone, run the program to check that the program is doing the right thing. Errors that occur when a program is running are called "run time errors" or "logic errors". Based on the variable names and the formulas, is the code correctly calculating the output values? If not, edit the program to fix these logic errors and recompile.

    Copy and paste your final program below.

    Copy and paste your program output below.

  5. Conditional Statements
  6. 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 kinds of conditional statements:

    If Statement

    You should use the if statement if you want to execute some code if a condition is true. The syntax of this command is shown below:

    if (condition)
    {
       code to execute if condition is true 
    }

    If-Else Statement

    You should use the if-else statement if you want to execute one block of code if a condition is true, and a different block of code if the condition is false. The syntax of this command is shown below:

    if (condition)
    {
       code to execute if condition is true 
    }
    else
    {
       code to execute 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 consider more than two alternatives. In such cases nested if-else statements are used. The syntax of this command is shown below:

    if (condition1)
    {
       code to execute if condition1 is true 
    }
    else if (condition2)
    {
       code to execute if condition2 is true 
    }
    .
    .
    .
    else if (conditionN)
    {
       code to execute if conditionN is true 
    } 
    else
    {
       code to execute if none of the conditions are true 
    }
    

    When the nested if-else statement is executed, the program evaluates the conditions one at a time from top to bottom, and when it finds a condition that is true, it executes the following block of code. After executing this code, the program jumps to the end of the nested if-else statement. If none of the conditions are true, the code following the final else will be executed.

  7. Modify a Program Using Conditional Statements
  8. Consider the following C++ program. It reads an integer from the user and then uses nested if-else statements to execute different blocks of code depending on which conditions are true. Notice that when the block of code following a condition is only ONE line long, you do NOT need to use the { } brackets.

    #include <iostream>
    using namespace std;
     
    int main()
    {
       // Read test score
       float Score = 0;
       cout << "Enter test score: ";
       cin >> Score;
    
       // Check test score
       if (Score > 100)
          cout << "You are better than perfect!\n";
       else if (Score >= 90)
          cout << "You get an A\n";
       else 
          cout << "You are something else\n";
       return 0;
    }
    

    Step 1: Create a program called "grade.cpp" and cut and paste the code above into your file. Compile and run the program to see what it does with different input values. Hint: When you are testing a program it is a good idea to see what happens when you enter any of the constants in the program (100 and 90 in this case) and values that are +1 and -1 of these values. These are called the "boundary conditions".

    Step 2: Now extend the program to print out different messages if the score is in the B range, the C range, the D range, or the F range using the grading scale for this class. You can print any messages you want. When the program is finished, run it several times to verify that each of your if-else conditions is working properly.

    Copy and paste your final program below.

    Copy and paste your program output below.

  9. Write a Program Using Conditional Statements
  10. The goal of this section is for students to write a program using conditional statements from scratch using an incremental development approach.

    Step 1: Create a new C++ program called "greeting.cpp" and type in (or copy from an example above) the basic program structure. Now compile this empty program and debug as needed until you have a working program.

    Step 2: Modify your program so it prompts the user to enter the hour of the day using a 24 hour clock (i.e. an integer value between 0..23). Based on the value the user types in, your program should use conditional statements print a greeting. Your program should print "Good Morning" for one range of times, "Good Afternoon" for another range of times, and "Good Evening" for a final range of times. It is up to you to work out what ranges of times are appropriate and create the corresponding boolean conditions.

    Step 3: Remember to implement your program incrementally and compile and debug frequently. For example, just work on printing one message above, then add code for the second message, then add code for the third message. Test your final program with a variety of input values to test the "boundary conditions" and save the results.

    Optional: If you are feeling creative, you could extend your program to perform error checking in case the user enters times outside the 0..23 range. You are welcome to make up any error messages you want.

    Copy and paste your final program below.

    Copy and paste your program output below.

  11. Switch Conditional Statement
  12. The switch statement can be used to select among many blocks of code based on the value of a single integer or character variable (floating point variables and strings will not work). The syntax of the switch statement is shown below:

    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;  
    } 
    

    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 switch statement. If there is a match, the block of code associated with that case is executed. We use the "break" statement to jump to the end of the switch statement. If you omit the "break" the program will start executing the code in the following case, which is seldom what you want.

  13. Write a Program Using Switch Statements
  14. Consider the C++ program below. It reads an integer value from the user and prints out the day of the week where 1=Sunday, 2=Tuesday, ... 7=Saturday.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       // Read day number
       int Day = 0;
       cout << "Enter day number: ";
       cin >> Day;
    
       // Print day of week
       if (Day == 1)
          cout << "Sunday\n";
       else if (Day == 2)
          cout << "Monday\n";
       else if (Day == 3)
          cout << "Tuesday\n";
       else if (Day == 4)
          cout << "Wednesday\n";
       else if (Day == 5)
          cout << "Thursday\n";
       else if (Day == 6)
          cout << "Friday\n";
       else if (Day == 7)
          cout << "Saturday\n";
       else 
          cout << "Error\n";
       return 0;
    }
    

    Step 1: Your task is to use a switch statement to implement a new version of this program. Start by creating a program called "day.cpp" and cut and paste the code above into this file. Then compile and run the program. Test it with a number of input values to verify that it is working correctly.

    Step 2: Make a copy of your day.cpp program called "day2.cpp". Now replace the nested if-else code with the corresponding switch statement. Remember to put the "break" statements at the bottom of each "case". Recompile and debug your program until it is working correctly. Congratulations you are finished!

    Copy and paste your final program below.

    Copy and paste your program output below.

  15. Submit Work
  16. 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: