Comp2011 f2024 Midterm Solution
Comp2011 f2024 Midterm Solution
Student Name
Student ID
Email Address
Problem Score
1 /4
2 /6
3 /8
4 /6
5 /6
6 /10
7 /15
8 /15
Total / 70
1
Problem 1 [4 points] C++ Operators
Solution:
4
5
3
3
3.1
0
2
Problem 2 [6 points] Function Call and Return
Solution:
(a) 8 -3 -3
2/3 point for each value (2 points total)
(b) Compilation error: passing const object ”c” as non-const function argument ”b” in func-
tion X.
1 point for writing ”error” and 1 point for the explanation (2 points total)
(c) 3 -3 -6
2/3 point for each value (2 points total)
3
Problem 3 [8 points] Control
Solution:
#include<iostream>
using namespace std;
enum Prize {First, Second, Third};
int main(){
char result, name;
int value;
if (name == 'a') {
if (value == First) {
result = 't';
} else if (value == Second || value == Third) {
result = 's';
} else {
result = 'e';
}
} else if (name == 'c') {
result = 'r';
} else {
result = '3';
}
return 0;
}
3pts for getting the outer switch correctly rewritten. 1 pt for each case. 3 pts for getting the
inner switch right. 1 extra point for the “hard case” which is value == Second || value
== Third
4
Problem 4 [6 points] Print Pattern
5
Solution: suggestion: Full mark if the pattern is correctly printed. If the pattern is not
correctly printed, then 0.5 mark for each correct answer.
#include <iostream>
using namespace std;
int main(){
functionv(7);
return 0;
}
6
Problem 5 [6 points] C String
Solution:
(a) 0 or false
(b) 1 or true
(c) 1 or true
7
Problem 6 [10 points] 2D Array
8
Solution:
#include <iostream>
using namespace std;
const int WHITE = 0;
const int BLACK = 1;
const int ROW = 6;
const int COL = 6;
/* 2 marks for the correct answer; and -0.5 for each missing or incorrect condition */
if (x < 0 || y < 0 || x >= ROW || y >= COL)
return 0;
/* 2 marks for the correct answer; and -2 for missing or incorrect answer */
grid[x][y] = WHITE;
/* 3 marks for the correct answer; and -3 for missing or incorrect answer */
int size = 1;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
size += CountBlob(grid, x+i, y+j);
/* 1 mark for the correct answer; and -1 for missing or incorrect answer */
return size;
}
int main(){
int grid[ROW][COL] = {{BLACK, BLACK, WHITE, WHITE, WHITE, WHITE},
{WHITE, BLACK, BLACK, WHITE, BLACK, BLACK},
{WHITE, WHITE, WHITE, WHITE, WHITE, WHITE},
{BLACK, WHITE, WHITE, WHITE, WHITE, WHITE},
{WHITE, BLACK, BLACK, BLACK, WHITE, BLACK},
{WHITE, WHITE, BLACK, WHITE, WHITE, WHITE}};
cout << "Blob size at (1, 4) is " << CountBlob(grid, 1, 4) << endl;
}
9
Problem 7 [15 points] 1D Array
Solutions:
10
/* Part (b): total 10 points
Note 1: The description stated that
A circular rotation is having the first element becoming the last element:
ith element becomes i-1th element
Check after some rotations whether the sequence of numbers is a palindrome
or not.
Therefore,
it is wrong to have i-1th element becomes ith element: -3 points
it is also wrong to consider only 1 rotation: -3 points
Note 2: no additional array should be defined.
-5 points for using additional array:
(even the code can check and returned correct results.)
Note 3: the 1st parameter is a constant array, it can not be modified.
- 5 points for having in-place rotations.
i.e. arr[i-1] = arr[i], etc.
-0.25 point for each minor syntax error (e.g. missing semicolon,
missing closing bracket, spelling mistakes),
and the max penalty is -1 points.
Same syntax error is counted once only.
*/
bool is_rotated_palindrome(const int arr[], int size) {
bool status;
int first, last;
/*** 3 points for considering rotations (i.e. 0 points for doing only 1 rotation) ***/
for (int i=0; i<size; i++) {
/* i correspond to number of rotations.
* e.g. i is 0 refers to the original positions, no rotations
* e.g. i is 1 refers to one rotation, only shifting the elements by 1 position
*/
status = true;
/*** 3 points for mapping the rotated indexes according to the num of rotations
in the correct direction, i.e. first element become last element
(i.e. 0 points if incorrect direction, or using additional array
or in-place rotation)
***/
first = i; // the index of the 1st element with i rotations
last = i + size - 1; // the index of the last element with i rotations
11
first++; last--;
}
if (status == true)
return status;
}
return false;
}
12
Problem 8 [15 points] Data Encryption
Solution:
correctly traverse plaintext (2)
correctly obtain the key character (or the index of that character) (3)
correctly compute the shift (1)
correctly encrypt alphabet with the shift(1)
handle space (1)
c string (handle null char) (1)
int i = 0;
while(plaintext[i] != '\0'){
char c = plaintext[i];
if (c == ' ') {
ciphertext[i] = ' ';
i++;
continue;
}
13
-------------------- END OF PAPER --------------------
14