CSCE 2004 - Laboratory Assignment 2

The objective of this laboratory assignment is to work through several small C++ programs to give students exposure to data types, arithmetic operations, and if-else statements. This assignment has the following steps:

  1. Getting Started
  2. Refer to the instruction in Lab #1 to login, start Netbeans, create C++ programs, and compile and execute C++ code as needed below.

  3. Data types and arithmetic operations
  4. A data type is a set of values and a set of operations on those values. The program shows some main data types and arithmetic operators in C++ language. Try to compile and execute this program and see the printed results.

    
    #include <iostream>
    using namespace std;
    
    int main()
    {
            // Local variable declarations
            int number1 = 100;
            int number2 = 3;
    
            int sum;
            int difference;
            int quotient;
            int remainder;
            int product;
    
            // Assignment statements
            sum = number1 + number2;
            difference = number1 - number2;
            quotient = number1 / number2;
            remainder = number1 % number2;
            product = number1 * number2;
    
            // Print results
            cout << "The sum is " << sum << "\n";
            cout << "The difference is " << difference << "\n";
            cout << "The quotient is " << quotient << "\n";
            cout << "The remainder is " << remainder << "\n";
            cout << "The product is " << product << "\n";
    
            return 0;
    }
    

    A question for yourself: Does any of the output look a little off?

  5. Modifying data types
  6. Modify the data types and values of some variables in this program. Then, try to compile again and re-execute the program.

    
            ... //above this line, same as before
            float number1=100.00; //try running this program a few times with different values here!
            float number2=3.00; //try running this program a few times with different values here!
    
            float sum;
            float difference;
            float quotient;
            float product;
    
            // Assignment statements
            // Delete the "remainder" statement because mod (the % operator) is undefined on floats!
            ...
    	// Edit your program above this line
    	
    
            // Print results
            cout << "The sum is " << sum << "\n";
            cout << "The difference is " << difference << "\n";
            cout << "The quotient is " << quotient << "\n";
            cout << "The product is " << product << "\n";
            .... //below this line, same as before
    
    

    A question for yourself: Compare your output this time (using number1=100.00; and number2=3.00;) with the output from the previous program.

    Did you get different output anywhere? Why is that?

  7. Conditional Statements
  8. Very often when you write code, you want to perform different actions for different decisions. (Hey! Homework #1 is a great example of this!) 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. Here's an example with n different alternatives.

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

    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 and then skip the remainder of the If-Else block. If a given condition is false, the compiler will check the subsequent condition (and so on).

    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.

  9. Write a small program using If - Else statements
  10. Write a small program based on the above syntax that accepts the time of the day from the user and based on the time returns a greeting. For example, if the time is less than 12:00pm then the greeting returned should be "Good Morning" and likewise "Good Afternoon" or "Good Evening".

    (Remember: In Netbeans, you will want to start a brand new project for this, and every new program!)

    Think of the design for this small program. Discuss it with the person sitting next to you or with your TA. You need to figure out how to accept the time of the day from the user. There are many options here. You can prompt the user for hour, minute, and 'am' 'pm' separately. Or you can read in time in military time where the hour goes from 0 to 23 so you don't need the 'am' 'pm'.

    There is more than one correct way to do this (of course, some ways can be better than others...). Be creative!

  11. Submit Work
  12. To submit your lab work, have your TA check over any code/output that you've done during this lab session and record that you completed the lab assignment.

    Important Note: It is your responsibility, not the TA's, to ensure that your lab is recorded as being completed before you leave! Although the labs are intended to be completed in the alloted lab time, if you need additional time to finish the lab assignment, you have 24 hours from the end of the lab meeting time to finish. If so, you should ask your TA for instructions on how to submit the lab assignment after the lab ends.

  13. Logout of the System

    After making sure that you submitted the code correctly and you have done the entire assignment, logout of the system by clicking on the menu button in the lower left hand side of the desktop. Then, click on Log Off. Wait a few seconds for a window to pop up and then click Log Off.