Pointers, Dynamic Memory Allocation Test Array. Create a project titled Example1
ID: 3690397 • Letter: P
Question
Pointers, Dynamic Memory Allocation
Test Array. Create a project titled Example10_TestArray. In the project, you should use the functions prototyped in //Figure 1: 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:
Your code should work with // Figure 2 test file
Hint: Study example code ////Figure 3 destructor, ///Figure 4 copy constructor, and //Figure 5 assignment overloading to observe the techniques to be used for the above functions.
Hint 2: int *p=new int[0]; works correctly. This allows to avoid treating the array of size zero in the above functions specially.
Variable Size Array. Create a project titled Lab10_VarArray. Write a program that allows the user to add or remove an integer number to a collection of integers and then prints all of them. A user may enter the number twice in which case, the repeated number should not be entered.
Here is an example program dialog:
The size of the user input can be arbitrarily large. For this program, to accommodate user input, you need to implement an integer array whose size varies as necessary. The numbers in the array should not be sorted.
//Figure 1: header file
//varArray.h
#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
// Figure 2 test file
#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
*/
}
////Figure 3 destructor
// demonstrates destructor with object containing array
class MyClass{
public:
MyClass(int); // constructor
~MyClass(); // destructor
private:
int *d;
int size;
};
MyClass::MyClass(int n){
size=n;
d = new int[size];
for(int i=0; i< size; ++i) d[i]=0;
}
MyClass::~MyClass(){
delete [] d;
}
// standalone function using an object of MyClass
void otherfunc(){
MyClass myobj(5); // constructor allocates dynamic array
} // destructor is implicitly invoked here
int main(){
otherfunc();
}
///Figure 4 copy constructor
class MyClass{
public:
MyClass(int); // regular constructuctor
MyClass(const MyClass&); // copy constructor
void addNum(int); // regular member function
private:
int *d;
int size;
};
MyClass::MyClass(int n){
size=n;
d = new int[size];
for(int i=0; i< size; ++i) d[i]=0;
}
MyClass::MyClass(const MyClass& org){
size=org.size;
d = new int[size];
for(int i=0; i< size; ++i) d[i]=org.d[i];
}
// adds "n" to every element of the array
void MyClass::addNum(int n){
for(int i=0; i < size; ++i)
d[i] += n;
}
// standalone function taking an object of MyClass by value
void otherfunc(MyClass newobj){
// nothing is done in the function
}
int main(){
MyClass myobj(5); // constructor allocates dynamic array
myobj.addNum(10); // adding ten to elements
otherfunc(myobj);
}
//Figure 5 assignment overloading
class MyClass{
public:
MyClass(int); // regular constructuctor
MyClass& operator= (const MyClass&); // overloaded assingment
void addNum(int); // regular member function
private:
int *d;
int size;
};
MyClass::MyClass(int n){
size=n;
d = new int[size];
for(int i=0; i< size; ++i) d[i]=0;
}
MyClass& MyClass::operator= (const MyClass& rhs){
if (this != &rhs){ // if not same
size=rhs.size;
delete [] d;
d=new int[size];
for (int i=0; i < size; i++)
d[i]=rhs.d[i];
}
return *this; // return lhs
}
// adds "n" to every element of the array
void MyClass::addNum(int n){
for(int i=0; i < size; ++i)
d[i] += n;
}
int main(){
MyClass firstObj(5); // constructor allocates dynamic array
firstObj.addNum(10); // adding ten to elements
MyClass secondObj(22), thirdObj(33);
firstObj = firstObj; // self-assignment
thirdObj = secondObj = firstObj; // stacked assignment
}
Explanation / Answer
//Figure 1: header file
//varArray.h
#include <iostream>
#ifndef VARARRAY_H
#define VARARRAY_H
FUNCTION 1:
// prints the values in "arrayPtr" of "size"
void output(int *arrayPtr, int size){
std:cout << endl;
//EXPLANATION:
//To output all the content of an array we only need to
//go through every element and print it's value.
//The variable "arrayPtr" it's the pointer to the array we want to
//inspect, and "size" the amount of element inside arrayPtr.
//Remenber that arrays indexes start from 0.
for(int i=0; i<size; i++){
std::cout << arrayPtr[i] << endl;
}
}
//FUNCION 2:
// 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){
//EXPLANATION:
//Just like the "output" function, wee need to go through arrayPtr,
//but in this case we "check" if number is inside the array.
for(int i=0; i<size; i++){
//Here we check if element by element number is inside the array.
if(number==arrayPtr[i]){
//If found we take advantage of the index which we use to inspect
//the array and return it.
return i;
}
}
//If number does not find a match, the function returns -1.
return -1;
}
//FUNCTION 3:
// 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){
//EXPLANATION:
//To speedup your development times and increase your code maintainability,
//we can take advantage of the DRY principle (Don't Repeat Yourself) to quickly
//find out if number is in arrayPtr by using a function we already defined.
//I am taking about "check".
//Remember, when a number is not inside arrayPtr, the check function
//returns -1.
if(check(arrayPtr, number, size) == -1){
//If the condition is met, create a new array with space for an additional element.
int *array = new int[size + 1];
//We will insert the new number in the first position of the array.
array[0] = number;
//Next, will insert all the elements of the original array inside the new one.
for(int i=0; i<size; i++){
array[i+1] = arrayPtr[i];
}
//Finally, the new array it's assigned to the old one's direction to override it.
//Note: this is only possible because of the "*&" symbols.
arrayPtr = array;
}
}
//FUNCTION 4:
// 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){
//EXPLANATION:
// Same as before we use the "check" function to get the index of number to be
//removed from the array.
int index = check(arrayPtr, number, size);
//if element it's not found exit from function.
if(index == -1) return;
else{
//else, create a new array smaller by one respect to the old arrayPtr.
int *array = new int[size - 1];
//To avoid appending the element to be deleted, we define a secondary index (j).
int j=0;
for(int i=0; i<size; i++){
//The "deletion" by ignoring the index that contains the element to delete.
if(i!=index){
array[j] = arrayPtr[i];
j++;
}
}
//Same as before, we override the old array with the new one.
arrayPtr = array;
}
}
#endif // VARARRAY_H