Question

Lab Tasks: 1. Define a dynamic array class in DynamicArray .h and DynamicArray.cpp files, according to the following UML clas

+ getCurrentSize(): int // Return the number of values currently stored in the array. 2. Write a testing program in main.cpp

Following the instruction

This is c++ programming

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Answer

DynamicArray.h

#ifndef DYNAMICARRAY_H_INCLUDED
#define DYNAMICARRAY_H_INCLUDED

#endif // DYNAMICARRAY_H_INCLUDED

class DynamicArray {
private:

int arraysize;
int currentsize;
int* arrayPtr, *iterator;

public:
DynamicArray(int isize);

DynamicArray();

~DynamicArray();

bool additem(int item);

int getitem(int index);

int getcurrentsize();

void resize();
};

DynamicArray.cpp

#include <iostream>
#include <algorithm>
#include <cstddef>

using namespace std;

class DynamicArray {
private:

int arraysize;
int currentsize;
int* arrayPtr, *iterator;

public:
DynamicArray(int isize) {
arrayPtr = new int[isize];
currentsize=isize;
arraysize=isize;
iterator = arrayPtr;
}
DynamicArray() {
arraysize = 0;
currentsize = 0;
arrayPtr = nullptr;
iterator = arrayPtr;
}

~DynamicArray() {
delete[] arrayPtr;
arrayPtr = nullptr;
}

bool additem(int item) {
if(*iterator) {
*iterator = item;
++iterator;
}
else {
resize();
*iterator = item;
}
return true;
}

int getitem(int index) {
return (*arrayPtr + index);
}

int getcurrentsize() {
return currentsize;
}

void resize() {
int* temp = new int[arraysize];
currentsize+=arraysize;
std::copy(arrayPtr, arrayPtr + arraysize, temp);
delete [] arrayPtr;
arrayPtr = temp;
iterator = arrayPtr + arraysize;
}
};

int main() {
int isize,temp,i=0,j=0,avg=0,sum=0;
cout<<"Enter size of array: ";
cin>>isize;
DynamicArray d = DynamicArray(isize);
bool b = true;
while(j<2) {
i++;
cout<<"Enter value for array: ";
cin>>temp;
sum+=temp;
d.additem(temp);
if(i == isize) {
avg=sum/isize;
cout<<"Average: "<<avg<<endl;
i=0;
j++;
}
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft ...

    Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft Visual Studio described here. As you work on the below project, demonstrate to the instructor the usage of this feature. Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use this class definition. The class attributes are a pointer to the dynamically allocated array dAarray and the array size size This...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following...

    In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep copy and supports...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT