COS110 Tutorial 2 -26July2011-MEMO

COS110 Tutorial 2 -26July2011-MEMO - University of Pretoria...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 University of Pretoria COS110 Tutorial 2 MEMO-MEMO-MEMO Date: 27 July 2011 Time: 50 minutes Marks: 38 Student Surname and Initials:__________________________________________________ Student number:_______________________ Employee number:_____________________ Question 1: (Topic: dynamic memory allocation) [12 marks] Study the code below and answer the questions that follow: int main( void ) { const int INCREMENT = 5; int arrSize = 5; float *fValues = new float[arrSize]; int index; for (index = 0; index < arrSize; index++) fValues[index] = 0.5 * index; int newSize = arrSize + INCREMENT; float *newValues = new float[newSize]; //1. copy contents of array fValues to array newValues //2. set remaining values of array newValues to zero //3. de-allocate memory for array fValues //4. assign fValues to newValues and update arrSize variable //5. prevent dangling pointer cout << "array size is " << arrSize << endl; //output 1 for (index = 0; index < arrSize; index++) cout << "value at index " <<index << " is " << fixed << setprecision(1) << fValues[index] << endl; //output 2 return 0; }
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon