Professional Documents
Culture Documents
1
Outline
• Assignment 2
• Formatting output
2
Outline
• Assignment 2
• Formatting output
3
Assignment 2: Missionaries and cannibals
Introduction
The objective of this assignment is to let you practice the use of variables,
operators, expressions, standard input/output and control flow in C++.
Under these constraints, how can the boat be used to safely carry all the
missionaries and cannibals across the river?
4
Assignment 2: Missionaries and cannibals
5
Assignment 2: Missionaries and cannibals
https://www.youtube.com/watch?v=dkJBn0o45vg
6
Assignment 2: Missionaries and cannibals
• The program is played via the console
• instead of a graphical user interface.
• The problem is generalized and ask user to input
• M: the number of missionaries
• C: cannibals
• B: the boat capacity
• The input should be valid
• B must not be < 2
• Unless M > C, for problems with B = 2, M and C must be <= 3
• for problems with B = 3, M and C must be <= 5 or else they have no solution
• Of course, M must always be >= C in a valid problem
7
Assignment 2: Missionaries and cannibals
• Once the valid M, C, B are obtained, the program would
repeatedly prompt the user for
• how many missionaries to get on the boat.
• how many cannibals to get on the boat.
• It will check whether the input numbers are valid
• The boat available can carry at most B people.
• The cannibals cannot outnumber the missionaries on boat
• The boat cannot cross the river by itself with no people on board
• ……
• and show the current game state.
8
Assignment 2: Missionaries and cannibals
• (Optional:) In case you are looking for some algorithms to solve M&C
puzzles, you may look at this reference.
https://flipkarma.com/media_dir/main_documents/Misionaries_And_
Cannibals_Report.pdf
9
Assignment 2: Missionaries and cannibals
• Assume that all people are on the left bank initially.
• The current game state can be represented by ⟨m, c, b⟩
• m denotes the counts of missionaries on the left bank
• c denotes the counts of cannibals on the left bank
• b equals 1 or 0 represents whether the boat is on the left or on the right bank
• ⟨3, 3, 1⟩: the initial state vector when the game starts.
• ⟨0, 0, 0⟩: means that all the people and the boat are not on the left
bank. They have all crossed the river!
10
Assignment 2: Missionaries and cannibals
<3, 3, 1>
<2, 2, 0>
<3, 2, 1>
<3, 0, 0>
<3, 1, 1>
state <1, 1, 0>
transition <2, 2, 1>
<0, 2, 0>
<0, 3, 1>
<0, 1, 0>
<0, 2, 1>
<0, 0, 0>
11
Assignment 2: Missionaries and cannibals
You win the game upon reaching the state ⟨0, 0, 0⟩ whereas you lose the game
whenever the missionaries on either bank are outnumbered by the cannibals there,
i.e. m < c and M - m < C - c.
12
Assignment 2: Missionaries and cannibals
Program Specification
• The program should obtain three numbers as
user input which control the number of
missionaries (M), the number of cannibals (C)
and the boat capacity (B) at the beginning. These
numbers have to be validated against the criteria
stated on p.1 to ensure that the game will have a
solution before the game gets started. Also, M
and C must be >= 1 to be meaningful.
• You can assume the user inputs are always non-negative integers bounded
by 1000. There is no need to validate the inputs against values that go
beyond this assumption, e.g. "abc", "@#&", 99.9, -1, 1001, 9,999,999, etc.
17
Assignment 2: Missionaries and cannibals
Assumptions and Hints
• Suppose the field width for the state counter is set to 2. In case the player
keeps playing the game for so many rounds such that the state counter
exceeds 2 digits, the output of the rest is simply shifted to the right
accordingly. See the example below:
• To know the number of digits of a number, you may use any working
method except for third party API, e.g. counting with a loop, or converting
into a string and get the string’s length.
18
Assignment 2: Missionaries and cannibals
Sample Runs
In the following sample runs, the
blue text is user input and the
other text is the program
printout. You can try the
provided sample program for
other input.
Your program output should be
exactly the same as the sample
program (same text, symbols,
letter case, spacings, etc.).
Note that there is a space after
the ‘:’ in the program printout.
19
Assignment 2: Missionaries and cannibals
Sample Runs
In the following sample runs, the blue text is user input and the other text is the program printout. You can try the
provided sample program for other input. Your program output should be exactly the same as the sample program
(same text, symbols, letter case, spacings, etc.). Note that there is a space after the ‘:’ in the program printout.
20
Assignment 2: Missionaries and cannibals
Sample Runs
• There are many combinations of possible inputs.
Please check your program correctness against the
results produced by our sample program executable
posted on Blackboard.
21
Assignment 2: Missionaries and cannibals
Submission and Marking
• Your program source file name should be rowboat.cpp. Submit the file in Blackboard
(https://blackboard.cuhk.edu.hk/).
• Insert your name, student ID, and e-mail as comments at the beginning of your source file.
• You can submit your assignment multiple times. Only the latest submission counts.
• Your program should be free of compilation errors and warnings. Your program should
include suitable comments as documentation.
• Do NOT plagiarize. Sending your work to others is subject to the same penalty for copying
work.
22
Outline
• Assignment 2
• Formatting output
23
Why do we want to format output?
• Output
24
Format flags
25
1 int number = 1023; 1023
2 3ff
3 cout.setf( ios::dec ); 3FF
4 cout << number << endl; 1777
5 cout.unsetf( ios::dec ); 1
6 true
7 cout.setf( ios::hex );
8 cout << number << endl;
9
10 cout.setf( ios::uppercase );
11 cout << number << endl;
12 cout.unsetf( ios::hex );
13
14 cout.setf( ios::oct );
15 cout << number << endl;
cout.unsetf( ios::oct );
16
17
cout << true << endl;
18
cout.setf( ios::boolalpha );
19
cout << true << endl;
20
Using format flags to format integers 26
Format flags
• boolalpha -- Boolean values can be input/output using the
words "true" and "false".
29
1 double num = 1.234; 100.000
2 // Default precision is 6 1.234000e+00
3 cout.setf( ios::showpoint ); 1.234000
4 cout << 100.0 << endl; 1.234000000000
5 1.234
6 cout.setf( ios::scientific ); 100
7 cout << num << endl; 1e+14
8 cout.unsetf( ios::scientific );
9
10 cout.setf( ios::fixed );
11 cout << num << endl;
12 cout.precision(12); // Set precision to 12
13 cout << num << endl;
14 cout.unsetf( ios::fixed );
15
16 // Use the "default" format for floating point numbers
17 cout.unsetf( ios::showpoint );
18 cout << num << endl;
19 cout << 100.0 << endl;
20 cout << 100000000000000.0 << endl;
Example: Using format flags to format floating point numbers 30
1 123
2 int main() { 1.00000
3 123.000
4 cout << 123.0 << endl;
5 cout.setf(ios::showpoint);
6 cout << 1.0 << endl;
7 cout << 123.0 << endl;
8
9 return 0;
10 }
11
12
13
14
32
1 int number = 0x03ff; 1023
2 3ff
3 cout << dec << number << endl; 3FF
4 cout << hex << number << endl; 1777
5 cout << uppercase << number << endl; 1
6 cout << oct << number << endl; true
7
8 cout << noboolalpha << true << endl;
9 cout << boolalpha << true << endl;
10 cout << endl;
11
33
1 double num = 1.234;
2
3 cout << showpoint << 100.0 << endl;
4 cout << scientific << num << endl;
5 cout << fixed << num << endl;
6
7 cout << setprecision(12) << num << endl;
8
9 // Reset to the "default" floating point format
10 cout.unsetf( ios::scientific | ios::fixed );
11 100.000
12 cout << noshowpoint << num << endl; 1.234000e+00
13 cout << 100.0 << endl; 1.234000
14 1.234000000000
15 1.234
16 100
36
noskipws Turns off the skipws flag X
nounitbuf Turns off the unitbuf flag X
nouppercase Turns off the uppercase flag X
oct Turns on the oct flag X X
right Turns on the right flag X
scientific Turns on the scientific flag X
showbase Turns on the showbase flag X
showpoint Turns on the showpoint flag X
showpos Turns on the showpos flag X
skipws Turns on the skipws flag X
unitbuf Turns on the unitbuf flag X
uppercase Turns on the uppercase flag X
ws Skip any leading whitespace X
37
Manipulators defined in <iomanip>
Manipulator Description Input Output
resetiosflags( long f ) Turn off the flags specified by f X X
setbase( int base ) Sets the number base to base X
setfill( char ch ) Sets the fill character to ch X
setiosflags( long f ) Turn on the flags specified by f X X
Sets the number of digits of
setprecision( int p ) X
precision
setw( int w ) Sets the field width to w X
38
Thanks
Q&A
39