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

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    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

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could use the std::min() function, but I doubt that's what your teacher is looking for.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    More like std::min_element, which works on a range.
    Oops.
    Yeah, what she said.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Can I cheat on my homework. I have a cabinet due in woodshop tomorrow... Just kidding. I do applaud honesty.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Thanks!

    Quote Originally Posted by nadroj View Post
    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

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Yep.

    Quote Originally Posted by laserlight View Post
    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

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There is no better complexity than O(n) for finding the minimum of an unsorted range. That would be a logical impossibility.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you mean there isnt one yet!

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by nadroj View Post
    you mean there isnt one yet!
    ...Because you aim to disprove a mathematical proof or because you refuse to accept them?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21