Comp2011 Midterm2017f Soln
Comp2011 Midterm2017f Soln
Student ID
Email Address
Problem Score
1 / 10
2 /4
3 /7
For T.A. 4 /6
Use Only 5 /6
6 / 13
7 / 12
8 / 14
9 / 28
Total / 100
1
Problem 1 [10 points] Switch
Vowels are represented by the letters, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’. Consonants are represented by the
remaining 21 letters. In particular, two consonant letters, ‘w’ and ‘y’, are also regarded as
semi-vowels. For example, the word “happy” has 1 vowel, 4 consonants and 1 semi-vowel.
The word “birthday” has 2 vowels, 6 consonants and 1 semi-vowel.
Complete the following program that counts the number of letters representing vowels, semi-
vowels and consonants in an English word. You may assume that all the characters in the
word are lower case alphabets (i.e., ‘a’ to ‘z’). Note that you cannot use any if-statement in
your code.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int vowel = 0, semivowel = 0, consonant = 0;
char word[20];
cout << "Enter a word: ";
cin >> word;
//strlen() returns the length of the C string
for (int i=0; i<strlen(word); i++) {
switch ( ) {
}
}
cout << "num of vowels:" << vowel << endl;
<< "num of semi-vowels:" << semivowel << endl;
<< "num of consonants:" << consonant << endl;
return 0;
}
2
Solution:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int vowel = 0, semivowel = 0, consonant = 0;
char word[20];
3
Problem 2 [4 points] enum
#include <iostream>
using namespace std;
int main()
{
enum Fruits { MELON = 100, APPLE, ORANGE };
enum Vegatables { CARROT, TOMATO, CUCUMBER = 20, EGGPLANT };
return 0;
}
Answer: 101#1
4
Problem 3 [7 points] Operator
C++ has the special pre- and post-increment operator ++ for integers. Let’s implement
these 2 operators by 2 separate functions pre increment and post increment, respectively
so that the following program
#include <iostream>
using namespace std;
int main()
{
int x = 5, y = 10;
return 0;
}
result of pre_increment: 6
after pre_increment: x = 6
result of post_increment: 10
after post_increment: y = 11
Remarks:
i. You are not allowed to use the C++’s built-in (pre- and post-increment) ++ operators
in your answer.
ii. Both functions will take an integer variable as their only argument.
iii. You have to decide the exact function header of the 2 functions yourselves.
5
Answer:
(a) [3 points]
/* The definition of function pre_increment */
(b) [4 points]
/* The definition of function post_increment */
6
Solution:
/* (a) 3 points */
int pre_increment(int& a)
{
return (a += 1);
}
/* (b) 4 points */
int post_increment(int& a)
{
int original_a = a;
a += 1;
return original_a;
}
7
Problem 4 [6 points] Function parameter passing
Fill in the blanks with appropriate parameter-passing mechanism, so that the output of the
following program is 8 6 6.
#include <iostream>
using namespace std;
int func( a, b)
{
int temp, sum;
sum = a + b;
temp = a;
a = b;
b = temp;
return sum;
}
int main()
{
int a = 3, b = 5;
cout << func(b, a) << " ";
cout << func(a, b) << " ";
cout << func(b, a) << endl;
return 0;
}
8
Problem 5 [6 points] Functions
In each of the following programs, there is an error. Please identify the error and explain
how to correct it.
(a) [3 points]
#include <iostream>
using namespace std;
int main(){
print_user_sum(0.5, -0.5, 'A');
return 0;
}
Answer:
(b) [3 points]
#include <iostream>
using namespace std;
int main(){
mystery(0.5, '*');
return 0;
}
Answer:
9
Solution:
(a) [3 points]
#include <iostream>
using namespace std;
/*
* Possible correction 1: remove either of the following
* two functions.
*/
void print_user_sum(double x, int y, char user)
{
cout << user << " " << (x + y) << endl;
}
/*
* Possible correction 2: add the following function:
*/
void print_user_sum(double x, double y, char user)
{
cout << user << " " << (x + y) << endl;
}
int main()
{
/* Error: Compilation error as neither 1st or 2nd version
* is preferred over the other for the following
* function call.
*/
print_user_sum(0.5, -0.5, 'A');
return 0;
}
(b) [3 points]
#include <iostream>
using namespace std;
10
void mystery(double x, int y = 0, char z = '\0')
/* Possible correction 2: remove the default arguement
void mystery(double x, int y, char z)
*/
{
cout << x << y << z << endl;
}
/* Possible correction 3: remove the above function
*/
int main()
{
mystery(0.5, '*');
return 0;
}
11
Problem 6 [13 points] Loops
Implement a function hat(int height) which prints a hat pattern of a given height. Below
are example hat patterns with height = 1, 3, and 6, respectively. Note: the row numbers
below are not part of the program output; it is for your reference only.
Output of hat(1)
* row 1
Output of hat(3)
##*## row 1
#*#*# row 2
*###* row 3
Output of hat(6)
#####*##### row 1
####*#*#### row 2
###*###*### row 3
##*#####*## row 4
#*#######*# row 5
*#########* row 6
Answer:
12
Solution:
13
Problem 7 [12 points] Array and Loops
#include <iostream>
using namespace std;
if (j > k)
{
k++;
a[k] = a[i];
}
}
n = k + 1;
}
int main()
{
int arr[] = {58, 26, 91, 26, 70, 70, 91, 58, 58, 58, 66};
int size = sizeof(arr)/sizeof(int);
mysterious(arr, size);
Answer:
14
Solution:
58 26 91 70 66 size = 5
n = last_unique + 1;
}
15
Problem 8 [14 points] Array and Recursion
This question is about finding the maximum value among values stored in a multi-dimensional
array using recursion.
(a) Write the recursive function array max to return the maximum value stored in the first
n cells of a 1-dimensional integer array a of size N , where n ≤ N .
(b) Write the recursive function matrix max to return the maximum value stored in the sub-
matrix of a 2-dimensional integer array a with M rows and N columns as shown in Fig.
1. The sub-matrix’s dimensions are r × c where r ≤ M and c ≤ N .
Note: You may make use of the function array max from part (a) to solve part (b) (even
if you cannot answer part (a)).
16
Answer:
17
Solution:
if (R == 1)
return last_row_max;
18
Problem 9 [28 points] 2D Character Array
#include <iostream>
using namespace std;
int main()
{
const char dictionary[VOCAB_SIZE][WORD_LEN+1] = {
"and", "boy", "cats", "dogs", "evening",
"fun", "google", "honey", "let", "play" };
char words[MAX_NUM_WORDS][WORD_LEN+1] = { };
char sentence[LINE_LEN] = { };
int found_words_index[MAX_NUM_WORDS];
cout << "\nNumber of words in the sentence = " << num_words << endl;
for (int k = 0; k < num_words; ++k)
cout << "word " << k << " : " << words[k] << endl;
cout << "\nThe following words are found in the dictionary:" << endl;
print_found_words(words, found_words_index, num_words_found);
return 0;
}
is used as a dictionary to store a list of VOCAB SIZE words; each word has a maximum length
of WORD LEN. A sentence is read in by calling cin.getline once. To make things simple, it
19
is assumed that the user is very cooperative:
Afterwards, words are extracted from the input sentence into another 2-dimensional char
array, words, which are output to the screen. Those words are then spell-checked using the
given dictionary, and the program will save the indices of the correctly spelled words in
an int array found words index and print them out. For example, given the following input
sentence:
“let us play with my cute dogs and your lazy dogs this evening.”
20
Implement the 3 functions:
• get_words_from_sentence
• spell_check
• print_found_words
as well as a helper function called same_word which will be used by the function spell_check
so that the program runs with the expected output above.
Note:
(a) [4 points] Implement the function same_word which checks if two given C strings a and
b are the same; it returns true if so, otherwise false. Its function header is given to you.
(b) [10 points] Implement the function get_words_from_sentence which extracts words
from the given 1D char array, sentence, into a 2D char array, words. It should return
the number of extracted words.
(c) [10 points] Implement the function spell_check that checks which of the extracted words
from part (b) can be found in the dictionary. If found, their indices are saved into an int
array, found_words_index, and it returns the number of words found.
(d) [4 points] Implement the function print_found_words that prints out the words in the
sentence that can be found in the dictionary according to the above output format.
21
Answer:
22
(c) [10 points] /* The definition of function spell_check */
23
Solution:
/* (a) 4 points */
bool same_word(const char a[], const char b[])
{
int k = 0;
/* (b) 10 points */
int get_words_from_sentence(const char sentence[LINE_LEN],
char words[][WORD_LEN+1])
{
if (sentence[0] == PERIOD)
return 0;
int num_words = 0;
return num_words;
}
/* (c) 10 points */
int spell_check(const char dictionary[][WORD_LEN+1],
int dictionary_size,
24
const char words[][WORD_LEN+1],
int num_words,
int found_words_index[])
{
int num_words_found = 0;
return num_words_found;
}
/* (d) 4 points */
void print_found_words(const char words[][WORD_LEN+1],
int found_words_index[],
int num_words_found)
{
for (int k = 0; k < num_words_found; ++k)
cout << "word " << found_words_index[k] << " : "
<< words[found_words_index[k]] << endl;
}
25