Question: C++ Implement void insertAt(int index, int value). Insert the value at the index without overwriting anything. If there is already a something stored at index,
C++
Implement void insertAt(int index, int value). Insert the value at the index without overwriting anything. If there is already a something stored at index, it should be shifted to the right. If index is larger than the current size of the array, then it needs to be resized in order to accomodate. If there is a gap between the old size of the array, and the newly inserted value, that gap should be filled with 0s.
.........
struct ResizableArray { int* data; int size; int counter; ResizableArray(){ data = new int[1]; size = 1; counter = 0; }
........
void insertAt(int index, int value){ // Provide your code here }
............
int main(int argc, const char * argv[]) { ResizableArray array; for (int i = 0; i < 6; i++) { array.insert(i); } array.insertAt(2, 77); array.insertAt(10, 89); array.insert(101); array.print(); /* Expected output of array.print() 0 1 77 2 3 4 5 0 0 0 89 101 */ cout << "count: " << array.counter << endl; cout << "size : " << array.size << endl; /* Expected output of 2 lines above count: 12 size : 16 */
.....
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
