Page 1 of 1

EXERCISES Add the following public methods (along with a complete documentation string as described) to the DynamicArray

Posted: Sun Jul 03, 2022 9:58 am
by answerhappygod
EXERCISES
Add the following public methods(along with a complete documentation string as described) to theDynamicArray class. Test your 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 filecontaining both DynamicArray.h and Wk7Lab.cpp
Code may not have any warnings andmemory 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 constructor DynamicArray(){ //cout << "default constructorcalled\n"; size = 0; array = nullptr; }
// defined constructor DynamicArray(int n){ //cout << "defined constructorcalled\n"; // set n to size // allocate size integers worth ofmemeory for array // set each item in array to 0 size = 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 integerbetween min and max, both inclusive srand(time(0)); size = n; array = new int[size]; for(int i = 0; i < size; i++){ array =GenerateRandom(min, max); } }
~DynamicArray(){ cout << "destructor called forarray of size " << size << endl; if(array){ delete[] array; array = nullptr; } }
void Show(){ // displays contents of array on a newline for(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 use int GenerateRandom(int min, int max){ // returns a random integer between minand max, both inclusive return 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;
}