Вы находитесь на странице: 1из 10

ENGG 1111B Computer Programming and Applications

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

Open Code::Blocks, open an empty file.


Step 1. Add in the standard C++ syntax, save it as question1.cpp.
#include <iostream>
using namespace std;
int main() {
// Step2. Declare two variables here.

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

(up to 10*10 = 100)


2d. Write a program that will print out the following pattern. Use two for loops to achieve the output.
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 54 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

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

Dear students of ENGG1111B,


If you face any problems in understanding the materials in the
lectures or the tutorials, please feel free to contact me (Kit) or our
TAs (Kevin , Frank and Jason).
We are very happy to help you! We wish you enjoy
learning programming in this class J.
Dr. Chui Chun Kit (Kit)

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

Question 3. Take home practice IF-ELSE


In Chapter 3 page 22 26 we have an in-class exercise to build a
program that returns the maximum of three integers. Now, as a warm up
practice, please write a program called minimum.cpp. The program reads
in 3 integers. Then the program outputs the minimum of the 3
integers.
Remember:
Proper indentation makes your code much more readable!
if (a > b){
// The body of this block are indented to right to
// make it more easy to read.
}

After writing the code, please compile it and test it with some test
cases to validate the program is logically correct

Question 4. Take home practice FOR-LOOP (*No solutions will be


provided for this as the technique is very similar with the
solution in assignment 1 )

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

Question 5. Take home practice - Practice on looping


and switch statement (Only work in Window)
Lets write a C++ program named jukebox.cpp. that gives a
song list and plays music !
Preparation
Download the 1.mp3 and 2.mp3 files to your desktop.
Open Code::Blocks, save the file with following content as
jukebox.cpp in the desktop.
#include<iostream>
#include<cstdlib>
using namespace std;
int main (){

return 0;
}

What is the library <cstdlib> for?


Ans: This library provides miscellaneous utilities, in this program since
we need to call the window system to open another mp3 file through
system("FileName.mp3"), we need to include this library which defines
the logic of system().

Task 1. Display the menu on the screen.


// Task
cout <<
cout <<
cout <<
cout <<
cout <<

1 - Display menu
"Welcome to ENGG1111B jukebox!" << endl;
"-----------------------------" << endl;
"1. You Raise Me Up" << endl;
"2. GENTLEMAN" << endl;
"3. Quit" << endl;

Task 2. Getting users choice and store it in a variable choice.


// Task 2. Getting users choice.
cout << "What is your choice? ";
int choice;
cin >> choice;

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.

Task 3. We will play the song based on the users choice,


and end the program until the user choose 3.
Engineers mindset:
We need to be smart enough to realize that we need a
loop (doing something repeatedly until user choose 3)
for loop or while loop?
o Use for loop when we know exactly how many iterations we are
going to loop.
o Use while loop when the number of iterations depends on some
conditions.
// Task 3. While loop process user input until choice is 3
while (choice!=3){
// Do something to play the song selected

// Update the choice to the next choice


}

What is the mechanism inside the while loop to let users give another
choice and complete the loop?

Answer: The following code should be added below the line //


Update the choice to the next choice
cout << "What is your choice? ";
cin >> choice;

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:

We are going to do different things based on the value of a


variable.
Therefore we can use switch statement to implement it! To use
switch, what is the controlling variable?
// Task 4. Use switch statement to handle branching
switch (???){

Design the cases - What are the possible values of the


controlling variable that we want to handle?
case 1:
// Something to be done if user choose 1
break;
case 2:
// Something to be done if user choose 2
break;
case 3:
// Something to be done if user choose 3
break;

As a professional, we often want to handle error cases. How about


the cases that users choice is not 1, 2 or 3?
default:
cout << "Invalid input!" << endl;
break;

What are we going to do for each case?


o When user choose 1:
cout << "You Raise Me Up"<<endl;
cout << "Singer: Westlife, Length: 5:04"<<endl;
system("start 1.mp3");
Note: system("start 1.mp3"); is just command to ask

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

cout << "GENTLEMAN" << endl;


8
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

cout << "Singer: PSY, Length: 3:15"<<endl;


system("start 2.mp3");

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 ?

Create the show_menu() function before main()


Important! You have to put the function before
and outside the main() function, because the logic of
show_menu() is independent to main().

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

In the main() function, replace the places that we would like to


display the menu by simply calling show_menu().

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

Вам также может понравиться