Question
Can you help trouble shoot this function in C++ requiring an array where the user determines its size, thus has to be dynamic. Question 4:
Can you help trouble shoot this function in C++ requiring an array where the user determines its size, thus has to be dynamic.
Question 4: Implement the function: void oddsKeepEvensFlip(int arr[], int arrSize) This function gets an array of integers arr and its logical size arrSize. When called, it should reorder the elements of arr so that: 1. All the odd numbers come before all the even numbers 2. The odd numbers will keep their original relative order 3. The even numbers will be placed in a reversed order (relative to their original order). For example, if arr = [5, 2, 11, 7, 6, 4], after calling oddsKeepEvensFlip(arr, 6), arr will be: [5, 11, 7, 4, 6, 2] Implementation requirements: 1. Your function should run in linear time. That is, if there are n items in arr, calling oddsKeepEvensFlip(arr, n) will run in theta (n) time 2. Write a main() program that tests this function. */
#include
void oddsKeepEvensFlip(int array[], int arraySize);
int main() { int array[] = { 5, 2, 11, 7, 6, 4 };
oddsKeepEvensFlip(array, 6);
cout << "Odds Keep & Evens Flip:" <
cout << endl;
return 0; }
void oddsKeepEvensFlip(int array[], int arraySize) { int* tempArray; // temporary array tempArray = new int[arraySize]; int i, index = 0;
for (i = 0; i < arraySize; i++) if ((array[i] % 2 == 1)) tempArray[index++] = array[i];
for (i = arraySize; i >= 0; i--) { if (array[i] % 2 == 0) tempArray[index++] = array[i]; }
for (i = 0; i < arraySize; i++) { array[i] = tempArray[i]; }
delete[] tempArray; tempArray = array; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started