The Hong Kong Polytechnic University: Department of Applied Mathematics
The Hong Kong Polytechnic University: Department of Applied Mathematics
1
© The Hong Kong Polytechnic University
Q2 (4 marks) Determine the output of the following statements. Assume <string> has
been included in the header.
string word = "ABCDEFGH";
string phase = word.substr(0,1) + word.substr(2,3);
cout << phase;
2
© The Hong Kong Polytechnic University
3
© The Hong Kong Polytechnic University
4
© The Hong Kong Polytechnic University
5
© The Hong Kong Polytechnic University
Q11 (6 marks) Suppose allzero() is a Boolean function with an integer array a[] and its
size n as input parameters. It returns true if all elements in a[] are zero. Complete this
function.
bool allzero(int a[], int n){
// insert function body
}
Q12 (6 marks) The function below inputs a two-dimensional double array b[m][n]which
represents a matrix of m rows and n columns. Suppose m and n are pre-defined global
constants. The function returns the sum of all entries in the matrix. Complete this function:
double matrixsum(double a[m][n]){
// insert function body
}
Write a function pell() with an integer input n. It returns the n-th Pell number. You need
to use recursion in writing this function otherwise no marks will be given.
int pell(int n){
// insert function body
}
6
© The Hong Kong Polytechnic University
Q14 (8 marks)
Consider the exam score (integer between 0 to 100) of a certain number of students. The
teacher would like to figure out the score of the x-th position student counting from the top.
For instance, the top-1 student should have the highest score. Write a function top() with
an integer x, an integer array a[] and its size n as input parameters, which returns the score
of the top-x student. You may assume that x is ranged from 1 to n. The main program has
been provided. Refer to the sample below. You may use any sorting algorithm you have
learnt but not the built-in sort function.
#include<iostream>
#include<string>
using namespace std;
// insert function top
int main() {
int num, pos;
cout << "Enter the number of students: ";
cin >> num;
int score[num];
cout << "Enter the scores: ";
for (int i=0; i<num; i++) cin >> score[i];
cout << "Enter the position: ";
cin >> pos;
cout << "Top-" << pos << " score is ";
cout << top(pos, score, num);
return 0;}
Sample:
Enter the number of students: 6
Enter the scores: 67 42 98 53 81 74
Enter the position: 3
Top-3 score is 74
7
© The Hong Kong Polytechnic University
Q15 (8 marks) A particle is located at position x=0 at the initial time t=0 in a one-
dimensional space. At each time step, there are equal chances that it will stay, move right, or
move left by one unit. Simulate its position for the time from t=1 up to t=10.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
// insert your coding
return 0;
}
A sample is shown below. Notice that the sample is generated randomly which might give
different results for each execution.
1 2 2 1 0 1 0 0 -1 -2
8
© The Hong Kong Polytechnic University
Q16 (10 marks) Suppose you received a supermarket cash coupon. To fully utilize its value
without paying any surplus, you are going to buy a certain number of products, with a total
price exactly equal to the coupon value. You would like to know how many possible
combinations are there. The program below prompts the user to enter the coupon value,
number of products and the price of each product. You may assume all these input parameters
are positive integers and each product has indefinite supply. The function returns the number
of combinations. Write this function using dynamic programming.
#include<iostream>
using namespace std;
//insert function comb
int main(){
int coupon, number;
cout << "Enter the coupon value: ";
cin >> coupon;
cout << "Enter the number of products: ";
cin >> number;
int price[number];
cout << "Enter the prices: ";
for (int i=0; i<number; i++) cin >> price[i];
cout << "The number of combinations is: ";
cout << comb (coupon, number, price);
return 0;}
For example, consider a coupon of $10 and three products of $2, $3 and $5. There are four
possible combinations: {2,2,2,2,2}, {2,2,3,3}, {2,3,5}, {5,5}. Ordering doesn’t matter. Notice
that you can buy the same product multiple times.
As an example, the result of the program should be:
Enter the coupon value: 10
Enter the number of products: 3
Enter the prices: 2 3 5
The number of combinations is 4
9
© The Hong Kong Polytechnic University
Q17 (16 marks) An open interval 𝑎, 𝑏 on the real number line is defined as follows:
𝑎, 𝑏 𝑥 ∈ ℝ|𝑎 𝑥 𝑏
which can be characterized by its left end point a and right end point b. With a less than b,
the interval is non-empty. Notice that the special case of a equals b gives a null set (not a
single point), which is also an open interval. Notice that the intersection of two open intervals
is also an open interval. The intersection is a null set if they do not overlap.
Write a class called openint that represents an open interval. It should contain the
following variables in the data field:
a - left endpoint (double)
You are only required to write the class. You may put both the data field and functions in the
public section. The main program is provided to you.
#include<iostream>
using namespace std;
//insert class openint
int main(){
openint p(1,2), q(5,1), r(2,6);
openint s = p * r;
openint t = q * r;
s.print();
cout << s.width() << endl;
t.print();
cout << t.width() << endl;
return 0;}
10
© The Hong Kong Polytechnic University
11