Assignment #2 - Using Selection Statements
Due: Tues, Sept 27
Objective
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:
(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
Filename: wave.cpp
This is based on Programming Challenge 18 in chapter 4 of your
textbook. This table shows the approximate speed of sound in air, water,
and steel.
Medium | Speed |
Air | 1,100 feet per second |
Water | 4,900 feet per second |
Steel | 16,400 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.
- 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 is underlined, to distinguish it from output)
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
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 for both programs
- 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:
Submit only your source code files through the web submission page.
As there are two of them, this will require filling out the
submission page twice. Make sure to click on "Assigment 2" both
times. Submit the files:
sort.cpp
wave.cpp