CS 112 Project 2: Pointers
- Practice using pointers.
- 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":
double sum(double *a, unsigned size);
This function should return the sum of the values in the array.
void read(istream& in, double *a, unsigned size);
This function should read size values from in
and store them in a.
void fill(const string& fileName, double *&a, unsigned &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.
void resize(double *&a, unsigned oldSize, unsigned 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,
only the first newSize elements of the array can be copied;
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!
void concat(double *a1, unsigned size1, double *a2, unsigned size2,
double *&a3, unsigned &size3);
This function should take in arrays a1 and a2,
of sizes size1 and size2 respectively, and 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
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.
An electronic copy of your project, in your
A hard copy of
a script file
in which you use ls to list all your project's files,
use cat to display the contents of each file,
and build+run your project, with
this grade sheet stapled to the front.
This page maintained by