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

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

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

Post 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;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply