Answered You can hire a professional tutor to get the answer.

QUESTION

I need to find the issue with this code. I need to pass an int* array and another int to a function and double the array's size. The argument passed...

I need to find the issue with this code. I need to pass an int* array and another int to a function and double the array's size. The argument passed has to be int*, not int**. Here's my code:

#include <iostream>

using namespace std;

int* transformArray(int* oldArray, int size)

{

// declaring a temporary array

int* currentArray = oldArray;

int newSize = size * 2; // the new size is double the old one

int* newArray = new int[newSize]; // dynamically allocating array

//assigning first array indexes

for (int i = 0; i < size; i++)

{

newArray[i] = currentArray[i];

}

//assigning next array indexes

 for (int i = 0; i < size; i++)

{

newArray[size+i] = currentArray[i]+1;

}

// changing the old array that we passed to the function

oldArray = newArray;

delete [] newArray;

delete [] currentArray; //preventing memory leaks

}

int main()

{

int* oldArray = new int[3];

oldArray[0] = 4;

oldArray[1] = 2;

oldArray[2] = 5;

transformArray(oldArray, 3);

for (int i=0; i<6; i++)

cout << oldArray[i] << endl;

delete []oldArray;

return 0;

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question