Question: The function contains() scans an array to determine if an item is in the array. The function returns true if the item found and false

The function contains() scans an array to determine if an item is in the array. The function returns true if the item found and false otherwise.

The following implementation works but is not a good solution.

bool contains(int arr[], int n, int item)

{

int i;

bool itemFound = false;

for (i = 0; i < n; i++)

if (arr[i] == item)

itemFound = true;

return itemFound;

}

What correction would make the code more efficient?

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!