Need help with function..

This is a discussion on Need help with function.. within the C++ Programming forums, part of the General Programming Boards category; Hello,
I'm just trying to cheat here with my homework.
I need to find the lowest number of a group ...

Need help with function..

Hello,

I'm just trying to cheat here with my homework.

I need to find the lowest number of a group of 5 numbers, entered by the user.

I also need to save all 5 numbers. So, I think it would be a function that somehow loops through each number and compares it to each other to determine the lowest one. I just can't think of how to write it.

I would guess that if you wrote out your idea more carefully, you'd be a lot farther along. (For instance, "loops through each number" makes no sense; and even if it did, once we have one number, "comparing it to each other" wouldn't make a lot of sense either.)

Sit down with a, b, c, d, and e and figure out what comparisons you want to make. Then figure out how to make those comparisons.

But yes, you are probably expected to come up with an algorithm rather than use an implemented generic algorithm. Think about it: with an array or some other sequential container, to find the lowest, you do not need to compare each number to every other number. You only need to compare the next number with the currently known lowest. When there are no more numbers left to compare, the currently known lowest must be the lowest of them all.

Yep.

Originally Posted by laserlight

More like std::min_element, which works on a range.

But yes, you are probably expected to come up with an algorithm rather than use an implemented generic algorithm. Think about it: with an array or some other sequential container, to find the lowest, you do not need to compare each number to every other number. You only need to compare the next number with the currently known lowest. When there are no more numbers left to compare, the currently known lowest must be the lowest of them all.

Yep, I had one like that too.. in which you need to find the lowest number entered, but that was WITHOUT having to save the entered numbers as variables, so you could just say "if this next number is lower than the first number then save it as lowNumber..) etc etc.. and at the end you have the lowest number. The trick to that was that you can actually run a test on declared variables before they even contain anything.. (took me a while to figure that out..)

Zach, i doubt you will be able to come up with any more 'elegant' algorithm for that, for the assignment/course you are doing. so dont think of it as 'not elegant'. the algorithm is O(n) that is it runs it linear time. i imagine any faster algorithms for finding a min are quite a bit more complex, and not what your professor/teacher is looking for at this level.