Programming Assignment 2 Due February 9, 2015 at 11:59 PM Objectives

advertisement
Programming Assignment 2
Due February 9, 2015 at 11:59 PM
Objectives
This assignment will consist of writing two small programs, which will give practice with basic
selection statements, as well as further practice with basic I/O.
Task
Write the following programs, each in a separate file. Filenames should be:
• sort.cpp
• wave.cpp
(Note that the filenames are all lowercase)
Exercise 1
Write a program (filename sort.cpp) that does the following:
Prompt the user and let them enter three integers. Store them in three variables. Print the
numbers in sorted order, from smallest to largest.
Sample run: (user input underlined)
Input integer 1 : 34
Input integer 2 : 200
Input integer 3 : -14
Sorted : -14 <= 34 <= 200
Exercise 2
This table shows the approximate speed of sound in air, water, and steel.
Medium Speed
Air
1100 feet per second
Water
4900 feet per second
Steel
16400 feet per second
Write a program that does the following:
• Display a menu allowing the user to select air, water, or steel. Prompt the user and allow
them to enter a choice. User entry should be a character: ’A’ for air, ’W’ for water, and ’S’
for steel. Allow both uppercase and lowercase as valid entries.
• If the user enters an invalid choice, print an appropriate error message and end the program.
For a valid choice, now ask the user to enter the distance that a sound wave will travel through
the selected medium. (Use type double for this entry).
• If the selected distance is negative, this is an invalid choice, so print an error message and
end the program.
1
• For a valid distance, compute and display the amount of time it will take for the sound wave
to travel the given distance, rounded to 4 decimal places.
Sample runs: (user input underlined)
Sample Run 1
Welcome to Sound Wave Calculator
Select the medium the sound wave will travel through.
A
Air
W
Water
S
Steel
> A
How far will the sound wave travel (in feet)? 3500
The sound wave will travel 3.1818 seconds
Goodbye
Sample Run 2
Welcome to Sound Wave Calculator
Select the medium the sound wave will travel through.
A
Air
W
Water
S
Steel
> w
How far will the sound wave travel (in feet)? 3500
The sound wave will travel 0.7143 seconds
Goodbye
Sample Run 3
Welcome to Sound Wave Calculator
Select the medium the sound wave will travel through.
A
Air
W
Water
S
Steel
> s
How far will the sound wave travel (in feet)? 98765
The sound wave will travel 6.0223 seconds
Goodbye
Sample Run 4
Welcome to Sound Wave Calculator
2
Select the medium the sound wave will travel through.
A
Air
W
Water
S
Steel
> z
Illegal entry. Aborting program.
Goodbye
Sample Run 5
Welcome to Sound Wave Calculator
Select the medium the sound wave will travel through.
A
Air
W
Water
S
Steel
> S
How far will the sound wave travel (in feet)? -10
Distance cannot be negative. Aborting program.
Goodbye
Requirements
• No global variables, other than constants.
• All input and output must be done with streams, using the library iostream.
• You may only use the iostream library (you do not need any others for these tasks).
• When you write source code, make sure it is clear, readable, and well-documented.
Submitting
Program submissions should be done through Blackboard under the Assignments tab. Make sure
you submit both .cpp files. Program submissions are due February 9 at 11:59 PM.
3
Download