Comp2011 s2024 Midterm Questions
Comp2011 s2024 Midterm Questions
Question Book
Student Name
Student ID
Problem Score
Loops 8
Finding Bugs 12
Recursion 10
C Strings 30
Total 100
1
Problem 1 [8 points] Loops
bool check_sth(int x)
{
if (x < 0)
return false;
int y = 0, temp = x;
while (temp)
{
y *= 10;
y += (temp % 10);
temp /= 10;
}
return (y == x);
}
Given the above function, what is the returned value for each of the following function calls?
(a) check_sth(153)
(b) check_sth(13531)
2
Problem 2 [9 points] enum and switch
#include <iostream>
using namespace std;
int main()
{
enum Grade {A, B, C = 3, D};
char c;
cin >> c;
switch (c - 'a')
{
case A:
cout << "Excellent" << endl;
break;
case B:
cout << "Very Good" << endl;
break;
case C:
cout << "Good" << endl;
break;
case D:
cout << "Satisfactory" << endl;
break;
default:
cout << "Pass" << endl;
}
return 0;
}
For each input below, what is the corresponding output of the above program?
(a) Input: b
(b) Input: d
(c) Input: 2
3
Problem 3 [9 points] Function Parameter Passing
1 #include <iostream>
2 using namespace std;
3
When the comment line #4 of the above program is replaced by each of the following function
definitions, give the corresponding output.
4
Problem 4 [12 points] Finding Bugs
In each part below, there is a compilation error. Write down the line number of the code
that causes the error, and give a brief explanation of why the program cannot be compiled.
(a)
1 #include <iostream>
2 using namespace std;
3
10 int main()
11 {
12 int numbers[3] = {1, 2, 3};
13 add_one(numbers, 3);
14 return 0;
15 }
(b)
1 #include <iostream>
2 using namespace std;
3
9 int main()
10 {
11 char arr[2][] = { "c ++", "cold" };
12 set(arr[1]);
13 cout << arr[1] << endl;
14 return 0;
15 }
5
(c)
1 #include <iostream>
2 using namespace std;
3
4 void add_one(double& x)
5 {
6 x += 1;
7 }
8
9 int main()
10 {
11 int n = 10;
12 add_one(n);
13 cout << n << endl;
14 return 0;
15 }
(d)
1 #include <iostream>
2 using namespace std;
3
10 int main()
11 {
12 int a = 10;
13 add_one(add_one(a));
14 return 0;
15 }
6
Problem 5 [10 points] Recursion
#include <iostream>
using namespace std;
const int N = 4;
print(temp, n - 1);
int main()
{
int A[N] = {1, 2, 3, 4};
print(A, N);
return 0;
}
7
Problem 6 [22 points] 2D Array and Function
We can write a program to implement the Sudoku game. Complete the following incomplete
program by writing the three functions required in parts (a)-–(c).
8
#include <iostream>
using namespace std;
const int N = 9;
const char BLANKSPACE = ' ';
const char COLBREAK[] = "| ";
const char LINEBREAK[] = "------+-------+------";
int main()
{
// 0 is for empty cell in Sudoku puzzle
int data[N * N] = {5, 3, 0, 0, 7, 0, 0, 0, 0,
6, 0, 0, 1, 9, 5, 0, 0, 0,
0, 9, 8, 0, 0, 0, 0, 6, 0,
8, 0, 0, 0, 6, 0, 0, 0, 3,
4, 0, 0, 8, 0, 3, 0, 0, 1,
7, 0, 0, 0, 2, 0, 0, 0, 6,
0, 6, 0, 0, 0, 0, 2, 8, 0,
0, 0, 0, 4, 1, 9, 0, 0, 5,
0, 0, 0, 0, 8, 0, 0, 7, 9};
// Sudoku grid
int grid[N][N];
if (isValidSudoku(grid))
{ // check whether a Sudoku is done
cout << "Congratulations! You solved the Sudoku!" << endl;
printSudoku(grid);
}
else
cout << "Oops, something is wrong." << endl;
return 0;
}
9
Note:
• Although in the given main program, the puzzle is initialized with some hard-coded
values, your solution should work with any properly initialized Sudoku puzzles.
• You only need to implement functions in tasks (a) to (c), and assume that the other
functions have been implemented.
10
Invalid number
input row, col, num
1 2 9
Invalid number
input row, col, num
11
(a) (4 points) Implement the function loadSudoku, which initializes a 2D Sudoku grid[N][N]
from 1D data[N].
(b) (8 points) Implement the function printSudoku, which prints the 2D Sudoku grid. Refer
to the sample output below. Make sure your implementation can produce exactly the
same output in the same format.
5 3 0 | 0 7 0 | 0 0 0
6 0 0 | 1 9 5 | 0 0 0
0 9 8 | 0 0 0 | 0 6 0
------+-------+------
8 0 0 | 0 6 0 | 0 0 3
4 0 0 | 8 0 3 | 0 0 1
7 0 0 | 0 2 0 | 0 0 6
------+-------+------
0 6 0 | 0 0 0 | 2 8 0
0 0 0 | 4 1 9 | 0 0 5
0 0 0 | 0 8 0 | 0 7 9
(c) (10 points) Implement the function isValidNum, which checks whether the input number,
num, is valid for the specified spot (row, col) in the current Sudoku grid.
Note:
• You may assume that the inputs, row and col, are always valid (e.g. locate an empty
spot in the Sudoku grid).
• Read carefully the basic rules of the Sudoku game. Check program output to under-
stand how to decide a num is valid or not.
12
Problem 7 [30 points] C Strings
C++ uses 1D character array with the null character end-marker to represent strings which are
called C strings. All character arrays in this question are used to store C strings of length less
than N. You are required to write 5 functions to deal with C strings with the given function
headers. Below is an example program that demonstrates how they work. Your solution
should work for any value of N though the program set it to 100.
Note: You are not allowed to define or create any additional arrays and helper function(s),
nor include additional libraries. In particular, you are NOT allowed to use any C string
functions in the cstring library. On the other hand, a function required by a later part may
call functions written for earlier parts.
#include <iostream>
using namespace std;
const char NULL_CHAR = '\0';
int main()
{
const int N = 100;
char s1[N], s2[N], str_sum[N], pattern[N];
int start, end, length;
while (true)
{
cout << "Enter a string : ";
cin >> s1;
cout << "Enter the substring to search in the form of \"a*b\": ";
cin >> pattern;
just_letters(s1);
cout << "s1 after calling just_letters: " << s1 << endl;
to_lower_case(s1);
cout << "s1 after calling to_lower_case: " << s1 << endl;
return 0;
}
13
A sample run of the program is shown below.
--------------------------------
Enter a string : hongkonguniversity
Enter the substring to search in the form of "a*b": n*n
substring: 8 ; 2 , 9
s1 after calling just_letters: hongkonguniversity
s1 after calling to_lower_case: hongkonguniversity
Enter another string to add: aaabbb
ipoimqnguniversity
--------------------------------
Enter a string : 2024Scomp2011LA5##
Enter the substring to search in the form of "a*b": 4*2
substring: 7 ; 3 , 9
s1 after calling just_letters: ScompLA
s1 after calling to_lower_case: scompla
Enter another string to add: ZZZZzzzz
scomplaz
--------------------------------
Enter a string : 2024Scomp2011LA5##
Enter the substring to search in the form of "a*b": S*1
substring: 9 ; 4 , 12
s1 after calling just_letters: ScompLA
s1 after calling to_lower_case: scompla
Enter another string to add: abcd
terqpla
--------------------------------
Enter a string : 2024Scomp2011LA5##
Enter the substring to search in the form of "a*b": S*c
substring: 2 ; 4 , 5
s1 after calling just_letters: ScompLA
s1 after calling to_lower_case: scompla
Enter another string to add: yyyyY
rbnlola
--------------------------------
14
(a) A function with the following header:
bool is_letter(char c)
to_lower_case takes a C string as input that consists of just English letters, and converts
any of its upper-case letters to their lower-case letters.
substring takes in two C strings, s and pattern. The pattern always contains 3 char-
acters of the form a*b, where a and b represent any ASCII characters which are not
necessarily English letters, and they may even be the same. Here, * means zero or more
ASCII characters. The function looks for the longest match of the pattern a*b in the C
string s, and records the first appearance of the character represented by pattern[0] in
the parameter start and the last appearance of the character represented by pattern[2]
in the parameter end (and pattern[1] is always ‘*’). It also returns the length of the
matched substring.
just_letters removes all characters in the input C string s that are not letters, and
shifts all of its letters forward to the spaces left by the removed characters. After the
function is called and when s is printed, only the letters in the original C string will be
printed and their order should be the same as their order before the function call.
add_strings adds the corresponding characters of the 2 input C strings s1 and s2 to the
parameter str sum. The two C strings may have different lengths, and the remaining
characters of the longer C string will be copied to str sum.
Assumption: The two input C strings, s1 and s2, contain only letters in lower case.
15
/* Rough work */
16
/* Rough work */
17
/* Rough work */
18