Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Pointers, Dynamic Memory Allocation Test Array. Create a project titled Lab1

ID: 3693092 • Letter: C

Question

C++

Pointers, Dynamic Memory Allocation

Test Array.  Create a project titled Lab10_TestArray. In the project, you should use the functions prototyped in REF _Ref448311475 h * MERGEFORMAT //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:

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

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)

Your code should work with REF _Ref448311488 h * MERGEFORMAT // Figure 2 test file

Hint: Study example code REF _Ref448311502 h * MERGEFORMAT ////Figure 3 destructor, REF _Ref448311515 h* MERGEFORMAT ///Figure 4 copy constructor, and REF _Ref448311536 h * MERGEFORMAT //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:

enter operation [a/r/q] and number: a 5

your numbers: 5

enter operation [a/r/q] and number: a 8

your numbers: 5 8

enter operation [a/r/q] and number: a 8

your numbers: 5 8

enter operation [a/r/q] and number: r 8

your numbers: 5

enter operation [a/r/q] and number: a 3

your numbers: 5 3

enter operation [a/r/q] and number: a 12

your numbers: 5 3 12

enter operation [a/r/q] and number: q

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.

Make sure your programs adhere to proper programming style. Submit your projects to the designated drop box on D2L. Due date is April 25 11 PM.

//Figure SEQ Figure * ARABIC 1: header file

//varArray.h

// functions to manipulate variable size array

// Saleh Alnaeli &Mikhail Nesterenko

// KSU

#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

// tests variable size array implementation

// Saleh Alnaeli & Mikhail Nesterenko

// KSU

#include "varArray.h"

#include

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 cotnaining 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 SEQ Figure * ARABIC 4 copy constructor

// demonstrates copy constructor with an object containing array

// Mikhail Nesterenko & Saleh Alnaeli

//KSU

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 SEQ Figure * ARABIC 5 assignment overloading

// demonstrates overloaded assingment with an object containing array

// Mikhail Nesterenko & Saleh Alnaeli

// KSU

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

#include <iostream>

#include "varArray.h"

using namespace std;

int main(void) {

char operation;

          int newNum;

int *arrayPtr = new int[0];

          int size = 0;

do

{

cout << "Enter operation [a/r/q] and number: ";

cin >> operation;

if (operation != 'q')

{

cin >> newNum;

if (operation == 'a')

addNumber(arrayPtr, newNum, size);

else if (operation == 'r')

removeNumber(arrayPtr, newNum, size);

output(arrayPtr, size);

cout << endl;

}

} while (operation != 'q');

}

void addNumber(int *& arrayPtr, int number, int &size)

{

   ++size;

int *newArr = new int[size];

for (int i = 0; i < size-1; ++i)

newArr[i] = arrayPtr[i];

newArr[size-1] = number;

delete [] arrayPtr;

arrayPtr = newArr;

}

void removeNumber(int *& arrayPtr, int number, int &size) {

bool found = false;

for (int i = 0; i < size; ++i)

{

if (arrayPtr[i] == number)

found = true;

}

if (found)

{

int *newArr = new int[size-1];

copyArray(arrayPtr, newArr, size);

for (int i = 0; i < size; ++i)

if (arrayPtr[i] == number) continue;

delete [] arrayPtr;

arrayPtr = newArr;

--size;

}

}

void copyArray(int *original, int *& copy, int size)

{

for (int i = 0; i < size; ++i)

copy[i] = original[i];

}

void output(int *arrayPtr, int size)

{

int *copy = new int[size];

copyArray(arrayPtr, copy, size);

cout << "Your numbers: ";

while (size != 0)

{           

int smallest = copy[0];

for (int i = 1; i < size; ++i)

{

if (copy[i] < smallest)

smallest = copy[i];

}

cout << smallest << ' ';

removeNumber(copy, smallest, size);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote