Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c. Write one clear sentence tellmg reference parameter. (5pts) What happens if y

ID: 3595367 • Letter: C

Question

c. Write one clear sentence tellmg reference parameter. (5pts) What happens if you call new but the heap is out of memory? d. (Spts) 2. Consider the bag from Chapter 4 (using a dynamic array). The pointer to the dynamic array is called data, and the size of the dynamic array is stored in a private member variable called capacity. Write the following new member function: (19 pts) Postcondition: The capacity of the bag s dynamic array has beern void bagartriple capacity . tripied. The bag til1 contains the same items that it previously had Do not use the reserve function, do not use realloc, and do not cause a heap leak. Do make sure that both data and capacity have new values that correctly indicate the new larger array.

Explanation / Answer

#include<iostream>

using namespace std;

class bag{

int *data;

int capacity;

public:

bag()

{

cout<<"Enter the capacity ";

cin>>capacity;

data=new int[capacity];

}

void input()

{

int i;

cout<<"Enter the Array elements are ";

for(i=0;i<capacity;i++)

cin>>data[i];

}

void display()

{

int i;

cout<<"Array elements are ";

for(i=0;i<capacity;i++)

{

cout<<data[i]<<" ";

}

}

void triple();

};

void bag::triple()

{

int i, *data1;

data1=new int[capacity];

for(i=0;i<capacity;i++)

data1[i]=data[i];

capacity*=3;

data=new int[capacity];

for(i=0;i<capacity;i++)

data[i]=data1[i];

cout<<"Enter the"<<2*capacity/3<<" new elemens ";

for(i=capacity/3;i<capacity;i++)

cin>>data[i];

delete(data1);

}

int main()

{

bag B;

B.input();

B.display();

B.triple();

//B.input();

B.display();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote