Vector Sum and Median Trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RSalzman94
    New Member
    • Oct 2014
    • 6

    Vector Sum and Median Trouble

    I'm supposed to write a program in which a user will input a list of numbers into a vector. the program will run and...
    1. output the list of numbers put into the vector
    2. identify the highest and lowest number in the list
    3. find the sum of the numbers
    4. and find the median of the list


    i was able to get it to this but i still dont know how to add the average formula and the median formula to the code

    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::vector <double> values;
        std::cout<<"Enter Values, 0 To Quit:\n";
        bool more= true;
        while(more){
            double s;
            std::cin>>s;
            if(s==0)
                more=false;
            else
                values.push_back(s);
        }
        double highest= values[0];
        double lowest= values[0];
        int i;
        for(i=1;i<values.size(); i++)
            if(values[i]>highest)
                highest=values[i];
            else if(values[i]<lowest)
                lowest=values[i];
        
        for(i=0; i<values.size(); i++)
        {
            std::cout<<values[i];
            if(values[i]==highest)
                std::cout<<" <== Highest Value";
            if(values[i]==lowest)
                std::cout<<" <== Lowest Value";
            std::cout<<"\n";
        }
    
    { if (values.size() == 0) return 0;
        double sum = 0;
        for (int i = 0; i < values.size(); i++)
            sum = sum + values[i];
        return sum / values.size();}
        return 0;
        
        
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Think in terms of functionality instead of linear "stream of consciousness" code.

    If you need to:

    1.output the list of numbers put into the vector

    2.identify the highest and lowest number in the list

    3.find the sum of the numbers
    4.and find the median of the list

    then your program can look like:

    Code:
    int main()
    {
        std::vector <double> values;
        GetVaues(values);
        ShowVector(values);
        ShowHighValue(values);
        ShowLowValue(values)
        ShowSumOfValues(values);
        ShowMedianValue(values);
    }
    This is much cleaner and easier to read. Plus you can add features just by writing a new function without disturbing what is already working.

    Comment

    • RSalzman94
      New Member
      • Oct 2014
      • 6

      #3
      could you possibly show me how you would write it for the GetValues(value s); part. I'm little confused on how to do it that way

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You have already written the code in your Post #1. Just package the code in a function format:

        Code:
        void GetValues(vector<double>& values)
        {
             //Just put your get values code here
             //Since the function argument is a reference, the vector
             //values inside the function is the same vector values
             //that is outside the function.
        }
        Rules prevent me from writing all the code for you.

        Comment

        Working...