Explore BrainMass

Using a Linear Search to Find the Smallest Element in a List

Please help with the following problem.

I am having trouble figuring out how to determine the last element of the print statement. For array A(N) and input element ARG, what kind of algorithm is needed to determine which element of A(N) is closest to but not greater than ARG? I've gotten this far but can't find anything in my course manual that describes this situation.

If A(1) < ARG > A(N) then
CODE = 2
Print ARG, CODE, A(N) that is closest to but not greater than ARG

© BrainMass Inc. brainmass.com July 23, 2018, 10:02 am ad1c9bdddf

Solution Preview

In order to find the element in the array with the closest, but not greater than, value to ARG you will have to go through the entire array. Going through an array one element at a time requires some sort of loop statement. So we will have something like this:

I = 0;
// do ...

Solution Summary

In this solution, I demonstrate how to search a list of numbers for the largest value less than a given argument. I start with a simple solution and then add the extra logic necessary for the more complex condition.