DOC PREVIEW
UCLA PIC 10B - EXAM 1 KEY

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 February 1 2010 KEY Check your TA s name Rachel 10 00 Josh 11 00 David 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 Matrix class used in HW3 is provided for reference PAGE POSSIBLE POINTS 1 10 2 6 3 12 4 12 5 10 TOTAL 50 SCORE 1 4 8 points Suppose we are creating a class Yoda that uses dynamic memory so we should equip the class with the Big 4 functions Write the single line declaration for each function in the Big 4 as it would appear in the Yoda class declaration The Yoda class does not use any templates i Yoda ii Yoda iii Yoda const Yoda right iv Yoda operator const Yoda right 2 2 points What is the difference between the delete and delete commands The delete command deallocates the one block that the pointer points to The delete command is used for dynamic arrays to deallocate all blocks in the sequence 2 3 2 points For the IntMatrix class you created in HW2 the function operator function could not be written as a member function Explain why For operator overloading to be written as a member function the left side of the symbol must be an object of the class In operator the left side is of type ostream so it cannot be written as a member function 4 4 points Consider the recursive function vader below int vader int N if N 1 return 2 if N 2 return 1 return vader N 1 2 vader N 2 What does this function print if the main routine calls it with cout vader 5 Show work This defines the recurrence relation V1 2 V2 1 Vn Vn 1 2Vn 2 So the sequence is 2 1 5 7 17 So it prints the 5th number 17 3 The declaration for the Matrix class is given on the last page for reference 5 12 points For HW3 you wrote the Matrix operator function as a nonmember friend function to add the corresponding entries in two matrices A B C Performing similar entry wise addition write a templated member function for the Matrix class that defines the symbol as in A B Your code should not call the Matrix class operator function You may assume the two matrices being added are the same size and the entries are of the same data type Version 1 uses the array elements template typename T Matrix T Matrix T operator const Matrix T right for int i 0 i rows cols i elements i right elements i return this Version 2 uses the subscript i j template typename T Matrix T Matrix T operator const Matrix T right for int i 0 i rows i for int j 0 j cols j this i j right i j return this 4 The declaration for the Matrix class is given on the last page for reference 6 12 points The trace of a m by n matrix A is defined as the sum of the elements along the diagonal min m n trace A A ii i 1 Note the number of rows and columns do not have to be equal Write a member function called trace for the templated Matrix class that computes the trace of the matrix An example is shown below for a matrix of integers but your function should be templated to work for an other data types such as Matrix double and Matrix string Matrix int A 3 4 cout A trace Prints 7 Note the return value sum is of type T We cannot initialize sum 0 because we don t know the type T being added up The for loop stops at min rows cols whichever comes first template typename T T Matrix T trace const T sum this 0 0 for int i 1 i rows i cols i sum this i i return sum Will your trace function work for a Matrix vector int Explain No because the addition operator is not defined for vectors 5 7 10 points A palindrome is a word that is the same backwards and forwards as in the examples below madam racecar dood ablewasiereisawelba Write a function isPalindrome that returns true if the given string is a palindrome false otherwise An example is shown below isPalindrome dood returns true isPalindrome good returns false To receive full credit your function must be recursive and should contain no loops This example was in the Lecture 9 notes bool isPalindrome string s if s length 1 return true return s 0 s s length 1 isPalindrome s substr 1 s length 2 6 The Matrix Class template typename T class Matrix public Matrix Matrix int r int c Matrix Matrix const Matrix T right Matrix T operator const IntMatrix T right T operator int i int j T operator int i int j const template typename T friend Matrix T operator const Matrix T left const Matrix T right template typename T friend Matrix T operator const Matrix T left const Matrix T right template typename T friend Matrix T operator const Matrix T left int power template typename T friend ostream operator ostream out const Matrix T right template typename T friend istream operator istream in Matrix T right private int rows int cols T elements


View Full Document

UCLA PIC 10B - EXAM 1 KEY

Documents in this Course
Load more
Download EXAM 1 KEY
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 EXAM 1 KEY 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 EXAM 1 KEY 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?