# Need help with function..

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 09-01-2008
Zzaacchh
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.

Thanks in advance.

-Zach
• 09-01-2008
nadroj
well your being honest.. however this board isnt for 'cheating.' i will give a hint though:

lets say i have a list (an array if you will) of 5 'random' numbers: 5, 3, 7, 2, 9. i will try to find the smallest one:

1) what is the smallest of 5 and 3? 3.
2) what is the smallest of 3 and 7? 3.
3) what is the smallest of 3 and 2? 2.
4) what is the smallest of 2 and 9? 2.

since ive gone through the list, the smallest of all of them is 2.

hope it helps
• 09-01-2008
tabstop
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.
• 09-01-2008
cpjust
You could use the std::min() function, but I doubt that's what your teacher is looking for. :p
• 09-01-2008
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.
• 09-02-2008
cpjust
Quote:

Originally Posted by laserlight
More like std::min_element, which works on a range.

Oops.
Yeah, what she said. ;)
• 09-02-2008
master5001
Can I cheat on my homework. I have a cabinet due in woodshop tomorrow... Just kidding. I do applaud honesty.
• 09-02-2008
Zzaacchh
Thanks!
Quote:

Originally Posted by nadroj
well your being honest.. however this board isnt for 'cheating.' i will give a hint though:

lets say i have a list (an array if you will) of 5 'random' numbers: 5, 3, 7, 2, 9. i will try to find the smallest one:

1) what is the smallest of 5 and 3? 3.
2) what is the smallest of 3 and 7? 3.
3) what is the smallest of 3 and 2? 2.
4) what is the smallest of 2 and 9? 2.

since ive gone through the list, the smallest of all of them is 2.

hope it helps

Yeah, I figured that one out about 5 minutes after writing to here for some help.. (usually happens that way)

I *was* looking for a more 'elegant' way of doing it, lest I had 300 numbers instead of just 5.. but we haven't gotten into arrays yet, so maybe thats when I'll find the real stuff.. ;)

thanks to everyone who contributed.

It's not cheating if I don't get caught. ;)

-Zach
• 09-02-2008
Zzaacchh
Yep.
Quote:

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..)

We haven't started into arrays yet, thats next chapter. Whoo hoo..

-Zach
• 09-03-2008
CornedBee
Quote:

The trick to that was that you can actually run a test on declared variables before they even contain anything..
Uh, no you can't. A good compiler should warn you about it, and you produce undefined behaviour.
• 09-03-2008
Elysia
What you need to learn is pseudo code and flow charts. It's amazing how many people don't learn them these days and get stuck all the time because they don't.
• 09-03-2008
nadroj
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.
• 09-03-2008
CornedBee
There is no better complexity than O(n) for finding the minimum of an unsorted range. That would be a logical impossibility.
• 09-03-2008
nadroj
you mean there isnt one yet!
• 09-03-2008
master5001
Quote:

Originally Posted by nadroj
you mean there isnt one yet!

...Because you aim to disprove a mathematical proof or because you refuse to accept them?
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last