Lab 10

Pointers, Dynamic Memory Allocation

There are two problems this week. These are similar to those assigned by Mikhail Nesterenko so be sure you do not obtain help from anyone in his class.

1) Dynamic Integer Array.
Create a program titled Lab10_DynamicInt. Write a program that dynamically stores and then prints an array of user-input integers. Your program should ask the user for the number of integers to be entered. Then the program allocates the appropriate-size array dynamically, stores the integers and then prints them. Here is an example dialog:

Enter number of integers: 5
Enter integers: -3 6 4 7 23
You entered: -3 6 4 7 23

2. Variable Size Array.
Create a program titled Lab10_VarArray. Write a program that adds to or removes from a collection of integers and then prints all of them in the increasing order. You can assume that the same number is never specified for a given array more than once. The program should allow more than one array to be input and manipulated during a run.

Use a file, vararray.txt, for input organized exactly as follows. The // are comments to explain entries for you.

# //signals a new array unless it is the last one in the file

a 5

a 8

r 8

a 3

a 12

# //requests a printout of the array and signals a new array i.e.print
r 2 // Your numbers: 3 5 12

a -5

a 11

r 6

a 2

a 9

a 3

r 11

# // Your numbers -5 2 3 9

The size of the user input can be arbitrarily large. For this program you need to implement an integer array whose size varies as necessary to accommodate the user input. You should use the functions prototyped in the header file below. The functions there are as follows:

addNumber() takes a pointer to the array and adds a number to the array. The pointer to the array is passed by reference. Note that the array cannot be enlarged to accommodate the number. Instead, this function needs to allocate a new array whose size is one more than the size of the old array, copy the contents of the old array to the new one, include the new number, deallocate the old array and then assign the address of the new array back to the original pointer.


Note that since the pointer is passed by reference, the call to this function results in effectively "enlarging" the array pointed to by the pointer.

removeNumber() takes a pointer to the array, a number, and removes the number if it is present in the array. If the number is removed, the array shrinks.

Note that the function does not change an array in any way if the number is not present in it. If the number is present, then the function should allocate a new array of smaller size and then copy all the numbers to it, except the one that needs to be erased. After copying is done, deallocate the old array and update the array pointer to point to the new array.

output() takes a pointer to the array and prints its contents in the increasing order. The operation of the function should be as follows: look for the smallest number in the array, print it and then erase it (using removeNumber()). Continue doing this until all elements of the array are removed. Note this is a selection sort that you have seen.


You may use the following when constructing your header file or you may choose to modify the header file and as you see fit. For example, you may consider defining a function that copies portions of the array.

// functions to manipulate variable size array
// Mikhail Nesterenko
// 10/29/2009

// adds "number" to the array pointed to by "array" of "size".
// Note the size of the array is thus increased. Note also
// that the pointer to the array is passed by reference
void addNumber(int *& array, int number, int &size);

// removes a "number" from the "array" of "size".
// if "number" not there -- no action
// note, "size" changes
void removeNumber(int *& array, int number, int &size);

// prints the values in "array" of "size" in sorted order
void output(int *const array, int size);


Make sure your programs adhere to proper programming style. Submit your files including at least one of your test files to the subversion repository. Do not forget to verify your submission on the web.