DOC PREVIEW
UCLA PIC 10B - 10B_W09_exam1

This preview shows page 1-2 out of 7 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PIC 10B EXAM 1 January 26 2009 KEY Check your TA s name Aleka 10 00 Bobby 11 00 Josh 12 00 You have 50 minutes to complete this exam No books notes or calculators are allowed Show all work Incomplete illegible or unintelligible answers may receive no credit There are a total of 50 points on this exam The declaration of the IntMatrix class used in HW2 is provided for reference PAGE POSSIBLE POINTS 1 10 2 6 3 10 4 12 5 12 TOTAL 50 SCORE 1 1 10 points Suppose we are creating a class Jedi Write the single line declaration of each function below as it would appear in the Jedi class declaration Declare each function as a member function if possible a The comparison for two Jedi objects as in if J1 J2 bool operator const Jedi right const b The function to add two Jedi objects as in J1 J2 J3 Jedi operator const Jedi right const c The function to add onto a Jedi object as in J1 J2 Jedi operator const Jedi right d The input pull to read in a Jedi object s data as in cin J1 This function will need access to the Jedi class private data friend istream operator istream in Jedi right e The copy constructor for the Jedi class Jedi const Jedi right 2 2 4 points Darth remembers hearing the professor say you can figure out the Big O runtime by counting the loops Darth claims that both of the loops below run in O N time What is the correct runtime of each block of code below Explain your answer a for int i 1 i 200 i cout i This loop always prints out 200 numbers so the runtime is constant O 1 b for int j 0 j N N 3 j cout j This loop print N2 3 numbers so the runtime is O N2 3 2 points Darth Vader wrote a program using a class DeathStar which has dynamic arrays in its private variables Darth wants to name the destructor for his DeathStar class void destroy Why is this a bad idea and what should Darth call his destructor The destructor should be called DeathStar because this function is automatically called to destroy local variables at the end of every function With the name destroy this function will not be called automatically Note this same question appeared on the Winter 2008 exam 3 4 10 points Write a template function called getMin which takes a vector as input and returns the minimum value in the vector Your function should work on a vector containing an arbitrary data type e g int double string An example usage is shown below template typename T T getMin const vector T v T minValue v 0 for int i 1 i v size i if v i minValue minValue v i return minValue Will your getMin function work on a vector of IntMatrix Explain No because the operator is not defined for the IntMatrix class We would need to compare the values in the if statement 4 5 12 points To multiply a matrix by a scalar single number all entries in the matrix are multiplied by that value as illustrated below Write a non member function for the IntMatrix class that defines scalar multiplication You may assume that the scalar value is an integer and always appears on the left side of the symbol You may also assume the function was declared as a friend in the IntMatrix class declaration IntMatrix operator int left const IntMatrix right IntMatrix A right rows right cols for int i 0 i A rows A cols i A elements i left right elements i return A Could your scalar multiplication function be written as a member function Explain No because the value on the left side of the operator is an integer Operator overloading in a member function requires that the value on the left side of the operator is an object of the class 5 6 12 points Recall our IntMatrix class stores a 2D matrix by reading the values row by row into a 1D dynamic array called elements as illustrated below Write a member function called addRow for the IntMatrix class that adds one row to the bottom of the matrix turning a MxN matrix into a M 1 xN matrix All data values in the matrix should be preserved An example is shown below IntMatrix M 2 2 M 0 0 10 M 0 1 2 M 1 0 9 M 1 1 5 M addRow The directions don t specify so you could leave the extra row blank or with garbage values void IntMatrix addRow int temp new int rows 1 cols for int i 0 i rows cols i temp i elements i delete elements elements temp rows Don t forget to update rows return 6 The IntMatrix Class class IntMatrix public IntMatrix IntMatrix int r int c IntMatrix IntMatrix const IntMatrix right IntMatrix operator const IntMatrix right int operator int i int j int operator int i int j const friend IntMatrix operator const IntMatrix left const IntMatrix right friend IntMatrix operator const IntMatrix left const IntMatrix right friend IntMatrix operator const IntMatrix left int power friend ostream operator ostream out const IntMatrix right friend istream operator istream in IntMatrix right private int rows int cols int elements


View Full Document

UCLA PIC 10B - 10B_W09_exam1

Documents in this Course
Load more
Download 10B_W09_exam1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 10B_W09_exam1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 10B_W09_exam1 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?