need help writing function

This is a discussion on need help writing function within the C++ Programming forums, part of the General Programming Boards category; i need write a reverseInput function that will take as many numbers as user inputs and display them back in ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    need help writing function

    i need write a reverseInput function that will take as many numbers as
    user inputs and display them back in a descending order.

    for example

    enter a number 6,
    enter a number 12,
    enter a number 4,
    ,,.....

    out put the number in descending order is 12, 6, 4...

    i can't used array.

    if i can't used array how am i suppose to store the number??

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    What about using a linked list?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you're allowed to use it, use something like a std::vector which will hide the implementation details.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    i don't think i can use a vector either. any more tips? thanks

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    i don't think i can use a vector either, any other method of doing so. i think i am suppose to create a recursive function.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well that's fine. You can do this recursively. Go ahead and post what you have so far.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    that is what i am stock on. can you help me start it off. thanks.
    Code:
    int main() /// main function
    {
    numOrder();
    }
    
    int numOrder()
    {
    int inputNum;
    cout<<"Enter a Number: ";
    cin>>inputNum;
    numOrder();
    }

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Few points:

    1. Your indentation sucks. Fixing it will improve a lot of things.
    2. You're forgetting to actually print the number.
    3. Very important part of recursion is to have a base case. How do you know when to quit? I would assume this is based upon the number given by the user. For example, you could return when the user enters -1.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    1) i try to used tab but it just doesn't work. sorry.
    2) i will print the number at the end.
    3) i am very confused on the recursive function, can you provide me with a example or help me started it off?

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I spent some time talking about recursion in this topic:

    Recursion UGH!

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    First thing you have to decide is whether you want to output the numbers in the reverse order they were entered (easy with recursion), or in a descending sorted order.

    a) Reversed
    Input 4 12 6
    Output 6 12 4

    or

    b) Descending sorted
    Input 4 12 6
    Output 12 6 4

    The name of the function - reverseInput - suggests a), your description of the requirements suggests b).
    Last edited by anon; 11-25-2007 at 08:30 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    i need it to be in Descending order.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well then you have a number of options:

    1) Read input into an array.
    2) Sort array (in descending order).
    3) Print array.

    or

    1) Read input into an array.
    2) Sort array in ascending order.
    3) Print array in reverse.

    or

    1) Read input and put it into the right position in a sorted array (insertion sort helps)
    2) Print array

    (I don't see where recursion would be useful, unless you plan to implement a recursive sort algorithm.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. 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
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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