Thread: medians and vectors

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Question medians and vectors

    Any ideas on how to do this?

    Write a program that allows the user to enter
    a set of positive integers in order from smallest to largest and then
    the program will output the median of the set of numbers. The user
    will type a -5 to indicate that the list of integers is complete. (The -5
    is not part of the list).

    If the user doesn't enter the numbers in order, the program should
    display "Numbers entered out of order. Exiting." and exit. You can exit the
    program using the command: exit(1)

    Write this program using vectors.


    My problem is that I can't seem to figure out in my head how to run a loop for this.
    Last edited by JoeJoe; 03-23-2008 at 11:34 PM. Reason: mistyped something

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: use the push_back() member function of vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    And for the order part (numbers are to be entered in order from smallest to largest) when doing the push_back just check the last valid element in the vector, if the last element is larger than the current number you are about to "push" then the user is entering the value out of order.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed