CMPT
130 Lab 2 - Decisions
In this lab
you are going to get some practice writing if statements. As usual you should use a text editor and the
Terminal to create and run your program.
Labs are assessed
so make sure that the TA has seen, and marked, your finished work before you
leave the lab.
Introduction
Write a
program that prompts the user to enter three integer values and that then
prints those values in ascending order, one number per line. I'm giving you
part of the program, you just have to complete the user input section and the
part that actually prints the values in order.
Program
#include <iostream>
using namespace std;
int main()
{
int num1,
num2, num3;
num1 = num2 = num3 = 0;
char next
= 'y';
while (next != 'n'
&& next != 'N') {
cout << "Enter
first value: ";
cin >> num1;
// Your work goes here!
cout << endl << num1
<< endl << num2
<< endl << num3
<< endl << endl;
cout << "Do
you want to continue? Enter n to stop, any other key to continue: ";
cin >>
next;
cout << endl;
}
return 0;
}
Test the
Program
Copy and
paste the program give above into your text editor. Save it as numbers.cpp. Then open the terminal and run
g++ -o
numbers numbers.cpp to compile the program. Run it (by entering ./numbers). You can
see that the program gets user input for the first number and then prints the
three variables. The program includes a while loop that allows the user to do
this multiple times – this will make testing your program less annoying!
Complete
the Input Section
Complete the
user input section by getting input from the user for the other two variables.
Then compile and run the program again. The output from the program should look
like this.
Enter first
value: 8
Enter second
value: 5
Enter third
value: 17
8
5
17
Do you want to
continue? Enter n to stop, any other key to continue: n
Version
1
Now that you've
got the program running and have completed the user input section you can write
the if statements to print the three
numbers in the correct order. Before you start any programming task it's
worthwhile thinking about what you are doing first. For a more complex program
this can be a very time-consuming process but since the lab is only an hour
long I'll keep this part short.
Analysis
Your program
is going to be given three numbers in any order and you have to print them in
ascending order (one number per line). There are 3!, or
6, possible orderings of the three values from lowest to highest.
num1, num2, num3
num1, num3, num2
num2, num1, num3
num2, num3, num1
num3, num1, num2
num3, num2, num1
Your program
is going to have to recognize each of these. This means your if statement is
going to have to have at least five different conditions. Why not six? Because
the last one could be an else clause.
Details
I've already given you a line of code that will print the three
values.
cout << endl << num1
<< endl << num2
<< endl << num3
<< endl << endl;
You can now write an if
statement, and make the line given to you above the body of that if statement, except that you print the
three variables in a different order for each of your six conditions.
For this
version I want you to write a single condition for each of the six possible
orderings. These conditions should contain multiple comparisons joined together
by && (and) operators. Each condition should correspond to one of the
orders shown above in the analysis section.
As a hint
let's look at the first order where num1
is less than both of num2 and num3, and num2 is less then num3.
This looks like it entails writing a condition with three comparisons joined by
ands: num1 less or equal to than num2 and num1 is less than or equal to num3
and num2 is less or equal to than num3. However, we can simplify this to num1 <= num2 and num2 <= num3. The condition num1 <= num3 is
redundant since if num1 is less than num2 and num2 is less than num3
then num1 must be less than num3. Similarly, if num1 <= num3 is false and num1 is less than num2
then num2 cannot be less than num3, making the expression false.
What if Values are Equal?
The short answer is – don't worry about it. Just use <= or >=
in your comparisons and basically ignore it. The goal is just to print the
numbers in ascending order so if all three variables have the same value it
doesn't matter which gets printed first.
Version
2
Once you are
happy that version 1 works redo your if
statement. This time rewrite your program so that each condition contains a
single comparison with no logical operators (i.e. without using &&).
This will entail writing more nested if
or if … else if statements.
Assessment
1 mark for completion of at least either version 1 or version 2 of
the lab.
John Edgar (johnwill@sfu.ca)