Question

In: Computer Science

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 bounds and return a reference to a zero value (the error private member).
  • void add(int v); increases the size of the array by 1 and stores v there. Should double the capacity when the new element doesn't fit (size == capacity).
  • void del(); reduces the size of the array by 1. Should shrink the capacity when only 25% or less of the array is in use after the delete (size <= capacity/4).
  • int length(); returns the length of the array.
  • int clear(); Frees any space currently used and starts over with an array of size 2.

For Part 2, you will implement the following new functions:

  • ~MyDynamicArray(); Destructor for the class. This should print the line "In the destructor"
  • you should also add a copy constructor and a copy assignment operator for the class, as below.
  • MyDynamicArray& operator= (const MyDynamicArray& src) This should print the line "In the copy assignment operator"
  • MyDynamicArray(const MyDynamicArray& src) This should print the line "In the copy constructor"

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) {
}
};

Solutions

Expert Solution

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.



Related Solutions

In the assignment you will use the vector class tomanage a dynamic array.After completing this assignmentyou...
In the assignment you will use the vector class tomanage a dynamic array.After completing this assignmentyou will be able to • use thevector classto implement programs; • using iteratorsto morethrough a vector; • use pointers to a vector; • use member functions of thevector class. Assignment Description: Call your driver "vector_test_driver.cpp", your classdeclaration file “vlist.h” and your class implementation file "vlist.cpp". Define the following behavior for vlist 1. Implement a default constructor. Include the followingmessage, "Default Constructor Invoked" every time...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
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...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public class DArray { private int array[]; public DArray() { } private void expandArray() { } private void shrinkArray() { } } --------------------------------------------------------------- public abstract class ArrayBP { protected int numElements; protected int numAllocations; public abstract void storeAt(int item, int index) { } public abstract getFrom(int index) { } public abstract int len() { } public abstract void remove();{ } public abstract void removeAt(int index)...
Suppose you have a class with 9 students. Create an Array called StudentName and StudentGrade which...
Suppose you have a class with 9 students. Create an Array called StudentName and StudentGrade which contains the student's name and the student's final grade. Create a function called "GetStudentInfo" for who you will pass the arrays and the user will enter the data for those 9 students. Back in main, loop through both arrays and display the information for each student after its entered in GetStudentInfo I need a C++ code for visual studio. Can anyone help me? I'll...
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...
Act 1: Class BooleanFunc Overview BooleanFunc will contain a truthTable (a dynamic array) which defines the...
Act 1: Class BooleanFunc Overview BooleanFunc will contain a truthTable (a dynamic array) which defines the boolean function being represented. You can load this truthTable using a mutator, setTruthTable() , and change the function it represents by calling setTruthTable() to give the table new values. You will not be able to change the size of the table (i..e. #inputs to the function) after an object is instantiated using a setTableSize-style method, but you can totally redefine the object using 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...
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT