Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. the function \"oddSwap\" uses reference parameters. Rewrite the function so i

ID: 3528637 • Letter: 1

Question

1. the function "oddSwap" uses reference parameters. Rewrite the function so it uses pointers instead of the reference variables. test this function from the main program, demonstrate that it changes the values of the variables passed into it. int oddSwap (int &x, int &y) { int temp = x; x = y * 5; y = temp * 5; return x + y; } 2. The function "expand" takes an int array and the array's size as arguments. It should create a new array that is twice the size of the argument array. Then the function should copy the contents of the argument array to the new array, and initialize the unused elements of the new array with -1. The function should return a pointer to the new array. 3. takes 2 int arrays and the arrays' sizes as arguments (that's four arguments). It should create a new array that is big enough to store both of the arrays. Then it should copy the contents of the first array to the new array, then it should copy the contents of the second array into the new array in the remaining elements, and return a pointer to the new array. 4. Define subArray as follows: int *subArray (int *array, int start, int length) { return duplicateArray(__________, ___________); } Add the code for duplicateArray int *duplicateArray (int *arr, int size) { int *newArray; if (size <= 0) //size must be positive return NULL; newArray = new int [size]; //allocate new array for (int index = 0; index < size; index++) newArray[index] = arr[index]; //copy to new array return newArray; } Fill in the blanks with the expressions so that the function subArray behaves as follows: It takes an int array, the start index and a length as arguments. It creates a new array that is a copy of the elements from the original array starting at the start index, and has length equal to the length argument. For example, subArray(aa,5,4) would return a new array containing only the elements aa[5], aa[6], aa[7], and aa[8] Thank you! Will rank!

Explanation / Answer

1.

Swap (int *x, int *y) {

int *temp = *x;

*x = (*y) * 5;

*y = (*temp) * 5;

return (*x) + (*y);

}



2.

int* expand(int arr[],int size){

int *newArr=new int[size*2];

for(int i=0;i<size*2;i++){

if(i<size)

newArr[i]=arr[i];

else

newArr[i]=-1;

}

return newArr;

}

3.

int* (int arrA[],int arrB[], int sizeA,int sizeB){

int size =sizeA+sizeB;//new size

int *newArr=new int[size];

int i;

for( i=0;i<sizeA;i++){ //first array

newArr[i]=arrA[i];

}

for(i=sizeA;i<size;i++){ //second array

newArr[i]=arrB[i];

}

return newArr;


}

4.

int *subArray (int *array, int start, int length) {

return duplicateArray(array+start,length);

//note array+ start = array[start] in most cases

}

//Add the code for duplicateArray

int *duplicateArray (int *arr, int size) {

int *newArray;

if (size <= 0) //size must be positive

return NULL;

newArray = new int [size]; //allocate new array

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

newArray[index] = arr[index]; //copy to new array

return newArray;

}