C++ ONLY PLEASE DO THE ADD(AddFirst etc.) FUNCTIONS FIRST!!! EXERCISES Add the following public methods (along with a co

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

C++ ONLY PLEASE DO THE ADD(AddFirst etc.) FUNCTIONS FIRST!!! EXERCISES Add the following public methods (along with a co

Post by answerhappygod »

C++ ONLY
PLEASE DO THE ADD(AddFirst etc.) FUNCTIONS FIRST!!!
EXERCISES
Add the following public methods (along with a completedocumentation string as described) to the DynamicArray class. Testyour code in Wk7Lab.cpp.You may modify the parameters and return types of the methods butmust give reasoning for doing so.
Once complete, submit a zip file containing both DynamicArray.hand Wk7Lab.cpp
Code may not have any warnings and memory leaks.
DynamicArray.h
#include <cstdlib>#include <ctime>#include <iostream>
#ifndef DYNAMICARRAY_H#define DYNAMICARRAY_H
using namespace std;
class DynamicArray{public:int size;private:int* array;
public:// constructors// default constructorDynamicArray(){//cout << "default constructor called\n";size = 0;array = nullptr;}
// defined constructorDynamicArray(int n){//cout << "defined constructor called\n";// set n to size// allocate size integers worth of memeory for array// set each item in array to 0size = n;array = new int[size];for(int i = 0; i < size; i++){array = 0;}}
DynamicArray(int n, int min, int max){// set n to size// allocate memory for n integers// each integer is a random integer between min and max, bothinclusivesrand(time(0));size = n;array = new int[size];for(int i = 0; i < size; i++){array = GenerateRandom(min, max);}}
~DynamicArray(){cout << "destructor called for array of size " << size<< endl;if(array){delete[] array;array = nullptr;}}
void Show(){// displays contents of array on a new linefor(int i = 0; i < size; i++){cout << array << " ";}cout << endl;}
private:// private methods are to be used inside of the class only// these are like class maintenance tools, not for the outisideworld to useint GenerateRandom(int min, int max){// returns a random integer between min and max, bothinclusivereturn rand() % (max - min + 1) + min;}};
#endif
lab7.cpp
#include <iostream>
#include "DynamicArray.h"
using namespace std;
int main(){
// DynamicArray* d1 = new DynamicArray(5, 2, 13);
// d1->Show();
// if(d1){
// delete d1;
// d1 = nullptr;
// }
DynamicArray d1;
DynamicArray d2(5);
return 0;
}
PLEASE DO THE ADD FUNCTIONS FIRST!!!
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply