Answer to Question #282071 in C++ for sai

Question #282071

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.


1
Expert's answer
2021-12-22T14:18:21-0500
#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();
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog