Question

In: Computer Science

Using only <iostream>, implement a dynamic array. You are to build a class called MyDynamicArray. Your...

Using only <iostream>, implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following:

MyDynamicArray(); Default Constructor. The array should be of size 2.

MyDynamicArray(int s); For this constructor the array should be of size s.

~MyDynamicArray();Destructor for the class.

int& operator[](int i); Traditional [] operator. Should print a message if i is out of bounds and return a reference to a zero value.

void add(int v); increases the size of the array by 1 and stores v there.

void del(); reduces the size of the array by 1.

int length(); returns the length of the array.

int clear(); Frees any space currently used and starts over with an array of size 2.

Here is a sample main.cpp file:
#include <iostream>
using namespace std;
#include "MyDynamicArray.cpp"
int main() {
MyDynamicArray x;
for (int i=0; i<100; i++){
x.add(i);
}
int sum = 0;
for (int i=0; i<x.length(); i++){
sum+=x[i];
}
cout << "The sum is : " << sum << endl;
for (int i=0; i<95; i++)
x.del();
x[60] = 27;

MyDynamicArray y(10);
for (int i=0; i<y.length(); i++) y[i] = i*i;
for (int i=0; i<200; i++){
y.add(i);
}
sum = 0;
for (int i=0; i<y.length(); i++){
sum+=y[i];
}
cout << "The sum is : " << sum << endl;
for (int i=0; i<195; i++)
y.del();
y[60] = 27;
for (int i=0; i<200; i++){
y.add(i);
}
sum = 0;
for (int i=0; i<y.length(); i++){
sum+=y[i];
}
cout << "The sum is : " << sum << endl;

}
Here is the output from the main.cpp above :
Doubling to : 4
Doubling to : 8
Doubling to : 16
Doubling to : 32
Doubling to : 64
Doubling to : 128
The sum is : 4950
Reducing to : 64
Reducing to : 32
Reducing to : 16
Out of bounds reference : 60
Doubling to : 20
Doubling to : 40
Doubling to : 80
Doubling to : 160
Doubling to : 320
The sum is : 20185
Reducing to : 160
Reducing to : 80
Reducing to : 40
Out of bounds reference : 60
Doubling to : 80
Doubling to : 160
Doubling to : 320
The sum is : 20195

Solutions

Expert Solution

#include <iostream>
using namespace std;

class MyDynamicArray {
public:
MyDynamicArray(); // Default Constructor
MyDynamicArray(int s); //parameterised Constructor
~MyDynamicArray(); //destructor
int& operator[](int i); //Traditional [] operator
void add(int v); //increases the size of the array by 1 and stores v there.
void del(); // reduces the size of the array by 1.
int length(); //returns the length of the array.
int clear(); //Frees any space currently used and starts over with an array of size 2
private:
int size;
int arr[2];
};

MyDynamicArray::MyDynamicArray() {
size = 2;
}

MyDynamicArray::MyDynamicArray(int s) {
size = s;
}

MyDynamicArray::~MyDynamicArray() {
size = 0;
}

void MyDynamicArray::add(int v) {
size++;
arr[size - 1] = v;
}

void MyDynamicArray::del() {
size--;
}

int MyDynamicArray::length() {
return size;
}

int MyDynamicArray::clear() {
size = 2;
return 1;
}

int &MyDynamicArray::operator[](int i) {
if (i > size) {
cout << "Index out of bounds" << endl;
// return first element.
return arr[0];
}
return arr[i];
}

//#include <iostream>
//using namespace std;
//#include "MyDynamicArray.cpp"

int main() {
MyDynamicArray x;
for (int i = 0; i < 100; i++) {
x.add(i);
}
int sum = 0;
for (int i = 0; i < x.length(); i++) {
sum += x[i];
}
cout << "The sum is : " << sum << endl;
for (int i = 0; i < 95; i++)
x.del();
x[60] = 27;
MyDynamicArray y(10);
for (int i = 0; i < y.length(); i++) y[i] = i * i;
for (int i = 0; i < 200; i++) {
y.add(i);
}
sum = 0;
for (int i = 0; i < y.length(); i++) {
sum += y[i];
}
cout << "The sum is : " << sum << endl;
for (int i = 0; i < 195; i++)
y.del();
y[60] = 27;
for (int i = 0; i < 200; i++) {
y.add(i);
}
sum = 0;
for (int i = 0; i < y.length(); i++) {
sum += y[i];
}
cout << "The sum is : " << sum << endl;
return 0;
}

output:

Please comment of you have any query!!


Related Solutions

For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array that can grow and shrink. The public methods of your class should already be the following: MyDynamicArray(); Default Constructor. The array should be of capacity 2. MyDynamicArray(int s); For this constructor the array should be of capacity and size s. int& operator[](int i); Traditional [] operator. Should print a message if i is out of...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
Below is a class called pointerDataClass that store data in a one-dimensional array using pointer. #include<iostream>...
Below is a class called pointerDataClass that store data in a one-dimensional array using pointer. #include<iostream> using namespace std; class pointerDataClass { int maxSize;//variable to store the maximum size of p int length;//variable to store the number of elements in p int *p;// pointer to an int array public: //Constructor to create an array of the size specified by the parameter size. pointerDataClass(int size); //Destructor to deallocate the memory space occupied by the array p ~pointerDataClass(); //the function insertAt inserts...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be...
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be used are the ones defined in the Queue class. class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) The codes to fill: """ 1. Stack2 class Implement stack data structure using queue """ class Stack2: def __init__(self): # Write your definition for __init__ here def isEmpty(self): #...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class Polynomial (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. (3) define the following methods: a. public Polynomial()    POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0)    POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p)    POSTCONDITION: Creates...
You will implement and test the sequence class using an array to store the sequence's items...
You will implement and test the sequence class using an array to store the sequence's items in C++. sequence1.h: The header file for the sequence class. Actually, you don't have to write much of this file. Start with the sequence1.h header file provided and add your name and other information at the top. Also, decide on appropriate private member variables, and declare these in the sequence class definition at the bottom of the header file. If some of your member...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT