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

Using the description of the compareArrays function in its module specs (given b

ID: 3849545 • Letter: U

Question

Using the description of the compareArrays function in its module specs (given below), write the function heading

Write as much of the compareArrays function as you can, USING the description of the logic in the module specs

Submit the function in C++ ONLY (NOT the whole program).

compareArrays

Return Value: numberMatched (int)

Reference parameters: (none)

Receives: array1 (array of ints), size1 (int), array2 (array of ints), size2 (int)

Preconditions: array1 and array2 have been populated, size1 indicates how many elements in array1, and size2 indicates how many are in array2

Logic: Search for each element of array1 in array2, add to a counter if they match. Use a for loop to traverse array1, checking if array1[i] is in array2 by calling the searchList function GIVEN BELOW, and if the searchList function returns a value >= 0, add 1 to the match counter.

int searchList(int list[], int numElems, int value)

{

   int index = 0; // Used as a subscript to search array

   int position = -1; // To record position of search value

   bool found = false; // Flag to indicate if the value was found

   while (index < numElems && !found)

   {

if (list[index] == value) // If the value is found

{

   found = true; // Set the flag

   position = index; // Record the value's subscript

}

index++; // Go to the next element

   }

   return position; // Return the position, or -1

}

Explanation / Answer

int compareArrays (int list1[], int size1, int list2[], int size2){
int numberMatched = 0; // Counter to count the number of matches
// Iterate over list 1 to count the matches
for(int i=0;i<size1;i++){
// Search for item in list 2
if(searchList(list2,size2,list1[i])>=0){
numberMatched++; // Update counter if integer is found in list 2 and position >=0
}
}
return numberMatched; // Return the number of matches
}