Академический Документы
Профессиональный Документы
Культура Документы
Laboratory Tutorial 2
Flow of Control
Question 1. While loop
Write a C++ program named question1.cpp.
The program has an integer variable named target, say
int target = 66;
The program allows users to input an integer (assume that users will
input an integer range from 1 to 100, and lets store the integer in a
variable named guess). If the user inputs a wrong answer, the program will
keep asking users to input an integer until the guess value matches the
target value (i.e. 66 in this example).
Program design
Initialize target =
66, and guess = -1.
Read in users
guess integer
guess !=
target
Yes
No
Display Bingo
1
ENGG1111B Tutorial materials prepared by Dr. Chui Chun Kit (ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
during the lab
Implementation of question1.cpp
return 0;
}
Step 2. Declare two variables: one for storing the target, one for
storing the guess value (We will decide what the initial value of guess
is later.
int target = 66;
int guess = ???;
Step 3. We will ask questions again and again until the guess value is
equal to the target value
// Ask questions again and again until guess!=target is false
while (???) {
cout << "Please input an integer ranging between 1 and 100 :";
cin >> guess;
}
Step 4. Display Bingo after the end of while loop, as by that time the
user has made the correct guess.
cout << "Bingo!";
2
ENGG1111B Tutorial materials prepared by Dr. Chui Chun Kit (ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
during the lab
Question 2. FOR-LOOP
2a. Write a program that will print out the following pattern.
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1 0
2b. Write a program that will print out the following pattern. Use one for loops to achieve the output.
1 * 1 = 1
2 * 1 = 2
3 * 1 = 3
4 * 1 = 4
5 * 1 = 5
9 * 1 = 9
10 * 1 = 10
2c. Write a program that will print out the following pattern. Use two for loops to achieve the output.
1 * 1 = 1
1 * 2 = 2
1 * 10 = 10
2 * 1 = 2
2 * 2 = 4
2 * 10 = 20
3
ENGG1111B Tutorial materials prepared by Dr. Chui Chun Kit (ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
during the lab
4
ENGG1111B Tutorial materials prepared by Dr. Chui Chun Kit (ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
during the lab
After writing the code, please compile it and test it with some test
cases to validate the program is logically correct
Write a program that will print out the following pattern. Use two for loops to achieve the
output.
0123456
012345
01234
0123
012
01
0
5
ENGG1111B Tutorial materials prepared by Dr. Chui Chun Kit (ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
during the lab
return 0;
}
1 - Display menu
"Welcome to ENGG1111B jukebox!" << endl;
"-----------------------------" << endl;
"1. You Raise Me Up" << endl;
"2. GENTLEMAN" << endl;
"3. Quit" << endl;
Professional programmer
style...
You can compile the code
before finishing the whole
program .
Implement
part
ENGG1111B Tutorial
materials prepared
by Dr. of
Chui the
Chun Kitprogram
(ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
first and compile it to check if you
during the lab
have implemented it correctly
before proceed.
What is the mechanism inside the while loop to let users give another
choice and complete the loop?
7
ENGG1111B Tutorial materials prepared by Dr. Chui Chun Kit (ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
during the lab
Task 4. Now, based on the value in the variable choice, we will play
different songs.
Engineers mindset:
the
computer to start playing the file 1.mp3. This technique is not
in the syllabus but it is simple and fun
o When user choose 2:
o
Run the code and see if you can get the result.
Question: I
want to display
the menu (the
song list)
every time
before asking
What is your
choice. What
should I do ?
void show_menu(){
cout << "Welcome to ENGG1111B jukebox!" << endl;
cout << "-----------------------------" << endl;
cout << "1. You Raise Me Up" << endl;
cout << "2. GENTLEMAN" << endl;
cout << "3. Quit" << endl;
}
9
ENGG1111B Tutorial materials prepared by Dr. Chui Chun Kit (ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
during the lab
show_menu();
10
ENGG1111B Tutorial materials prepared by Dr. Chui Chun Kit (ckchui@cs.hku.hk), TA
Kevin, Frank and Jason. Please feel free to contact us for help if you have any problem
during the lab