22C:30, 22C:115 Solution to Exam 1

  1. Given in the matrix class.

  2. It only deletes the array that myMatrix is pointing to, however each slot in this array is a pointer pointing to an array of integers and these arrays are not deleted.

  3. Given in the matrix class.

  4. 2. Yes.

  5. When we use X[i][j] to access the slot in row i and column j, it is checked that i is within bounds, but not j.

  6. Given in the matrix class.

  7. (i) Check if an object is being assigned to itself and in this case avoid doing any work and (ii) Delete memory for the object on the left hand side of the assignment statement. Look at the implementation of the = operator given in the matrix class.

  8. Change the return type of the operator = function to matrix and return the object on the right hand side of the assignment operator at the end of the function.

  9. 		matrix< vector > X(10, 20);
    		for(int i = 0; i < 10; i++)
    			for(int j = 0; j < 20; j++)

  10. 		void matrix::resize(int newRows, int newCols)