I need help with a project that requires the following.
"The functions to be implemented are as follows:
output() takes the pointer to the array and its size and prints the arrays' contents
check() takes the pointer to the array and a number and checks if the number is in the array. If the number is in the array, returns the index of the element of the array holding the number. Otherwise, returns -1.
addNumber() takes a pointer to the array and adds a number to the array if the number is not already there. 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 should 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.
Copying and removing one element may be done in a single for-loop. Here is a pseudocode for the solution:
create a boolean variable "found", set it to to false
iterate over elements in the old array,
if old array element equals to the number to be removed then
set found to true
else
if not found then
copy old array element to the new array element
else // found
copy old array element to the new array
element with index one less than old array's
(move them to the left)
The code in a header file that it must work with is as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef VARARRAY_H
#define VARARRAY_H
// prints the values in "arrayPtr" of "size"
void output(int *arrayPtr, int size);
// returns the index of the element in "arrayPtr" of "size"
// that corresponds to the element holding "number"
// if number is not in the array, returns -1
int check(int *arrayPtr, int number, int size);
// adds "number" to the array pointed to by "arrayPtr" of "size".
// if the number is not already there, if "number" is there - no action
// Note, the size of the array is thus increased.
void addNumber(int *& arrayPtr, int number, int &size);
// removes a "number" from the "arrayPtr" of "size".
// if "number" is not there -- no action
// note, "size" changes
void removeNumber(int *& arrayPtr, int number, int &size);
#endif // VARARRAY_H
|
It must also work with the following test code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include "varArray.h"
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main(){
int size=5; // setting array size
int *a = new int[5]; // allocating dynamic array
// initializing array
a[0] = 0; a[1] = 10; a[2] = 20; a[3] = 30; a[4] = 40;
output(a, size); // printing out the array
/*
// asking user to input a number
cout << "Input number to search for: ";
int number;
cin >> number;
// checking if the input number is in the array
int index = check(a, number, size);
if (index == -1)
cout << "The number is not in the array" << endl;
else
cout << "The number is at position " << index << endl;
// adding a new number to array
addNumber(a, 55, size);
cout << "Array after adding a new number: "; output(a, size);
// adding a duplicate number to array
// the function should not add it
addNumber(a, 20, size);
cout << "Array after adding existing number: "; output(a, size);
//
removeNumber(a, 10, size);
cout << "Array after removing number: "; output(a, size);
delete [] a; // deallocating the array
*/
}
|
I've done well so far in this course until projects like this ask to write code that works with preexisting code and then it becomes confusing to me. Any help is really appreciated.