Vector Output Help

I am to write a program where the user is to enter a 5 or more values into a vector and it will output:
*the list of values in the same order as the user entered them
*find the min and max value of the list
*find the average value of the list
*and find the median of the list

i was able to write the code for lowest and highest value but it returns the list of values twice and each list it identifies the highest and lowest value in each list.

list is my code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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];
    
    for(i=1; i<values.size();i++)
        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";
        std::cout<<"\n";
    }
    
    for(i=0; i<values.size();i++)
    {
        std::cout<<values[i];
        if(values[i]!=highest)
            std::cout<<" <== Lowest Value";
        std::cout<<"\n";
    }
    return 0;
    
    
}
Somethings wrongg here, at the if statement
1
2
3
4
5
6
7
for(i=1;i<values.size(); i++)
        if(values[i]>highest)
            highest=values[i];
    
    for(i=1; i<values.size();i++)
        if(values[i]>lowest)
            lowest=values[i];

To find Average sum up every element in vector using loop, then devide it with its size
And for median, ts rather hard if its unsorted vector
i was able to get it to this. not sure what loop to use to find the sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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";
    }

return 0;
}
    
}
Last edited on
That's fine but it doest need to be a new function, for median find the closest number to (min+max)/2
what would you recommend using for the sum formula?
Usng for loop ._.)a same as yours but inside main
would it be possible to add this code to my existing code and use it for the sum formula?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double num[5]; //5 is the number of spaces available in the variable
    for (int i = 0; i < 5; i++)
    {
        cout << "Enter a number for space " << i + 1 << ": ";
        cin >> num[i];
    }
    double math = 0;
    for (int i = 0; i < 5; i++)
    {
        math = math + num[i];
    }
    cout << "The sum of all numbers is: " << math << endl;
    double average = math / 5.0;
    cout << "The average of the numbers is: " << average << endl;
Topic archived. No new replies allowed.