Lab 2 - Basic data types
The following sample programs in the book will be helpful to look at to
complete this lab. You can look at the programs in the book or on the book's
resource CD.
- Display 1.8 (p21) shows how to declare integers, read in values from
the user and print out integers.
- Display 2.1 (p41-42) shows how to use both integers and doubles.
You can also look at the sample program
addition.cpp by Dr. Marc Thomas, who is teaching the
other section of CS221.
Complete the following programs. Email both files to my Helios account.
-
Write a program that asks the user to enter an integer and then a double.
Then echo the entered data to the screen. The interaction should appear as
follows:
Enter an integer:
9
Enter a double:
5.3
You entered the integer 9 and the double 5.3.
Save this program as the filename lab2_1.cpp. Run the program a few times
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
Helios prompt: echo "5 1.3" | a.out
Substitute a.out for the name of your executable if you compiled with the
-o option.
-
Copy the program you just made and do the following modifications. Remove
the "You entered" line and instead output the following calculations, one
output per line:
- The integer divided by the integer 10.
- The double divided by the integer 10.
- The integer divided by the double.
The output should appears as follows:
Enter an integer:
9
Enter a double:
5.3
The value of 9 / 10 is 0.
The value of 5.3 / 10 is 0.53.
The value of 9 / 5.3 is 1.69811.
Save this program as the filename lab2_2.cpp