test aray

Study Observing Variables feature of MSVS described here. As you work on the below project, demonstrate to the instructor displaying local variable values with "Autos" and "Locals" tab. Create a project titled Lab10_TestArray. In the project, you should use the functions prototyped in this header file. You should place the functions you implement in a file named varArray.cpp. 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)

Last edited on
The header file is
#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
The main function is as follows and our code should work properly with it. Please need help.


#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[size]; // 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
*/
}
Topic archived. No new replies allowed.