Write a program that reads the prices of a shop’s products continuously until the user enters −1. The program should display the minimum price, the maximum, and the average of those within [5,30], before it terminates. Assume that none of the product’s costs more than $100.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
//vector of all prices
vector<double>vec;
//vector of prices in limit [5,30]
vector<double>lim;
double price;
cout<<"Please, enter the prices of a shop`s products (\"-1\" - exit)\n";
cout<<"Enter: ";
while(price!=-1)
{
cin>>price;
if(price!=-1)
vec.push_back(price);
}
vector<double>::iterator it;
//Filling vector of prices in limit [5,30]
for(it=vec.begin();it!=vec.end();++it)
{
if((*it)>=5&&(*it)<=30)
lim.push_back(*it);
}
sort(lim.begin(),lim.end());
double sum=0;
//Finding sum of all elements in limit [5,30]
for(it=lim.begin();it!=lim.end();++it)
{
sum+=(*it);
}
cout<<"\nThe minimum price within [5,30] is "<<lim[0];
cout<<"\nThe maximum price within [5,30] is "<<lim[lim.size()-1];
cout<<"\nThe average price within [5,30] is "<<sum/lim.size();
}
Comments
Leave a comment