Lab 2 - Basic Data Types and Flow Control

Due: Midnight on the day of the lab

Complete the following programs. Email both source code files to my Sleipnir account to turn in your work for this lab. Each program is worth 5 points.

  1. Write a program that asks the user to enter an integer and then a double. Read what the user types into variables, then echo the contents of the variables to the screen. Finally, calculate the sum of the two numbers and print the results to the screen.

    The program output should appear similar to the following:

    Enter an integer: 9
    Enter a double: 5.3
    You entered the integer 9 and the double 5.3.
    The sum is 14.3.
    
    Save this program as the filename lab2_1.cpp. Compile and run it using the following Sleipnir commands:
    g++ -o lab2_1 lab2_1.cpp
    ./lab2_1
    
    Note that lab2_1.cpp is your source code file and lab2_1 is your executable file.

    Run the program a few times (repeat the ./lab2_1 command) and experiment with giving different input such as entering a double instead of an integer for prompt 1 and an integer instead of a double for prompt 2. Also try entering both numbers at once with just a space between them instead of hitting enter after each number. Finally, try this command from your Sleipnir prompt:

    echo "5 1.3" | ./lab2_1
    Note how the numbers you gave to echo are used as input to the program.
  2. Copy the program you just made using the Sleipnir command:
    cp lab2_1.cpp lab2_2.cpp
    
    Open lab2_2.cpp in pico or vi and remove the "You entered" and "The sum is" output lines. Replace these lines with an if-else statement which compares the two numbers and outputs the maximum of the two.

    The output should now appear as follows:

    Enter an integer: 9
    Enter a double: 5.3
    The max is 9.
    
    Compile and run using the Sleipnir commands:
    g++ -o lab2_2 lab2_2.cpp
    ./lab2_2