Thread: vectors

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

    vectors

    I am supposed 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)

    I am supposed to 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.

    I keep getting errors but here is my program so far. Can someone give me some advice?
    I am very new at this and I think it has something to do with how I am asking the user to input things but I am not sure.
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    
    int main()
    {
    	vector <int> theNumbers;
    	cout << "Enter a set of positive integer from least to greatest." << endl;
    	cout << "When you are done, press '-5' to evaluate the median." << endl;
    	int i;
    	while (i>0)
    	{
                    cin >> i;
                    theNumbers.push_back(i);
    		if ( i < i-1)
    		{
    			cout << "Numbers entered out of order. Exiting." << endl;
    			exit(1);
    		}
    	}
    		
    	
    	int theMedian;
            theMedian = (theNumbers.size() -1) /2 ;
    
    		
    	cout << "The median is: " << theMedian << endl;
    	return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your input should be inside the loop
    so I think somethink like this:
    Code:
    while(cin>>i)
    {
       if(i == -5)
          break; /* end input indication */
    ...
    should be suitable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that using "exit" is almost never a good idea. It will instantly terminate the program, not allowing for cleanup or recovery or the like.
    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.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    theNumbers.push_back(i);
    if ( i < i-1)
    {
        cout << "Numbers entered out of order. Exiting." << endl;
        exit(1);
    }
    That should be something more like:
    Code:
    if( theNumbers.size() && i < theNumbers[theNumbers.size()-1] )
    {
        cout << "Numbers entered out of order. Exiting." << endl;
        exit(1);
    }
    theNumbers.push_back(i);


    Code:
    int theMedian;
    theMedian = (theNumbers.size() -1) /2 ;
    That does not calculate the median value but rather calculates the number of items in the vector (minus 1) divided by two. You're looking for something closer to the value at that index, not the index itself. The statistical median value of a set with odd number of elements is the middle value. The median value of a set with an even number of
    elements is the average of the two middle-most values.

    ...and you probably should have just continued posting on the other thread instead of creating a new one.
    "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

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM