Question: Can someone help me fix my c++ code? The program is meant to take in an array of integers up to a certain size (ie

Can someone help me fix my c++ code? The program is meant to take in an array of integers up to a certain size (ie if arraysize is 5, possible array numbers are 1,2,3,4,5) and return an array with all the numbers up to the Arraysize that weren't included in the original array (if original array was 2,3,4,4,4, returned array should be 1,5). My program is working, but is not including the largest possible number in the returned array. For example, when I enter the array 2,4,4,3,2, the array returned only includes 1 when it should include both 1 and 5. Anyway, help me fix my code, thanks!

#include

using namespace std;

int* findMissing(int arr[], int n, int& resArrSize);

int main() {

int n,resArrSize;

cout<<"Enter the size of your array: ";

cin>>n;

int numbers[n];

cout<<"Enter the numbers that belong in your array: ";

for(int i=0;i

cin>>numbers[i];

}

int *newArray = findMissing(numbers,n,resArrSize);

for(int i=0;i

cout<

}

return 0;

}

int* findMissing(int arr[], int n, int& resArrSize) {

resArrSize = n;

int* resArray = new int[resArrSize];

int *tempArr = new int[n];

int place = 0;

for(int i=0;i

tempArr[i] = i;

}

for(int i=0;i

if(tempArr[arr[i]] != -1) {

tempArr[arr[i]] = 0;

resArrSize--;

}

}

int count=0;

for(int i=0;i

if(tempArr[i] > 0) {

resArray[count] = tempArr[i];

count++;

}

}

resArrSize = count;

return resArray;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!