In: Computer Science
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:
For Part 2, you will implement the following new functions:
The output below should result from running the main.cpp file with your MyDynamicArray class:
Identify and understand where and why the destructor, assignment operator, and copy constructor calls take place.
Doubling to : 4
Doubling to : 8
Doubling to : 16
Doubling to : 32
Doubling to : 64
Doubling to : 128
Doubling to : 256
The sum is : 8385
Reducing to : 128
Reducing to : 64
Reducing to : 32
Reducing to : 16
Out of bounds reference : 10
Doubling to : 200
Doubling to : 400
The sum is : 171600
The sum is : 168133
10
Out of bounds reference : 2
2
In the copy assignment operator
The x sum is : 110
The b sum is : 10
In the copy constructor
In the destructor
The b sum is : 10
In the destructor
In the destructor
In the destructor
In the destructor
****main file cannot change or edit******
#include <iostream>
using namespace std;
#include "MyDynamicArray.cpp"
void foo(MyDynamicArray param) {
for(int i=0; i<param.length(); i++)
param[i]=0;
}
int main() {
MyDynamicArray x;
for (int i=0; i<130; 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<125; i++)
x.del();
x[10] = 27;
MyDynamicArray y(100);
for (int i=0; i<y.length(); i+=2) y[i] = i*i;
for (int i=0; i<200; i++) {
y.add(i);
}
sum = 0;
for (int i=0; i<y.length(); i+=2) {
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+=2) {
sum+=y[i];
}
cout << "The sum is : " << sum << endl;
MyDynamicArray z(10);
z[9] = 1000;
cout << z.length() << endl;
z.clear();
z[2] = 5;
z.add(9);
z.add(5);
cout << z.length() << endl;
MyDynamicArray b;
b = x;
x[1] += 100;
sum = 0;
for (int i=0; i<x.length(); i++) {
sum+=x[i];
}
cout << "The x sum is : " << sum << endl;
sum = 0;
for (int i=0; i<b.length(); i++) {
sum+=b[i];
}
cout << "The b sum is : " << sum << endl;
foo(b);
sum = 0;
for (int i=0; i<b.length(); i++) {
sum+=b[i];
}
cout << "The b sum is : " << sum << endl;
}
***can only change this file the MyDynamicArray file*****
#include <iostream>
using namespace std;
class MyDynamicArray {
private:
int size, capacity, error, *a;
public:
MyDynamicArray() {
/* Your code goes here */
}
MyDynamicArray(int s) {
/* Your code goes here */
}
~MyDynamicArray() {
/* Your code goes here */
}
int& operator[](int i){
/* Your code goes here */
}
void add(int v) {
/* Your code goes here */
}
void del() {
/* Your code goes here */
}
int length() { return size;}
void clear() {
delete a;
a = new int[capacity=2];
size = 0;
}
MyDynamicArray& operator=(const MyDynamicArray& src)
{
}
MyDynamicArray(const MyDynamicArray & src) {
}
};
plz check the below code.
plz maintain some fixed space while typing code on you window.
save the below file as main.cpp
#include <iostream>
#include "MyDynamicArray.cpp"
void foo(MyDynamicArray param)
{
for(int i=0; i<param.length(); i++) // parameter length to be added.
param[i]=0;
}
int main()
{
MyDynamicArray x;
for (int i=0; i<130; i++)
{
x.add(i);
}
int sum = 0;
for (int i=0; i<x.length(); i++)
{
sum+=x[i];
}
cout << "The sum is : " << sum << endl; // the sum value to be printed.
for (int i=0; i<125; i++)
x.del();
x[10] = 27
MyDynamicArray y(100); // array we have taken as 100
for (int i=0; i<y.length(); i+=2) y[i] = i*i;
for (int i=0; i<200; i++)
{
y.add(i);
}
sum = 0; // here sum value to be taken as the 0 .
for (int i=0; i<y.length(); i+=2)
{
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+=2)
{
sum+=y[i];
}
cout << "The sum is : " << sum << endl;
MyDynamicArray z(10);
z[9] = 1000;
cout << z.length() << endl;
z.clear();
z[2] = 5;
z.add(9);
z.add(5);
cout << z.length() << endl;
MyDynamicArray b;
b = x;
x[1] += 100;
sum = 0;
for (int i=0; i<x.length(); i++)
{
sum+=x[i];
}
cout << "The x sum is : " << sum << endl;
sum = 0;
for (int i=0; i<b.length(); i++){
sum+=b[i];
}
cout << "The b sum is : " << sum << endl;
foo(b);
sum = 0;
for (int i=0; i<b.length(); i++){
sum+=b[i];
}
cout << "The b sum is : " << sum << endl;
}
Plz save the below file as the MyDynamicArray.cpp
Here we have used private and public modifiers,plz maintain some fixed space between lines whie coding.
#include <iostream>
class MyDynamicArray
{
private:
int size, capacity, error, *a;
public:
MyDynamicArray()
{
a = new int[capacity = 2];
size = error = 0;
}
MyDynamicArray(int s)
{
a = new int[capacity = size = s];
error = 0;
}
MyDynamicArray& operator= (const MyDynamicArray& src)
{
size = src.size;
capacity= src.capacity;
error = src.error;
free(a);
a = new int[capacity]; // integer capacity to be added .
for ( int i = 0 ; i < size ; i++ )
*(a+i) = (src.a[i]);
}
MyDynamicArray(const MyDynamicArray& src)
{
size = src.size;
capacity= src.capacity;
error = src.error;
int *temp = new int[capacity];
for ( int i = 0 ; i < size ; i++ )
*(temp+i) = (src.a[i]);
a = temp;
}
~MyDynamicArray()
{
delete[] a;
}
int& operator[](int i)
{
if (i>=size || i < 0)
{
cout << "Out of bounds reference : " << i <<
endl; // for the reference the out bonds we are using
return error;
}
return *(a+i);
}
void add(int v)
{
// Add element to array
if ( size < capacity )
{
*(a + size ) = v ;
size++;
return;
}
// new element doesn't fit double the size of array
// new array with double capacity
cout << "Doubling to : " << capacity * 2 <<
endl;
int *temp = new int[capacity*2];
// copy all elements to new array
for ( int i = 0 ; i < capacity ; i++ )
*(temp + i) = *(a + i);
// capacity is double
capacity *= 2;
*(temp + size ) = v ;
size++;
// free old array
free(a);
// point to new array
a = temp;
}
void del()
{
size--;
if ( size <= (.25) * capacity )
{
int new_capacity = (.5) * capacity;
cout << "Reducing to :" << new_capacity << endl;
// here reducing the capacity of the given array.
int *temp = new int[ new_capacity ];
for ( int i = 0 ; i < size ; i++ )
*(temp + i) = *(a + i);
capacity = new_capacity;
free(a);
a = temp;
}
}
int length() { return size;
}
void clear()
{
delete a;
a = new int[capacity=2];
size = 0;
}
};
plz understand the above code.
Output:
Doubling to : 4
Doubling to : 8
Doubling to : 16
Doubling to : 32
Doubling to : 64
Doubling to : 128
Doubling to : 256
The Sum is : 8385
Reducing to : 128
Reducing to : 64
Reducing to : 32
Reducing to : 16
Out of bounds reference : 10
Doubling to : 200
Doubling to : 400
The Sum is : 171600
The Sum is : 168133
10
Out of bounds reference : 2
2
The x sum is : 110
The b sum is : 10
The b sum is : 10
plz like us ...It improves our community..Thank you.