Lowest Value In Vector Problem
Dec 5, 2014 at 2:42am UTC
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;
}
Dec 5, 2014 at 8:56am UTC
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
#include<iostream>
#include<vector>
int main()
{
std::vector<double > values;
// Some test data
values.push_back(4.5);
values.push_back(34.6);
values.push_back(5.8);
values.push_back(17.4);
values.push_back(9.8);
double minVal = 1000.0;
double maxVal = 0.0;
for (int i = 0; i < values.size(); ++i)
{
double element = values.at(i);
if (element < minVal)
{
minVal = element;
}
if (element > maxVal)
{
maxVal = element;
}
}
std::cout << "Max: " << maxVal << std::endl;
std::cout << "Min: " << minVal << std::endl;
return 0;
}
Topic archived. No new replies allowed.