辅导R语言程序|解析Haskell程序|解析Haskell程序|辅导R语言编程
- 首页 >> Java编程 CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
1
CS2311 Computer Programming
2020/21 Semester B
Department of Computer Science
City University of Hong Kong
Assignment Two
Due Date: 30 April 2021 (Friday) 23:59
All questions should be submitted in Canvas. Note that all solutions will be assessed by (1)
correctness, (2) programming styles (including comments), (3) non-redundancy in expressing
solutions, and (4) efficiency, if applicable. In particular, Question 4 will consider the efficiency aspect
in marking.
This assignment contains 5 questions named as Q1 to Q5. Q5 has 10% bonus.
Your solutions are forbidden to INCLUDE any library other than, ,
, , and/or . Each solution using any function/facility in other
libraries will receive 0 mark. If possible, not using in your solution.
Solution Submission Instruction:
• You only need to submit the content of .cpp files of your solutions for the five questions.
• In Canvas, there are 5 submission links, one for each question.
o Submit your C++ code for Question 1 to the submission link for Question 1,
o Submit your C++ code for Question 2 to the submission link for Question 2,
o Submit your C++ code for Question 3 to the submission link for Question 3,
o Submit your C++ code for Question 4 to the submission link for Question 4,
o Submit your C++ code for Question 5 to the submission link for Question 5.
Submission Example.
Copy and Paste Your Code developed for Question 1 into “Submission for Q1” and then press
[submit assignment]. Repeat the procedure for each question. Remember to submit through
different submission links.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
Q1. Sum Matrix Game [20%]
Sum Matrix is a number placement game.
In this game, an n x n matrix is filled with numbers such that (1) the sum of the numbers in each row or
each column is all same, and (2) all the numbers from 1 to n*n should be placed in the matrix. The
program should develop the matrix based on the following given rules.
1. The program should ensure the input value n as an odd number in the range of 1 to 11. Otherwise,
the program should display the following message and terminate: Please enter an odd
positive number in the range of [1-11]. Bye.
2. The possible range of values in each cell of the matrix should be a product of the input odd number.
• E.g., For sum matrix of order 3*3, the cell values should be in the range of (1-9)
• E.g., For sum matrix of order 5*5, the cell values should be in the range of (1-25)
3. In the output, set the width of each column of the matrix to 4 characters and there is one space
to separate two columns.
4. Note that the cells in your matrix may have different values. (There are multiple solutions, and
you only need to generate one possible solution.)
Hints:
• Observe where to place “1” in a particular matrix and the pattern to place “2”, “3”, “4”, and so
on. You will observe a general pattern.
Example 1
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
2
Please enter an odd positive number in the range of [1-11]. Bye.
Example 2
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
3
The Sum Matrix for 3 * 3 Order is
8 1 6 -------> 15
3 5 7 -------> 15
4 9 2 -------> 15
---- ---- ----
15 15 15
Example 3
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
5
The Sum Matrix for 5 * 5 Order is
17 24 1 8 15 -------> 65
23 5 7 14 16 -------> 65
4 6 13 20 22 -------> 65
10 12 19 21 3 -------> 65
11 18 25 2 9 -------> 65
---- ---- ---- ---- ----
65 65 65 65 65
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
4
Example 4
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
7
The Sum Matrix for 7 * 7 Order is
30 39 48 1 10 19 28 -------> 175
38 47 7 9 18 27 29 -------> 175
46 6 8 17 26 35 37 -------> 175
5 14 16 25 34 36 45 -------> 175
13 15 24 33 42 44 4 -------> 175
21 23 32 41 43 3 12 -------> 175
22 31 40 49 2 11 20 -------> 175
---- ---- ---- ---- ---- ---- ----
175 175 175 175 175 175 175
Example 5
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
9
The Sum Matrix for 9 * 9 Order is
47 58 69 80 1 12 23 34 45 -------> 369
57 68 79 9 11 22 33 44 46 -------> 369
67 78 8 10 21 32 43 54 56 -------> 369
77 7 18 20 31 42 53 55 66 -------> 369
6 17 19 30 41 52 63 65 76 -------> 369
16 27 29 40 51 62 64 75 5 -------> 369
26 28 39 50 61 72 74 4 15 -------> 369
36 38 49 60 71 73 3 14 25 -------> 369
37 48 59 70 81 2 13 24 35 -------> 369
---- ---- ---- ---- ---- ---- ---- ---- ----
369 369 369 369 369 369 369 369 369
Example 6
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
11
The Sum Matrix for 11 * 11 Order is
68 81 94 107 120 1 14 27 40 53 66 -------> 671
80 93 106 119 11 13 26 39 52 65 67 -------> 671
92 105 118 10 12 25 38 51 64 77 79 -------> 671
104 117 9 22 24 37 50 63 76 78 91 -------> 671
116 8 21 23 36 49 62 75 88 90 103 -------> 671
7 20 33 35 48 61 74 87 89 102 115 -------> 671
19 32 34 47 60 73 86 99 101 114 6 -------> 671
31 44 46 59 72 85 98 100 113 5 18 -------> 671
43 45 58 71 84 97 110 112 4 17 30 -------> 671
55 57 70 83 96 109 111 3 16 29 42 -------> 671
56 69 82 95 108 121 2 15 28 41 54 -------> 671
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
671 671 671 671 671 671 671 671 671 671 671
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
5
Q2. Document Correction and Alignment Problem [20%]
Nowadays, the natural language processing technology is increasingly more mature. Recently the
Beetles Phone Technology company released their latest smart phone, Beetles Resolver 66. The most
eye-catching part of their release conference is not their new smart phone, but rather, a speech
recognition input method Taxman input method.
Now suppose we have a document, and the content of which is generated by a speech recognition input
method. Due to the error of the speech recognition input method, some continuously duplicated words
appear in the content. Also, the format of the content is messy because of different length of each line
(see the content of D1.txt in example).
You are required to write a program to remove the word duplication and align the content based on the
following instructions:
1) Your program can assume that every word in the input document consist of at most 20 characters,
the input document may be jammed with empty line, but the whole input document represents one
single paragraph with at most 1000 words. Similarly, your output document should contain one
paragraph with at most 1000 words.
2) The given document is a text file, named as document.txt, and the output text file is called output.txt.
Your program should place it in the project directory of your VS C++ project.
3) Your program only needs to handle the duplication for a single word, like "The the the world". The
duplication for multiple words, like "The world the world", will not appear in the given document
read by your program. That is, if there is a consecutive sequence of same word (irrespective to the
use of capital letters and lowercase letters), then it is a duplication of that single word.
4) Your program can assume that the following characters consider as punctuations in an English
sentence: comma and full stop. All other characters are treated as a part of a word. In Example 2 on
next page, “(Friday)” is a word. In this case, “(Friday) Friday” has no word duplication and
your program needs not to correct this sequence of characters.
5) The number of the duplication is indefinite.
6) Each duplication of a single word should be replaced by a single occurrence of that word after your
correction. For example, "The the the world" should become "The world" after your correction.
7) After the alignment, each line should contain exactly 60 characters (contain the space and
punctuations), with the exception of the last line.
8) Your program can align the content by adjusting the number of words per line and the space
between the words. The space number between the words should be determined by the total space
available and the total words number in this line. There is no space between words and
punctuations.
a) E.g., if a line can contain 4 words and 3 spaces, then the amounts of space characters in
between two consecutive words on this line should be 1, 1, and 1.
b) E.g., if one line can contain 4 words and 5 spaces, then the amounts of space characters in
between two consecutive words should be 2, 2, 1.
9) After the alignment, each line should start with the word and end with the word or punctuations.
10) No word can be spited during the alignment process.
11) The result is required to be stored in a text file named as output.txt.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
6
Hints:
1. Your program may consider using a two-dimensional array to keep 1000 text strings, each
with a length sufficient to keep your required characters (including the end-of-string character
‘\0’)).
2. Your program may consider the logics outlined below.
a. It read the lines from the given text file line by line.
b. For each line, identify each word together with a possible ending punctuation
appending to it and keep that string into an entry in the above-mentioned twodimensional
array.
c. Now you keep the whole text document as a sequence of words in an array.
d. Your program should then compare whether two consecutive words in the array refer
to the same word. If you find a duplicated word, then you can use your strategy to
either remove that duplicated work from the array or mark the corresponding entry
in the array as “duplicated”.
e. Now, you can scan the array from the first entry and decide the length of each entry.
While your scanning, you can determine which words to be placed in the same line in
the output text file together with the number of space characters you needed to
separate the words in that output line.
f. You should then output that line to the text file.
g. Of course, before your program ends, it should close all files opened by it.
3. Your program may use strlen() and strcmp(), strcpy(). Note that in Visual Studio 2019, the
functions strcmp() and strcpy() are not allowed by default. See Slide 28 of the Lecture note
L08a to observe how to bypass the limitations. You may use strcmp_s() instead of strcmp()
and strcpy_s() instead of strcpy(), which are directly supported by VS 2019 by default.
4. Since duplicated occurrences of the same words (e.g., The the the) may be in different
combinations of uppercase and lowercase characters. Your program may firstly convert them
into all lowercases and then do a comparison. You may just assume that the first occurrence
is the one you wish to put into the text file output.txt.
5. A word in the given document may be appended with a full stop or a comma. Since it is a text
file, a line also ends with the end-of-line character (‘\0’). To identify a word, your program
should consider whether a word is ended with a full stop, a comma, or the end-of-line
character.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
7
Example 1:
Input document:
The the the town revolved around the river river. In summer,
when the blazing sun sun sun sun beat beat down, it dozed under the weight
of of sultry days. On Main Street a sow and her litter litter
litter of pigs might root along the wooden side walks, sharing the
deeply deeply rutted rutted roadway with foraging hens and
and and and a hound languidly scratching his fleas fleas.
Output Content of Output.txt is as follows
The town revolved around the river. In summer, when the
blazing sun beat down, it dozed under the weight of sultry
days. On Main Street a sow and her litter of pigs might
root along the wooden side walks, sharing the deeply rutted
roadway with foraging hens and a hound languidly scratching
his fleas.
Example 2:
Input document:
CS2311 Computer Programming
2020/21 Semester B
Department of Computer Science
City University of Hong Kong
Assignment Two
Due Date: 30 April 2021 (Friday) 23:59
All questions should be submitted in Canvas. Note that all solutions will
be assessed by (1) correctness, (2) programming styles (including
comments), and (3) non-redundancy in expressing solutions, if applicable.
This assignment contains 5 questions named as Q1 to Q5. Q5 has 10% bonus.
Your solutions are forbidden to INCLUDE any library other than,
, , , and/or . Each solution using any
function/facility in other libraries will receive 0 mark. If possible, not
using in your solution.
Output: Content of Output.txt is as follows
CS2311 Computer Programming 2020/21 Semester B Department
of Computer Science City University of Hong Kong Assignment
Two Due Date: 30 April 2021 (Friday) 23:59 All questions
should be submitted in Canvas. Note that all solutions will
be assessed by (1) correctness, (2) programming styles
(including comments), and (3) non-redundancy in expressing
solutions, if applicable. This assignment contains 5
questions named as Q1 to Q5. Q5 has 10% bonus. Your
solutions are forbidden to INCLUDE any library other than
, , , , and/or
. Each solution using any function/facility in other
libraries will receive 0 mark. If possible, not using
in your solution.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
8
Q3. Pointer and Dynamic Array [20%]
The code on the next page is an executable solution for Exercise 5 of Tutorial 7 on Array.
Your task is to (1) rewrite the program to perform the same computation that meets the following
requirements. Note that you can use your own array-version programs as long as your program meets
the following requirements.
• Every array in the program is a dynamic array.
• Exceptthe statement in which it uses of the “new” operator (e.g., int* faces = new int[12];),
there is no any use of any array construct in the program. (hint: Use Pointers!)
o For example, all of the following array constructs in the code to refer to the elements in
the corresponding dynamic arrays are forbidden to be used.
sum[i + j - 1]
sum[k]
sum[index]
sorted[index][0]
sorted[index][1]
sorted[k][0]
sorted[k - 1][0]
sorted[k][1]
sorted[k - 1][1]
sorted[i][0]
sorted[i-1][0]
faces[sorted[0][1] - 1]
faces[sorted[i][1] - 1]
faces[k]
• Every dynamic array must be deleted before the program terminates.
o Hint: when deleting the 1D array faces, use delete: delete[] faces;
o Hint: when deleting a 2D array, you need to delete all the rows of the 2D array first, and
then the 2D array.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
9
#include
using namespace std;
int main() {
char faces[12] = { 0 };
int sum[12] = { 0 }; // max 12 values of occurrence counts
int sorted[12][2]; // a 2D array
// sorted[i][0] = sum of face values kept in sum[index]
// sorted[i][1] = index of sum[index]
for (int i = 1; i <= 6; i++) // i is dice 1
for (int j = 1; j <= 6; j++) // j is dice 2
sum[i + j - 1] = sum[i + j - 1] + 1;
for (int k = 0; k < 12; k++)
if (sum[k] != 0)
cout << sum[k]
<< " occurrence(s) of the sum "
<< k + 1 << endl;
// we firstly copy sum to rank
for (int index = 0; index < 12; index++)
{
sorted[index][0] = sum[index];
sorted[index][1] = index + 1;
}
// then we sort the array sorted[] using bubble sort
// check out the lecture note slide 31 of Lec07
// we want sorted[0] as the LARGEST after the sorting process
int tmp;
for (int j = 0; j < 12 - 1; j++) // outer loop
for (int k = 12 - 1; k > j; k--) // bubbling
if (sorted[k][0] > sorted[k - 1][0]) { // check against col 0
// note that the condition is ">" rather than "<".
tmp = sorted[k][0]; // swap neighbors for col 0
sorted[k][0] = sorted[k - 1][0];
sorted[k - 1][0] = tmp;
tmp = sorted[k][1]; // swap neighbors for col 1
sorted[k][1] = sorted[k - 1][1];
sorted[k - 1][1] = tmp;
}
for (int i = 0; i < 12; i++)
if (sorted[i][0] != 0)
cout << sorted[i][0] << ' ' << sorted[i][1] << endl;
char c = 'A'; // the 1st character to be shown
faces[sorted[0][1] - 1] = c;
for (int i = 1; i < 12; i++) {
if (sorted[i][0] != sorted[i - 1][0])
c++;
if (sorted[i][0] != 0)
faces[sorted[i][1] - 1] = c;
}
// print out the letter rank
for (int k = 0; k < 12; k++)
if (faces[k] != 0)
cout << faces[k] << " occurrence(s) of the sum " << k + 1 << endl;
return 0;
}
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
10
Example:
1 occurrence(s) of the sum 2
2 occurrence(s) of the sum 3
3 occurrence(s) of the sum 4
4 occurrence(s) of the sum 5
5 occurrence(s) of the sum 6
6 occurrence(s) of the sum 7
5 occurrence(s) of the sum 8
4 occurrence(s) of the sum 9
3 occurrence(s) of the sum 10
2 occurrence(s) of the sum 11
1 occurrence(s) of the sum 12
6 7
5 6
5 8
4 5
4 9
3 4
3 10
2 3
2 11
1 2
1 12
F occurrence(s) of the sum 2
E occurrence(s) of the sum 3
D occurrence(s) of the sum 4
C occurrence(s) of the sum 5
B occurrence(s) of the sum 6
A occurrence(s) of the sum 7
B occurrence(s) of the sum 8
C occurrence(s) of the sum 9
D occurrence(s) of the sum 10
E occurrence(s) of the sum 11
F occurrence(s) of the sum 12
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
11
Q4. Recursion
In tutorial T04, Exercise 4 asks students to develop a program using four nested loops. The tutorial
exercise is reproduced below.
Write a program without using any loop (i.e., without any while-loop, for-loop, or do-while loop) to solve
the same program. I.e., the program should accept a positive integer number N and outputs a sequence
of lines in the format of p = a^3 + b^3 = c^3 + d^3 where p, a, b, c, d are all distinct positive integers such
that p <= N and p = a3 + b3 = c3 + d3 and no two lines contains the same set of {a, b, c, d} (in the sense of
values).
Your program only needs to handle 0 ≤ N ≤ 1000000.
Your program should be efficient. For instance, when a user inputs a large number, your program should
output the correct list and terminates within a reasonable time (e.g., 1 minute). FYI, a correct program
can produce the output within a few seconds.
Important Hints:
• The solution of this question does not require to use any arrays.
• Students are advised to work on Exercise 4 in Tutorial 4, particularly following the slides 22-27
to resolve the subproblems stated on the slides. Or else your program will easily incur bugs.
• Your program will be at least four recursions, one on each of the four variables “a”, “b”, “c”, and
“d” to enumerate their combinations. The recursion on “d” is nested in the recursion on “c”,
which in turn is nested in the recursion on “b”, and which is also in turn nested on “a”.
• Students are advised to study the patterns in the reference solution of Exercise 4 in Tutorial 4. In
that reference solution, when looping on variable “b”, it needs the information of the current
value of the variable “a” within the loop body of this loop. Similarly, when looping on variable
“c”, it needs the information of the current value of variable “a”, and when looping on variable
“d”, it needs the information of the current values of variables “a”, “b”, and “c”.
Exercise 4: 1729 is Interesting
There was an Indian mathematician who was famous for his intuition for
numbers. When the English mathematician G. H. Hardy came to visit him in
the hospital one day, Hardy mentioned that the number of his taxi was 1729,
a rather dull number. To which the Indian mathematician replied, "No! It is a
very interesting number. 1729 is the smallest positive integer expressible
as the sum of two cubes in two different ways" .
Verify this claim by writing a program twoCubric.cpp that
takes an integer N and prints out all positive integers less than
or equal to N that can be expressed as the sum of two cubes in
two different ways: i.e., find distinct positive integers a, b, c,
and d such that N = a3 + b3 = c3 + d3.
hint: Use four nested for loops, one for each of a, b, c, d.
Let’s assume that N is not a very large number.
Read the next slide 20
Examples of Console for Ex4
21
Input: 1728
Input: 1729
1729 = 1^3 + 12^3 = 9^3 + 10^3
Input: 10000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
Input: 100000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
46683 = 3^3 + 36^3 = 27^3 + 30^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
40033 = 9^3 + 34^3 = 16^3 + 33^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
65728 = 12^3 + 40^3 = 31^3 + 33^3
64232 = 17^3 + 39^3 = 26^3 + 36^3
Check whether or not our solution contains duplicated entries.
1729 = 1 3 + 123 = 93 + 103
4104 = 2 3 + 163 = 93 + 153
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
12
Q5. Ash and Pikachu [20% + 10% Bonus]
In the Pokemon series of manga, Ash is the main character who joins force with Pikachu, a Pokémon, for
various adventure. We now have the Pokemon Go. In this question, we are going to implement some
of the concepts involved through C++ classes.
In this question, your program should allow a user to input: (1) a Pokemon world, (2) a list of Pokemons
and (3) [For Bonus] the information about a trainer called Ash.
Ensure that the output follows the format presented in Examples given at the end of this question.
Important note: Your program can assume the following: (1) the number of Pokemons is at least 1 and at
most 1000, (2) the name of any object is a cstring consisting of at most 10 characters (including ‘\0’), and
(3) all user inputs are valid.
• The concept of PokemonWorld is implemented as a C++ class.
o Each PokemonWorld object owns two PokemonStation objects. We simply refer to each
PokemonStation object of this class as a station.
o Each PokemonWorld object is responsible to initialize the two (2) stations.
(Note: The use of an array to keep two stations will make your program significantly more
complex than keeping them as two separate variables. The reason is due to our limited
known-how introduced in CS2311 to use object pointers rather than the limitation of C++.)
o When a Pokemon enters a PokemonWorld, the PokemonWorld is responsible to assign this
Pokemon to one of the two stations on its own. The assignment of each Pokemon will be done to
the PokemonStation in a round-robin manner (i.e., the first Pokemon is assigned to
PokmonStation1, the second Pokemon is assigned to PokmonStation2, the third Pokemon is
assigned to PokmonStation1 again, the fourth Pokemon is assigned to PokmonStation2 and so on).
o The ‘name’ variable of the PokemonWorld object can be accessed by the object itself.
o Hint: study http://stackoverflow.com/questions/12927169/how-can-i-initialize-c-objectmember-variables-in-the-constructor
• The concept of PokemonStation is also implemented as a C++ class.
o Each object of this class represents a station which has a station identifier. We simply refer to
each object of this class as a station. The station identifiers are s1 and s2, respectively.
o The identifier of each station is provided by the PokemonWorld object that owns the station
through the class constructor of PokemonStation. (Hint: You may wish to initialize each
PokemonStation when the constructor of PokemonWorld object is being called.)
o Each station maintains its own list of Pokemons assigned by the PokemonWorld object.
o Both the identifier of a PokemonStation and the list of Pokemons (i.e., each Pokemon that
belongs to a particular PokemonStation) should not be accessible by any other object nor be
returned via a function.
o Only PokemonWorld and the PokemonStation itself can invoke the functions of that station.
• The concept of Pokemon is also implemented as a C++ class.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
13
o Each Pokemon is an object of this class.
o Each Pokemon object has its own name (a cstring) and hp value (an integer).
o All data members (name, hp) of each Pokemon object once initialized can only be accessed by
that Pokemon object.
• The main() function is responsible for the following:
o To accept all the inputs from the user.
o To create a PokemonWorld and all the Pokemons.
o To set each Pokemon to enter the PokemonWorld.
o To call the method print() of Pokemon, which in turn calls the method print() of each
PokemonStation, which in turn calls the method print() of each Pokemon to print out the
information of the PokemonWorld, the PokemonStation, and the Pokemon, respectively.
Bonus Part [Extra 10%]
• The requirements stated above should remain intact.
• A Trainer needs to be implemented as a C++ class.
o Each trainer (e.g. Ash) is an object of this class.
o Each trainer has his/her own name (cstring), identifier (integer), and strength (integer). All this
information is taken from the user as input from console.
o All data members of each trainer object cannot be accessed outside the trainer object. That is,
the value of each data member should not be returned via a function nor be retrieved via callby-reference.
• The main() function is responsible to create a trainer called Ash and let Ash enter the PokemonWorld.
• PokemonWorld is responsible to guide Ash to visit its two stations one by one once Ash enters this
world.
• Each station is responsible to determine whether a trainer can capture each Pokemon kept by the
station. Specifically, if the hp of the Pokemon is strictly less than (<) the strength of the trainer, then
the Pokemon is marked as captured by the trainer.
o Note that following the above requirements, both the hp of the Pokemon and the strength of
the trainer should not be returned via any function.
• The print() method of each captured Pokemon calls the print() method of the trainer object that
captures it to print the information of Ash.
More description about the above three classes
Ideas on the data members of the
Pokeworld class.
[The class may contain other data members]
Ideas on the data members of the
PokeStation class.
[The class may contain other data members]
Ideas on the data members of the
Pokemon class.
[The class may contain other data members
Name The name of this Pokeworld
object
ID The identifier of this station,
such as 1 and 2.
Name The name of this Pokemon
object
s1 1st station owned by the
Pokeworld
list An array of Pokemon hp The HP of the Pokemon
object
s2 2nd station owned by the
Pokeworld
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
14
Example 1: (without Bonus)
What is the World Name?
Macau
Please input the number of Pokemon in the Macau world
7
Please input the characteristics of all Pokemon: Name HP
Pikachu 50
Regice 100
Articuno 100
Charizard 150
Blastoise 240
Mew 340
Gengar 101
World is Macau
Pokemon in Station #1:
Pikachu HP 50
Articuno HP 100
Blastoise HP 240
Gengar HP 101
Pokemon in Station #2:
Regice HP 100
Charizard HP 150
Mew HP 340
Example 2: (without Bonus)
What is the World Name?
Hongkong
Please input the number of Pokemon in the Hongkong world
1
Please input the characteristics of all Pokemon: Name HP
Pikachu 50
World is Hongkong
Pokemon in Station #1:
Pikachu HP 50
No Pokemon in Station #2
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
15
Example 3: (with Bonus)
What is the World Name?
Macau
Please input the number of Pokemon in the Macau world
7
Please input the characteristics of all Pokemon: Name HP
Pikachu 50
Regice 100
Articuno 100
Charizard 150
Blastoise 240
Mew 340
Gengar 101
Enter Trainer information: ID NAME HP
1 Ash 101
World is Macau
Pokemon in Station #1:
Pikachu HP 50 Ash is its master
Articuno HP 100 Ash is its master
Blastoise HP 240
Gengar HP 101
Pokemon in Station #2:
Regice HP 100 Ash is its master
Charizard HP 150
Mew HP 340
*** END OF ASSIGNMENT TWO ***
1
CS2311 Computer Programming
2020/21 Semester B
Department of Computer Science
City University of Hong Kong
Assignment Two
Due Date: 30 April 2021 (Friday) 23:59
All questions should be submitted in Canvas. Note that all solutions will be assessed by (1)
correctness, (2) programming styles (including comments), (3) non-redundancy in expressing
solutions, and (4) efficiency, if applicable. In particular, Question 4 will consider the efficiency aspect
in marking.
This assignment contains 5 questions named as Q1 to Q5. Q5 has 10% bonus.
Your solutions are forbidden to INCLUDE any library other than
libraries will receive 0 mark. If possible, not using
Solution Submission Instruction:
• You only need to submit the content of .cpp files of your solutions for the five questions.
• In Canvas, there are 5 submission links, one for each question.
o Submit your C++ code for Question 1 to the submission link for Question 1,
o Submit your C++ code for Question 2 to the submission link for Question 2,
o Submit your C++ code for Question 3 to the submission link for Question 3,
o Submit your C++ code for Question 4 to the submission link for Question 4,
o Submit your C++ code for Question 5 to the submission link for Question 5.
Submission Example.
Copy and Paste Your Code developed for Question 1 into “Submission for Q1” and then press
[submit assignment]. Repeat the procedure for each question. Remember to submit through
different submission links.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
Q1. Sum Matrix Game [20%]
Sum Matrix is a number placement game.
In this game, an n x n matrix is filled with numbers such that (1) the sum of the numbers in each row or
each column is all same, and (2) all the numbers from 1 to n*n should be placed in the matrix. The
program should develop the matrix based on the following given rules.
1. The program should ensure the input value n as an odd number in the range of 1 to 11. Otherwise,
the program should display the following message and terminate: Please enter an odd
positive number in the range of [1-11]. Bye.
2. The possible range of values in each cell of the matrix should be a product of the input odd number.
• E.g., For sum matrix of order 3*3, the cell values should be in the range of (1-9)
• E.g., For sum matrix of order 5*5, the cell values should be in the range of (1-25)
3. In the output, set the width of each column of the matrix to 4 characters and there is one space
to separate two columns.
4. Note that the cells in your matrix may have different values. (There are multiple solutions, and
you only need to generate one possible solution.)
Hints:
• Observe where to place “1” in a particular matrix and the pattern to place “2”, “3”, “4”, and so
on. You will observe a general pattern.
Example 1
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
2
Please enter an odd positive number in the range of [1-11]. Bye.
Example 2
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
3
The Sum Matrix for 3 * 3 Order is
8 1 6 -------> 15
3 5 7 -------> 15
4 9 2 -------> 15
---- ---- ----
15 15 15
Example 3
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
5
The Sum Matrix for 5 * 5 Order is
17 24 1 8 15 -------> 65
23 5 7 14 16 -------> 65
4 6 13 20 22 -------> 65
10 12 19 21 3 -------> 65
11 18 25 2 9 -------> 65
---- ---- ---- ---- ----
65 65 65 65 65
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
4
Example 4
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
7
The Sum Matrix for 7 * 7 Order is
30 39 48 1 10 19 28 -------> 175
38 47 7 9 18 27 29 -------> 175
46 6 8 17 26 35 37 -------> 175
5 14 16 25 34 36 45 -------> 175
13 15 24 33 42 44 4 -------> 175
21 23 32 41 43 3 12 -------> 175
22 31 40 49 2 11 20 -------> 175
---- ---- ---- ---- ---- ---- ----
175 175 175 175 175 175 175
Example 5
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
9
The Sum Matrix for 9 * 9 Order is
47 58 69 80 1 12 23 34 45 -------> 369
57 68 79 9 11 22 33 44 46 -------> 369
67 78 8 10 21 32 43 54 56 -------> 369
77 7 18 20 31 42 53 55 66 -------> 369
6 17 19 30 41 52 63 65 76 -------> 369
16 27 29 40 51 62 64 75 5 -------> 369
26 28 39 50 61 72 74 4 15 -------> 369
36 38 49 60 71 73 3 14 25 -------> 369
37 48 59 70 81 2 13 24 35 -------> 369
---- ---- ---- ---- ---- ---- ---- ---- ----
369 369 369 369 369 369 369 369 369
Example 6
Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]
11
The Sum Matrix for 11 * 11 Order is
68 81 94 107 120 1 14 27 40 53 66 -------> 671
80 93 106 119 11 13 26 39 52 65 67 -------> 671
92 105 118 10 12 25 38 51 64 77 79 -------> 671
104 117 9 22 24 37 50 63 76 78 91 -------> 671
116 8 21 23 36 49 62 75 88 90 103 -------> 671
7 20 33 35 48 61 74 87 89 102 115 -------> 671
19 32 34 47 60 73 86 99 101 114 6 -------> 671
31 44 46 59 72 85 98 100 113 5 18 -------> 671
43 45 58 71 84 97 110 112 4 17 30 -------> 671
55 57 70 83 96 109 111 3 16 29 42 -------> 671
56 69 82 95 108 121 2 15 28 41 54 -------> 671
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
671 671 671 671 671 671 671 671 671 671 671
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
5
Q2. Document Correction and Alignment Problem [20%]
Nowadays, the natural language processing technology is increasingly more mature. Recently the
Beetles Phone Technology company released their latest smart phone, Beetles Resolver 66. The most
eye-catching part of their release conference is not their new smart phone, but rather, a speech
recognition input method Taxman input method.
Now suppose we have a document, and the content of which is generated by a speech recognition input
method. Due to the error of the speech recognition input method, some continuously duplicated words
appear in the content. Also, the format of the content is messy because of different length of each line
(see the content of D1.txt in example).
You are required to write a program to remove the word duplication and align the content based on the
following instructions:
1) Your program can assume that every word in the input document consist of at most 20 characters,
the input document may be jammed with empty line, but the whole input document represents one
single paragraph with at most 1000 words. Similarly, your output document should contain one
paragraph with at most 1000 words.
2) The given document is a text file, named as document.txt, and the output text file is called output.txt.
Your program should place it in the project directory of your VS C++ project.
3) Your program only needs to handle the duplication for a single word, like "The the the world". The
duplication for multiple words, like "The world the world", will not appear in the given document
read by your program. That is, if there is a consecutive sequence of same word (irrespective to the
use of capital letters and lowercase letters), then it is a duplication of that single word.
4) Your program can assume that the following characters consider as punctuations in an English
sentence: comma and full stop. All other characters are treated as a part of a word. In Example 2 on
next page, “(Friday)” is a word. In this case, “(Friday) Friday” has no word duplication and
your program needs not to correct this sequence of characters.
5) The number of the duplication is indefinite.
6) Each duplication of a single word should be replaced by a single occurrence of that word after your
correction. For example, "The the the world" should become "The world" after your correction.
7) After the alignment, each line should contain exactly 60 characters (contain the space and
punctuations), with the exception of the last line.
8) Your program can align the content by adjusting the number of words per line and the space
between the words. The space number between the words should be determined by the total space
available and the total words number in this line. There is no space between words and
punctuations.
a) E.g., if a line can contain 4 words and 3 spaces, then the amounts of space characters in
between two consecutive words on this line should be 1, 1, and 1.
b) E.g., if one line can contain 4 words and 5 spaces, then the amounts of space characters in
between two consecutive words should be 2, 2, 1.
9) After the alignment, each line should start with the word and end with the word or punctuations.
10) No word can be spited during the alignment process.
11) The result is required to be stored in a text file named as output.txt.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
6
Hints:
1. Your program may consider using a two-dimensional array to keep 1000 text strings, each
with a length sufficient to keep your required characters (including the end-of-string character
‘\0’)).
2. Your program may consider the logics outlined below.
a. It read the lines from the given text file line by line.
b. For each line, identify each word together with a possible ending punctuation
appending to it and keep that string into an entry in the above-mentioned twodimensional
array.
c. Now you keep the whole text document as a sequence of words in an array.
d. Your program should then compare whether two consecutive words in the array refer
to the same word. If you find a duplicated word, then you can use your strategy to
either remove that duplicated work from the array or mark the corresponding entry
in the array as “duplicated”.
e. Now, you can scan the array from the first entry and decide the length of each entry.
While your scanning, you can determine which words to be placed in the same line in
the output text file together with the number of space characters you needed to
separate the words in that output line.
f. You should then output that line to the text file.
g. Of course, before your program ends, it should close all files opened by it.
3. Your program may use strlen() and strcmp(), strcpy(). Note that in Visual Studio 2019, the
functions strcmp() and strcpy() are not allowed by default. See Slide 28 of the Lecture note
L08a to observe how to bypass the limitations. You may use strcmp_s() instead of strcmp()
and strcpy_s() instead of strcpy(), which are directly supported by VS 2019 by default.
4. Since duplicated occurrences of the same words (e.g., The the the) may be in different
combinations of uppercase and lowercase characters. Your program may firstly convert them
into all lowercases and then do a comparison. You may just assume that the first occurrence
is the one you wish to put into the text file output.txt.
5. A word in the given document may be appended with a full stop or a comma. Since it is a text
file, a line also ends with the end-of-line character (‘\0’). To identify a word, your program
should consider whether a word is ended with a full stop, a comma, or the end-of-line
character.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
7
Example 1:
Input document:
The the the town revolved around the river river. In summer,
when the blazing sun sun sun sun beat beat down, it dozed under the weight
of of sultry days. On Main Street a sow and her litter litter
litter of pigs might root along the wooden side walks, sharing the
deeply deeply rutted rutted roadway with foraging hens and
and and and a hound languidly scratching his fleas fleas.
Output Content of Output.txt is as follows
The town revolved around the river. In summer, when the
blazing sun beat down, it dozed under the weight of sultry
days. On Main Street a sow and her litter of pigs might
root along the wooden side walks, sharing the deeply rutted
roadway with foraging hens and a hound languidly scratching
his fleas.
Example 2:
Input document:
CS2311 Computer Programming
2020/21 Semester B
Department of Computer Science
City University of Hong Kong
Assignment Two
Due Date: 30 April 2021 (Friday) 23:59
All questions should be submitted in Canvas. Note that all solutions will
be assessed by (1) correctness, (2) programming styles (including
comments), and (3) non-redundancy in expressing solutions, if applicable.
This assignment contains 5 questions named as Q1 to Q5. Q5 has 10% bonus.
Your solutions are forbidden to INCLUDE any library other than
function/facility in other libraries will receive 0 mark. If possible, not
using
Output: Content of Output.txt is as follows
CS2311 Computer Programming 2020/21 Semester B Department
of Computer Science City University of Hong Kong Assignment
Two Due Date: 30 April 2021 (Friday) 23:59 All questions
should be submitted in Canvas. Note that all solutions will
be assessed by (1) correctness, (2) programming styles
(including comments), and (3) non-redundancy in expressing
solutions, if applicable. This assignment contains 5
questions named as Q1 to Q5. Q5 has 10% bonus. Your
solutions are forbidden to INCLUDE any library other than
libraries will receive 0 mark. If possible, not using
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
8
Q3. Pointer and Dynamic Array [20%]
The code on the next page is an executable solution for Exercise 5 of Tutorial 7 on Array.
Your task is to (1) rewrite the program to perform the same computation that meets the following
requirements. Note that you can use your own array-version programs as long as your program meets
the following requirements.
• Every array in the program is a dynamic array.
• Exceptthe statement in which it uses of the “new” operator (e.g., int* faces = new int[12];),
there is no any use of any array construct in the program. (hint: Use Pointers!)
o For example, all of the following array constructs in the code to refer to the elements in
the corresponding dynamic arrays are forbidden to be used.
sum[i + j - 1]
sum[k]
sum[index]
sorted[index][0]
sorted[index][1]
sorted[k][0]
sorted[k - 1][0]
sorted[k][1]
sorted[k - 1][1]
sorted[i][0]
sorted[i-1][0]
faces[sorted[0][1] - 1]
faces[sorted[i][1] - 1]
faces[k]
• Every dynamic array must be deleted before the program terminates.
o Hint: when deleting the 1D array faces, use delete: delete[] faces;
o Hint: when deleting a 2D array, you need to delete all the rows of the 2D array first, and
then the 2D array.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
9
#include
using namespace std;
int main() {
char faces[12] = { 0 };
int sum[12] = { 0 }; // max 12 values of occurrence counts
int sorted[12][2]; // a 2D array
// sorted[i][0] = sum of face values kept in sum[index]
// sorted[i][1] = index of sum[index]
for (int i = 1; i <= 6; i++) // i is dice 1
for (int j = 1; j <= 6; j++) // j is dice 2
sum[i + j - 1] = sum[i + j - 1] + 1;
for (int k = 0; k < 12; k++)
if (sum[k] != 0)
cout << sum[k]
<< " occurrence(s) of the sum "
<< k + 1 << endl;
// we firstly copy sum to rank
for (int index = 0; index < 12; index++)
{
sorted[index][0] = sum[index];
sorted[index][1] = index + 1;
}
// then we sort the array sorted[] using bubble sort
// check out the lecture note slide 31 of Lec07
// we want sorted[0] as the LARGEST after the sorting process
int tmp;
for (int j = 0; j < 12 - 1; j++) // outer loop
for (int k = 12 - 1; k > j; k--) // bubbling
if (sorted[k][0] > sorted[k - 1][0]) { // check against col 0
// note that the condition is ">" rather than "<".
tmp = sorted[k][0]; // swap neighbors for col 0
sorted[k][0] = sorted[k - 1][0];
sorted[k - 1][0] = tmp;
tmp = sorted[k][1]; // swap neighbors for col 1
sorted[k][1] = sorted[k - 1][1];
sorted[k - 1][1] = tmp;
}
for (int i = 0; i < 12; i++)
if (sorted[i][0] != 0)
cout << sorted[i][0] << ' ' << sorted[i][1] << endl;
char c = 'A'; // the 1st character to be shown
faces[sorted[0][1] - 1] = c;
for (int i = 1; i < 12; i++) {
if (sorted[i][0] != sorted[i - 1][0])
c++;
if (sorted[i][0] != 0)
faces[sorted[i][1] - 1] = c;
}
// print out the letter rank
for (int k = 0; k < 12; k++)
if (faces[k] != 0)
cout << faces[k] << " occurrence(s) of the sum " << k + 1 << endl;
return 0;
}
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
10
Example:
1 occurrence(s) of the sum 2
2 occurrence(s) of the sum 3
3 occurrence(s) of the sum 4
4 occurrence(s) of the sum 5
5 occurrence(s) of the sum 6
6 occurrence(s) of the sum 7
5 occurrence(s) of the sum 8
4 occurrence(s) of the sum 9
3 occurrence(s) of the sum 10
2 occurrence(s) of the sum 11
1 occurrence(s) of the sum 12
6 7
5 6
5 8
4 5
4 9
3 4
3 10
2 3
2 11
1 2
1 12
F occurrence(s) of the sum 2
E occurrence(s) of the sum 3
D occurrence(s) of the sum 4
C occurrence(s) of the sum 5
B occurrence(s) of the sum 6
A occurrence(s) of the sum 7
B occurrence(s) of the sum 8
C occurrence(s) of the sum 9
D occurrence(s) of the sum 10
E occurrence(s) of the sum 11
F occurrence(s) of the sum 12
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
11
Q4. Recursion
In tutorial T04, Exercise 4 asks students to develop a program using four nested loops. The tutorial
exercise is reproduced below.
Write a program without using any loop (i.e., without any while-loop, for-loop, or do-while loop) to solve
the same program. I.e., the program should accept a positive integer number N and outputs a sequence
of lines in the format of p = a^3 + b^3 = c^3 + d^3 where p, a, b, c, d are all distinct positive integers such
that p <= N and p = a3 + b3 = c3 + d3 and no two lines contains the same set of {a, b, c, d} (in the sense of
values).
Your program only needs to handle 0 ≤ N ≤ 1000000.
Your program should be efficient. For instance, when a user inputs a large number, your program should
output the correct list and terminates within a reasonable time (e.g., 1 minute). FYI, a correct program
can produce the output within a few seconds.
Important Hints:
• The solution of this question does not require to use any arrays.
• Students are advised to work on Exercise 4 in Tutorial 4, particularly following the slides 22-27
to resolve the subproblems stated on the slides. Or else your program will easily incur bugs.
• Your program will be at least four recursions, one on each of the four variables “a”, “b”, “c”, and
“d” to enumerate their combinations. The recursion on “d” is nested in the recursion on “c”,
which in turn is nested in the recursion on “b”, and which is also in turn nested on “a”.
• Students are advised to study the patterns in the reference solution of Exercise 4 in Tutorial 4. In
that reference solution, when looping on variable “b”, it needs the information of the current
value of the variable “a” within the loop body of this loop. Similarly, when looping on variable
“c”, it needs the information of the current value of variable “a”, and when looping on variable
“d”, it needs the information of the current values of variables “a”, “b”, and “c”.
Exercise 4: 1729 is Interesting
There was an Indian mathematician who was famous for his intuition for
numbers. When the English mathematician G. H. Hardy came to visit him in
the hospital one day, Hardy mentioned that the number of his taxi was 1729,
a rather dull number. To which the Indian mathematician replied, "No! It is a
very interesting number. 1729 is the smallest positive integer expressible
as the sum of two cubes in two different ways" .
Verify this claim by writing a program twoCubric.cpp that
takes an integer N and prints out all positive integers less than
or equal to N that can be expressed as the sum of two cubes in
two different ways: i.e., find distinct positive integers a, b, c,
and d such that N = a3 + b3 = c3 + d3.
hint: Use four nested for loops, one for each of a, b, c, d.
Let’s assume that N is not a very large number.
Read the next slide 20
Examples of Console for Ex4
21
Input: 1728
Input: 1729
1729 = 1^3 + 12^3 = 9^3 + 10^3
Input: 10000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
Input: 100000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
46683 = 3^3 + 36^3 = 27^3 + 30^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
40033 = 9^3 + 34^3 = 16^3 + 33^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
65728 = 12^3 + 40^3 = 31^3 + 33^3
64232 = 17^3 + 39^3 = 26^3 + 36^3
Check whether or not our solution contains duplicated entries.
1729 = 1 3 + 123 = 93 + 103
4104 = 2 3 + 163 = 93 + 153
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
12
Q5. Ash and Pikachu [20% + 10% Bonus]
In the Pokemon series of manga, Ash is the main character who joins force with Pikachu, a Pokémon, for
various adventure. We now have the Pokemon Go. In this question, we are going to implement some
of the concepts involved through C++ classes.
In this question, your program should allow a user to input: (1) a Pokemon world, (2) a list of Pokemons
and (3) [For Bonus] the information about a trainer called Ash.
Ensure that the output follows the format presented in Examples given at the end of this question.
Important note: Your program can assume the following: (1) the number of Pokemons is at least 1 and at
most 1000, (2) the name of any object is a cstring consisting of at most 10 characters (including ‘\0’), and
(3) all user inputs are valid.
• The concept of PokemonWorld is implemented as a C++ class.
o Each PokemonWorld object owns two PokemonStation objects. We simply refer to each
PokemonStation object of this class as a station.
o Each PokemonWorld object is responsible to initialize the two (2) stations.
(Note: The use of an array to keep two stations will make your program significantly more
complex than keeping them as two separate variables. The reason is due to our limited
known-how introduced in CS2311 to use object pointers rather than the limitation of C++.)
o When a Pokemon enters a PokemonWorld, the PokemonWorld is responsible to assign this
Pokemon to one of the two stations on its own. The assignment of each Pokemon will be done to
the PokemonStation in a round-robin manner (i.e., the first Pokemon is assigned to
PokmonStation1, the second Pokemon is assigned to PokmonStation2, the third Pokemon is
assigned to PokmonStation1 again, the fourth Pokemon is assigned to PokmonStation2 and so on).
o The ‘name’ variable of the PokemonWorld object can be accessed by the object itself.
o Hint: study http://stackoverflow.com/questions/12927169/how-can-i-initialize-c-objectmember-variables-in-the-constructor
• The concept of PokemonStation is also implemented as a C++ class.
o Each object of this class represents a station which has a station identifier. We simply refer to
each object of this class as a station. The station identifiers are s1 and s2, respectively.
o The identifier of each station is provided by the PokemonWorld object that owns the station
through the class constructor of PokemonStation. (Hint: You may wish to initialize each
PokemonStation when the constructor of PokemonWorld object is being called.)
o Each station maintains its own list of Pokemons assigned by the PokemonWorld object.
o Both the identifier of a PokemonStation and the list of Pokemons (i.e., each Pokemon that
belongs to a particular PokemonStation) should not be accessible by any other object nor be
returned via a function.
o Only PokemonWorld and the PokemonStation itself can invoke the functions of that station.
• The concept of Pokemon is also implemented as a C++ class.
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
13
o Each Pokemon is an object of this class.
o Each Pokemon object has its own name (a cstring) and hp value (an integer).
o All data members (name, hp) of each Pokemon object once initialized can only be accessed by
that Pokemon object.
• The main() function is responsible for the following:
o To accept all the inputs from the user.
o To create a PokemonWorld and all the Pokemons.
o To set each Pokemon to enter the PokemonWorld.
o To call the method print() of Pokemon, which in turn calls the method print() of each
PokemonStation, which in turn calls the method print() of each Pokemon to print out the
information of the PokemonWorld, the PokemonStation, and the Pokemon, respectively.
Bonus Part [Extra 10%]
• The requirements stated above should remain intact.
• A Trainer needs to be implemented as a C++ class.
o Each trainer (e.g. Ash) is an object of this class.
o Each trainer has his/her own name (cstring), identifier (integer), and strength (integer). All this
information is taken from the user as input from console.
o All data members of each trainer object cannot be accessed outside the trainer object. That is,
the value of each data member should not be returned via a function nor be retrieved via callby-reference.
• The main() function is responsible to create a trainer called Ash and let Ash enter the PokemonWorld.
• PokemonWorld is responsible to guide Ash to visit its two stations one by one once Ash enters this
world.
• Each station is responsible to determine whether a trainer can capture each Pokemon kept by the
station. Specifically, if the hp of the Pokemon is strictly less than (<) the strength of the trainer, then
the Pokemon is marked as captured by the trainer.
o Note that following the above requirements, both the hp of the Pokemon and the strength of
the trainer should not be returned via any function.
• The print() method of each captured Pokemon calls the print() method of the trainer object that
captures it to print the information of Ash.
More description about the above three classes
Ideas on the data members of the
Pokeworld class.
[The class may contain other data members]
Ideas on the data members of the
PokeStation class.
[The class may contain other data members]
Ideas on the data members of the
Pokemon class.
[The class may contain other data members
Name The name of this Pokeworld
object
ID The identifier of this station,
such as 1 and 2.
Name The name of this Pokemon
object
s1 1st station owned by the
Pokeworld
list An array of Pokemon hp The HP of the Pokemon
object
s2 2nd station owned by the
Pokeworld
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
14
Example 1: (without Bonus)
What is the World Name?
Macau
Please input the number of Pokemon in the Macau world
7
Please input the characteristics of all Pokemon: Name HP
Pikachu 50
Regice 100
Articuno 100
Charizard 150
Blastoise 240
Mew 340
Gengar 101
World is Macau
Pokemon in Station #1:
Pikachu HP 50
Articuno HP 100
Blastoise HP 240
Gengar HP 101
Pokemon in Station #2:
Regice HP 100
Charizard HP 150
Mew HP 340
Example 2: (without Bonus)
What is the World Name?
Hongkong
Please input the number of Pokemon in the Hongkong world
1
Please input the characteristics of all Pokemon: Name HP
Pikachu 50
World is Hongkong
Pokemon in Station #1:
Pikachu HP 50
No Pokemon in Station #2
CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021
15
Example 3: (with Bonus)
What is the World Name?
Macau
Please input the number of Pokemon in the Macau world
7
Please input the characteristics of all Pokemon: Name HP
Pikachu 50
Regice 100
Articuno 100
Charizard 150
Blastoise 240
Mew 340
Gengar 101
Enter Trainer information: ID NAME HP
1 Ash 101
World is Macau
Pokemon in Station #1:
Pikachu HP 50 Ash is its master
Articuno HP 100 Ash is its master
Blastoise HP 240
Gengar HP 101
Pokemon in Station #2:
Regice HP 100 Ash is its master
Charizard HP 150
Mew HP 340
*** END OF ASSIGNMENT TWO ***