Module: Linear and binary search for elements in an array


Problem

1/7

Linear Search - Looking for the maximum

Theory Click to read/hide

Linear array search
Very often you need to find a given value in an array or report that it is not there. To do this, you need to look through all the elements of the array from the first to the last. As soon as an element equal to the given value X is found, the search should end and the result should be displayed. Such an algorithm is called linear.

A linear algorithm is used to find the maximum (minimum) element of an array. This is also a search algorithm. But here we are forced to go to the end of the array, because it is necessary to compare all elements with the current maximum (minimum) value and if the current element is greater (less) than the maximum (minimum) value, replace the maximum (minimum) value. 
 

Problem

Find the maximum of the negative elements in the array. Add a condition inside the loop (after the word if) to have the program find the maximum element among the negative elements.