0% found this document useful (0 votes)
24 views11 pages

The Hong Kong Polytechnic University: Department of Applied Mathematics

This document is an exam paper for the Principles of Programming course at The Hong Kong Polytechnic University. It consists of 17 questions focused on C++ programming concepts, including functions, classes, and algorithms, with a total time allowance of 2 hours. The exam is open book, with specific instructions on allowed materials and tools.

Uploaded by

D Wong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

The Hong Kong Polytechnic University: Department of Applied Mathematics

This document is an exam paper for the Principles of Programming course at The Hong Kong Polytechnic University. It consists of 17 questions focused on C++ programming concepts, including functions, classes, and algorithms, with a total time allowance of 2 hours. The exam is open book, with specific instructions on allowed materials and tools.

Uploaded by

D Wong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

© The Hong Kong Polytechnic University

THE HONG KONG POLYTECHNIC UNIVERSITY


DEPARTMENT OF APPLIED MATHEMATICS
_____________________________________________________________________

Subject Code: AMA2222


Subject Title: Principles of Programming

Programme: BSc(Hons) in Investment Science and Finance Analytics (63426)


BSc(Hons) Scheme in Data Science (63428)

Session: Semester 2, 2023/2024

Date: 11/05/2024 Time: 12:30 - 14:30

Time Allowed: 2 hours


_____________________________________________________________________

This question paper has 11 pages (including the cover page).


_____________________________________________________________________

Instructions to This question paper has 17 questions. Attempt all questions.


Candidates: All questions are based on C++.
You may assume necessary header files have been included.
The line using namespace std; has been included.
Unless under specification, the codes to be run are within main().
Open book exam with electronic materials on Blackboard only.
HKEAA approved calculator allowed.
Use Chrome only. Other browsers (Edge, ... ) are NOT allowed.
Programming development platform is NOT allowed.
Generative AI is NOT allowed.
Internet searching is NOT allowed.
Communication with others is NOT allowed.
_____________________________________________________________________

Subject Examiner: Leung Man Kin, Adam


_____________________________________________________________________

DO NOT TURN OVER THE PAGE UNTIL YOU ARE TOLD TO DO SO

1
© The Hong Kong Polytechnic University

Q1 (4 marks) Determine the output of the following statements.


int n = 1;
while (true) {
n *= 6;
if (n>100) break;}
cout << n;

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;

Q3 (4 marks) Determine the output of the following statements.


for (int i=10; i>0; i--)
if (i%2==1)
cout << i;

Q4 (4 marks) A function is defined as follows.


void update(int x, int &y) {
x++;
y--;}

Determine the output of the following statements.


int a = 3, b = 6;
update(a, b);
cout << a << b;

2
© The Hong Kong Polytechnic University

Q5 (4 marks) A function is defined as follows.


void f(int a[], int n) {
for (int i=0; i<n; i++)
cout << a[(i+2)%n];
}

Determine the output of the following statements.


int seq[6] = {1,4,2,8,5,7};
f(seq, 6);

Q6 (4 marks) A function is defined as follows.


int g(int n) {
if (n==1) return 1;
else return 2*n-1+g(n-1);}

Evaluate the return value of g(5).

Q7 (4 marks) Determine the output of the following statements.


int arr[9] = {11,22,33,44,55,66,77,88,99};
int* p = &arr[2];
int* q = p + 4;
cout << *q;

3
© The Hong Kong Polytechnic University

Q8 (4 marks) A function is defined as follows.


int h(int* a, int s){
if (s == 1) return (*a > 5);
else return (h(a, s/2) + h(a+s/2, s-s/2));
}

If we define an integer array as follows:


int list[8] = {3,0,6,2,4,7,7,0};

Evaluate the return value of h(list, 8).

Q9 (4 marks) Suppose card is a class defined as follows.


class card {
public:
char status;
int value;
card(char s, int v){
status = s;
value = v;}
void spend(int x){
if (x <= value) value -= x;}
};

Determine the output of the following statements.


card mycard('A', 250);
mycard.spend(100);
mycard.spend(200);
mycard.spend(30);
cout << mycard.value;

4
© The Hong Kong Polytechnic University

Q10 (4 marks) Suppose circle is a class with a subclass sector.


class circle {
public:
double radius;
paper(){}
paper(double r){
radius = r;}
};
class sector: public circle{
public:
double angle;
sector(double r, double a){
radius = r;
angle = a;}
double getArea(){
return 3.14*radius*radius*angle/360.0;}
};

Determine the output of the following statements.


sector S(2, 180);
cout << S.getArea();

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
}

Q13 (6 marks) Pell numbers 𝑃 is a sequence of integers defined as follows:


0, 𝑛 0
𝑃 1, 𝑛 1
𝑃 2𝑃 , 𝑛 1

The first few Pell numbers are 0,1,2,5,12,29, …

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)

b - right endpoint (double)

Under this class, the following should be included:


- a default constructor to create an openint object of the null set 0,0 ;
- a constructor to create an openint object with two input parameters (double), assigning a
and b to the smaller and bigger values respectively;
- a function print()that displays the open interval in the form (a,b) on the screen if a is
less than b; it displays null if a equals b; start a new line afterward;
- a function width()that returns the distance between the two endpoints;
- an operator * between two openint objects that returns a new openint object as the
intersection of the two open intervals.

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

The result of this program is shown below:


null
0
(2,5)
3

-------------------- END OF PAPER --------------------

11

You might also like