medians and vectors

This is a discussion on medians and vectors within the C++ Programming forums, part of the General Programming Boards category; Any ideas on how to do this? Write a program that allows the user to enter a set of positive ...

  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-24-2008 at 12:34 AM. Reason: mistyped something

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,281
    Hint: use the push_back() member function of vector.
    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

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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

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