150k views
0 votes
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: (C++ LANGUAGE)

void bag::triple_capacity( )
// Postcondition: The capacity of the bag's dynamic array has been
// tripled. The bag still 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.

User Luella
by
5.5k points

1 Answer

7 votes

Answer:

Following are code to this question:

void bag::triple_capacity()//defining a method bag that uses the scope resolution operator to hold triple_capacity

{

int* newArray = new int[3*capacity];//defining an array that holds capacity value

for(int x=0;x<capacity;x++)//defining for loop to hold array value

{

newArray[x] = data[x];//holding array value in newArray

}

delete[] data;//using the delete keyword to delete value in array

data = newArray;//holding array value in data

capacity = capacity*3;//increaing the array size value

}

Step-by-step explanation:

In this code, a method "bag" is declared that uses the scope resolution operator that holds the triple_capacity method, inside the method an array "newArray" is declared that uses the for loop to hold array value.

In the next step, the delete keyword is declared that delete data value and hold newArray value within it and at the last, it increases the capacity of the array.

User Erjot
by
5.7k points