Thread: Pointing to the last element and ending of program

  1. #1
    Registered User Daniel Primed's Avatar
    Join Date
    Jun 2005
    Posts
    13

    Pointing to the last element and ending of program

    Been working on some C++ exercises and what I’m doing is finding out the 5 number summary(max, min, q1,q2,q3) and the number of inputted ints for a group of integers that are inputted. This is an extension of an exercise for Accelerated C++. There are a few things that I’m unsure of. Firstly how do to refer to the last element if I don’t know how many elements there are in the vector. It also shows the results once you input the ints that you want to have in and then end-of-file. How do you end-of-file in a program. I’ve tried pressing the end button but it doesn’t work. Also I have used one and two as the 1st and 3rd quartile respectively. Have I done the right thing, if not, why not and how would you do it. Thanks for the help.

    Code:
    #include<algorithm>
    #include<iomanip>
    #include<ios>
    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    
    int main()
    {
        cout<<"Please input your numbers:";
        vector<int> numbers;
        int number;
        while(cin>>number)
              numbers.push_back(number);
        typedef vector<int>::size_type numbersize;
       
        
        numbersize size=numbers.size();
        if(size==0){
                    cout<<endl<<"You must enter your numbers"<<endl;
                    return 1;
        }
        //sort the grades
        sort(numbers.begin(),numbers.end());
        
        //computer the median homework grade
        numbersize mid=size/2;
        double median;
        median= size % 2 ==0 ? (numbers[mid] +numbers[mid-1])/2
                             : numbers[mid];
                             
        numbersize one= mid/2;
        numbersize two=mid + one;
        
        cout<<"The median is: "<<median;
        cout<<"The 1st quartile is: "<< one;
        cout<<"The 3rd quartile is: "<<two;
        cout<<"The min value is: "<<numbers[0];
        cout<<"There are "<<numbers.size()<<" elements that you inputted";
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Firstly how do to refer to the last element if I don’t know how many elements there are in the vector
    Your code already contains the answer - it's the .size()

    > How do you end-of-file in a program
    For DOS/Win32, press ctrl-z at the start of a line, then press enter (perhaps)
    For Linux/Unix, it's usually ctrl-d

    > Also I have used one and two as the 1st and 3rd quartile respectively. Have I done the right thing
    If you're concerned about the calculation, then lookup statistics on http://mathworld.wolfram.com/

    Your cout need endl

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Daniel Primed
    How do you end-of-file in a program.
    For the purposes of your program, stopping getting input from the user and inserting the integers into the vector, you could also just type in a non-numeric character(s) followed by <enter>. That would cause the while(cin>>number) part of the code to evaluate to false and break out of that loop.
    "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

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    15
    Quote Originally Posted by Salem
    > Firstly how do to refer to the last element if I don’t know how many elements there are in the vector
    Your code already contains the answer - it's the .size()
    size() would tell you how many elements you have. To refer to the last element of a vector use back().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM