CS 112 Project 2: Pointers


  1. Practice using pointers.
  2. Practice using the debugger (if necessary).


This week's project is to add more functionality to the array.h and array.cpp files we used in this week's lab. In particular, you are to add the following functions to this "library":

  1. double sum(double *a, int size); This function should return the sum of the values in the array.
  2. void read(istream& in, double *a, int size); This function should read size values from in and store them in a.
  3. void fill(const string& fileName, double *&a, int &numValues); This function should (i) open an ifstream to the file named fileName, (ii) read an integer from the file into numValues, (iii) dynamically allocate an array of numValues double values, storing its address in a, (iv) read numValues values from the file, storing them in the dynamic array, and (v) close the ifstream.
  4. void resize(double *&a, int oldSize, int newSize); This function should change the size of the array from oldSize to newSize. If newSize > oldSize, all of the values in the old array must be copied into the new array. If newSize < oldSize, the end-elements of the array should be truncated. Don't forget to properly "dispose of" the old array, or your function will leak memory!
  5. void concat(double *a1, int size1, double *a2, int size2, double *&a3, int &size3); This function should pass back (i) array a3 containing the values of a1 followed by those of a2; and (ii) size3 equal to size1 plus size2. If the initial address in a3 is not NULL, then you should assume it is pointing at an "old" array and dispose of that old array, to avoid leaking memory.

You should use test-driven development to create these functions. To let you focus on understanding pointers, I have created ArrayTester.h, ArrayTester.cpp, main.cpp, and arrayData.txt to help you test what you write. Import these into your project, as the grader will use them to check your functions.

If you get your program to compile, but it fails a test and you cannot figure out what is wrong, use the debugger! Use it to draw a memory diagram and trace through the execution of the problematic function one statement at a time, until you identify the logic error.


CS > 112 > Projects > 02

This page maintained by Joel Adams.