0% found this document useful (0 votes)
5 views14 pages

Comp2011 f2024 Midterm Solution

The document outlines the COMP 2011 Midterm Exam for Fall 2024 at HKUST, detailing the exam date, time, and instructions for students. It includes a breakdown of the exam structure with 8 questions and a total score of 70 points, along with specific grading criteria for each problem. The exam is closed-book, requires answers in C++ without additional functions or libraries, and emphasizes correct formatting and syntax.

Uploaded by

Matthew NG
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)
5 views14 pages

Comp2011 f2024 Midterm Solution

The document outlines the COMP 2011 Midterm Exam for Fall 2024 at HKUST, detailing the exam date, time, and instructions for students. It includes a breakdown of the exam structure with 8 questions and a total score of 70 points, along with specific grading criteria for each problem. The exam is closed-book, requires answers in C++ without additional functions or libraries, and emphasizes correct formatting and syntax.

Uploaded by

Matthew NG
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/ 14

COMP 2011 Midterm Exam - Fall 2024 - HKUST

Date: October 26, 2024 (Saturday)


Time Allowed: 2 hours, 10:00-12:00noon
Instructions: 1. This is a closed-book, closed-notes examination.
2. There are 8 questions on 14 pages (including this cover page, honor code, and 4
blank pages for rough work at the end).
3. Write your answers in the space provided in black/blue ink. NO pencil please,
otherwise you are not allowed to appeal for any grading disagreements.
4. All programming codes in your answers must be written in the ANSI C++ version
as taught in the class.
5. For programming questions, you are NOT allowed to define additional helper
functions or structures, nor global variables unless otherwise stated. You cannot
use any library functions not mentioned, nor the auto keyword in defining identi-
fiers in the questions.

Student Name

Student ID

Email Address

Lecture & Lab Section

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

1pt for each of the numbers


-1pt penalty for incorrect formatting (e.g., missing newline)

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;

cin >> name; cin >> 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;

void functionv(int nrows){


int row, pound, star;

for(row=1; row <= nrows; row++){

for(pound=1; pound <= row-1; pound++)


cout << "#";
for(star=1; star<=2*(nrows-row)+1; star++)

if(star == 1 || star == 2*(nrows-row)+1)


cout << "*";
else
cout << "#";
for(pound=1; pound <= row-1; pound++)
cout << "#";

cout << endl;


}
}

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

Grading Scheme: 2 points for each line of correct output.

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;

int CountBlob(int grid[][COL], int x, int y){


/* 2 marks for the correct answer; and -2 for missing or incorrect answer */
if (grid[x][y] == WHITE)
return 0;

/* 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:

/* Part (a): total 5 points


* -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.
* Note: no additional array should be defined.
*/
bool is_palindrome(const int arr[], int size) {
// correctly checking palindrome and returning true: 3 points
// correctly checking not palindrome and returning false: 2 points
int first = 0, last = size-1;
while (first <= last) {
if (arr[first] != arr[last])
return false;
first++; last--;
}
return true;
}

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

/*** 2 points for checking palidrome ***/


while (first <= last) {
/*** 2 points for circulating back the indexes
(i.e. 0 points if using additional array or in-place rotation)
***/
if (arr[first % size] != arr[last % size]) { // %size to prevent out-of-bound
// and map to the actual element index
status = false;
break;
}

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)

void vigenereCipher(const char plaintext[], char ciphertext[],


const char key[]) {
int keyIndex = 0;
int keyLength = 0;
while(key[keyLength] != '\0'){
keyLength++;
}

int i = 0;
while(plaintext[i] != '\0'){
char c = plaintext[i];

if (c == ' ') {
ciphertext[i] = ' ';
i++;
continue;
}

char keyChar = key[keyIndex % keyLength];


char shift = keyChar - 'a';
ciphertext[i] = char(int('a' + (c - 'a' + shift) % 26));
keyIndex++;
i++;
}
ciphertext[i] = '\0';
}

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

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy