cis330 name: winter 2021 final exam time limit: 120 minutes

23
CIS330 Name: Winter 2021 Final Exam 3/17/20, 02:45PM to 4:45PM Time Limit: 120 Minutes This exam contains 23 pages (including this cover page and extra sheets) and 7 questions. Total number of points is 100, excluding extra credit. This exam is printed on both sides. You are not allowed use any electronic devices of any kind. Only exceptions to this rule are i) to email the instructor with questions about the exam, or ii) to use Zoom to ask questions to the instructor, or iii) to use electronic pen and tablet/device to write your answers. Write your answers carefully and legibly. Re- member, partial answers are better than no answers. You are allowed one A4 double-sided cheat sheet. This MUST BE HAND-WRITTEN. All answers to this exam also MUST BE HAND-WRITTEN. Feel free to skip around and go back to an earlier question later. You may find it helpful to skim over the entire exam first and start with the easier ones, then move on to the more difficult ones. Remember to distribute your time appropriately among the questions. There is an extra credit question at the end - do not miss it! Good luck! Grade Table (for instructor use only) Question Points Score 1 10 2 15 3 6 4 10 5 8 6 18 7 33 Total: 100

Upload: others

Post on 06-Nov-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Name:Winter 2021Final Exam3/17/20, 02:45PM to 4:45PMTime Limit: 120 Minutes

This exam contains 23 pages (including this cover page and extra sheets) and 7 questions.Total number of points is 100, excluding extra credit. This exam is printed on both sides.

You are not allowed use any electronic devices of any kind. Only exceptions tothis rule are i) to email the instructor with questions about the exam, or ii) touse Zoom to ask questions to the instructor, or iii) to use electronic pen andtablet/device to write your answers. Write your answers carefully and legibly. Re-member, partial answers are better than no answers.

You are allowed one A4 double-sided cheat sheet. This MUST BE HAND-WRITTEN.All answers to this exam also MUST BE HAND-WRITTEN.

Feel free to skip around and go back to an earlier question later. You may find it helpfulto skim over the entire exam first and start with the easier ones, then move on to themore difficult ones. Remember to distribute your time appropriately among the questions.

There is an extra credit question at the end - do not miss it!

Good luck!

Grade Table (for instructor use only)

Question Points Score

1 10

2 15

3 6

4 10

5 8

6 18

7 33

Total: 100

Page 2: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 2 of 23 3/17/20, 02:45PM to 4:45PM

1 The C Language

1. (10 points) (12 minutes) Answer the following short-answer and multiple choice ques-tions.

(a) (3 points) Given the following code and execution command, at least how manybytes are lost due to memory leak?

#include <stdio.h>

#include <stdlib.h>

typedef struct {

int i;

int* j;

} mystruct;

int main(int argc, char** argv) {

mystruct* myarray = (mystruct*) malloc(sizeof(mystruct) * atoi(argv[1]));

for(int i = 0; i < atoi(argv[1]); i++) {

myarray[i].j = (int*) malloc(sizeof(int) * atoi(argv[2]));

}

return 0;

}

./a.out 10 20

(b) (4 points) Given the following piece of code, what will be printed?

A. 3 6 7 B. 3 6 76 4 9 4 6 97 5 9 7 5 9

C. 3 6 7 D. 3 6 64 6 9 6 4 96 6 9 6 6 9

Page 3: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 3 of 23 3/17/20, 02:45PM to 4:45PM

(c) (3 points) Which of the following is not a correct method for printing the ith ele-ment (where i starts from 1, and not 0) of an array A, defined below. Note thatfor option B, the number inside B[] is a 1, and not an i

int* A = (int*) malloc(sizeof(int) * 10)

2 Coding in C

2. (15 points) (20 minutes) Given a 2-D array of integers (e.g., int** arr) with m rowsand m columns, implement a function that rotates the array by 90 degrees counter-clockwise.

For example, for the given 3 × 3 matrix

1 2 34 5 67 8 9

Its 90 degrees counter-clockwise rotation would be

3 6 92 5 81 4 7

Things to note:

� You must use the function definition: void rotate arr(int** mat, int m);

� The rotated array must be stored in int** mat.

� You may use additional storage (i.e., a temporary array) to do the rotation.

� Continued on next page...

Page 4: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 4 of 23 3/17/20, 02:45PM to 4:45PM

� However, make sure your function does not create memory leaks (i.e., don’t forgetto free memory).

� Look at the extra credit question 1 before you answer this question. If you cando the rotation in-place (i.e., without using any additional memory, other than aswap variable), you can answer both questions with one implementation.

Hint: Think in terms of how rows are “moved.”

Page 5: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 5 of 23 3/17/20, 02:45PM to 4:45PM

This sheet is left blank on purpose

Page 6: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 6 of 23 3/17/20, 02:45PM to 4:45PM

3. (6 points) (5 minutes) Given an array of integers of length m, calculate its prefix sumin-place.Recall that for a given input array x, its prefix sum array y can be calculated by:

y0 = x0

y1 = x0 + x1

y2 = x0 + x1 + x2

...

(1)

Things to note:

� You must use the function declaration void prefix sum(int* A, int m);

� You must not use additional storage (e.g., no temporary array or even a swapvariable).

� You must not have redundant operations (i.e., operations that will have no impacton the result even if taken out).

Page 7: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 7 of 23 3/17/20, 02:45PM to 4:45PM

3 The C++ Language

4. (10 points) (15 minutes) Answer the following short-answer and multiple choice ques-tions.

(a) (3 points) Given the following piece of code, what will be printed?

Page 8: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 8 of 23 3/17/20, 02:45PM to 4:45PM

(b) (1 point) Given the following piece of code, what will be printed?

A. i is 10 j is 100i is 10 j is 1000

B. i is 10 j is 100i is 10 j is 100

C. i is 10 j is 1000i is 10 j is 1000

D. i is 10 j is 10i is 10 j is 10

E. None of the above

(c) (2 points) Given the following piece of code, which lines will cause a compilererror?

A. Only lines 3, 6, 7, and 8

B. Only lines 6 and 7

C. Only lines 7 and 8

D. Only lines 3 and 6

E. None of the lines will cause a compiler error

Page 9: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 9 of 23 3/17/20, 02:45PM to 4:45PM

(d) (2 points) Given the following piece of code, what will be printed?

Page 10: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 10 of 23 3/17/20, 02:45PM to 4:45PM

(e) (2 points) Given the following class definition, which of the following is not a cor-rect method for creating an array of myClass objects (or an array of pointers tomyClass objects) and initializing them with ii = i, where i is each object’s indexin the array.

Page 11: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 11 of 23 3/17/20, 02:45PM to 4:45PM

4 Coding in C++

5. (8 points) (12 minutes) Given the following class definition:

(a) (2 points) Provide the declaration for the destructor and the copy-constructorfor this class (just the declaration, and not the implementation).

Page 12: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 12 of 23 3/17/20, 02:45PM to 4:45PM

(b) (2 points) Implement the destructor for this class that will free memory allo-cated on the heap.

(c) (4 points) In the function void f(Test t) below, when t1 is passed in, the func-tion “destroys” t1 by deleting the array and setting n to 0. Implement the copy-constructor for this class that will prevent t1 from being destroyed (i.e., when theprogram returns from f(t1); t1 will still hold its original array)

Page 13: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 13 of 23 3/17/20, 02:45PM to 4:45PM

This sheet is left blank on purpose

Page 14: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 14 of 23 3/17/20, 02:45PM to 4:45PM

6. (18 points) (20 minutes) A digital image is made up of pixels (or picture elements). A300 × 300 image is made up of 300 × 300 = 90, 000 pixels, and each pixel is made up ofthree color components – red, blue, and green – which can be “combined” to makedifferent colors for the pixel. Each color component (r, g, and b) represents that color’s(red, green, and blue) intensity (between 0 to 255, inclusive) for the pixel. Given thefollowing class definition for a pixel:

(a) (5 points) Implement a parametric constructor that accepts red, green, and bluecolor values, and initializes the object appropriately. Assume that the constructoris implemented in the source code file (i.e., not inlined), and include any necessarynamespace and function header and parameters.Include both:

� the constructor declaration that should go in the class definition (where TODO

is), and

� the complete constructor implementation that should go in the source code file(i.e., .cc file).

Page 15: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 15 of 23 3/17/20, 02:45PM to 4:45PM

(b) (3 points) Implement the destructor. Assume that the destructor is implementedin a source code file, and include any necessary namespace and function header andparameters.

(c) (5 points) Implement the uchar addSamples(uchar , uchar); function. Thisfunction “adds” two color components (e.g., red added to red) and returns the re-sult. Here, addition means taking the average of the two components. Assumethat the function is implemented in a source code file, and include any necessarynamespace and function header and parameters.Hint: Maximum value that can be held by an unsigned char is 255.

Page 16: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 16 of 23 3/17/20, 02:45PM to 4:45PM

(d) (5 points) Overload the “+” operator, as declared in the class definition. The“+” operator is a binary operator that takes two Pixel objects and then producesanother Pixel object whose color components are the average of the correspondingcolor components of the two input Pixel objects. Note that:

� the operator is implemented in the source code file (i.e., not inlined), and includeany necessary namespace and function header and parameters.

� the function should use the addSamples function implemented in part (c).

Page 17: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 17 of 23 3/17/20, 02:45PM to 4:45PM

7. (33 points) (30 minutes) We are given a templated class for storing some array of data:

(a) (5 points) Implement the get size() and the get elem(unsigned int index)

functions. get size() returns the size of the array, and get elem(unsigned int

index) returns the pointer to the array element at position index. Make sure toinclude their function headers and any necessary namespace and functionparameters.

Page 18: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 18 of 23 3/17/20, 02:45PM to 4:45PM

This sheet is left blank on purpose

Page 19: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 19 of 23 3/17/20, 02:45PM to 4:45PM

(b) (6 points) We wish to use class Vect to store a 2-D array. However, our Vect classstores its data in a single 1-D array (i.e., in T* arr). Therefore we must convertthe 2-D array (with m rows and n columns) into a 1-D array (of size m × n), suchthat each row in the 2-D array is stored consecutively in a single row. See thefigure below for an example:

Implement the function for converting a 2-D array into a 1-D array, given thefollowing function declaration:template <class T>

void convert matrix(T** in, T** out, unsigned int m, unsigned int n)

, where in is the input 2-D array, out is a pointer to a 1-D array, and m and n are thenumber of rows and columns in the 2-D array, respectively. You MUST allocatememory for the variable out inside the function using new (i.e., not malloc).

Page 20: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 20 of 23 3/17/20, 02:45PM to 4:45PM

(c) (6 points) To store the 2-D array (that has been converted to a 1-D array in part(b)), we will declare a new inherited class:

This class stores a 2-D array of m rows and n columns as a 1-D array of size m × n.Implement the parametric constructor that takes in m, n, and a T* arr that storesthis matrix. T* arr would be the output generated by convert matrix(). Includeboth the constructor definition (that would go where TODO is) and its implementa-tion ss it would appear in the .cc file.

Page 21: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 21 of 23 3/17/20, 02:45PM to 4:45PM

(d) (8 points) Implement the get elem(unsigned int I, unsigned int J) functionfor class Mat. It returns the pointer to the element at position (I, J). Be sure toinclude the function header, and any necessary namespace and function parameters.

Hint: You will need to use previously implemented functions from the base class.

Page 22: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 22 of 23 3/17/20, 02:45PM to 4:45PM

(e) (8 points) Implement the polymorphed print() function for the class Mat. Youmust use the parent class’ print() function to print the matrix in such a way thateach row of the matrix is printed in its own line. For example, a 5×5 matrix printedusing this function should look like:

83 86 77 15 93

35 86 92 49 21

62 27 90 59 63

26 40 26 72 36

11 68 67 29 82

Hint: Create a temporary Vect and use its interface to print each row.

Page 23: CIS330 Name: Winter 2021 Final Exam Time Limit: 120 Minutes

CIS330 Final Exam - Page 23 of 23 3/17/20, 02:45PM to 4:45PM

Extra Credit 1 [5] Implement Question 2 (rotating a 2-D array) without using additionalstorage (i.e., in-place). You are allowed the use of one swap variable.Hint: Use algorithm(s) you have learned in this class.