Answer to Question #48552 in Engineering for Himani Sharma

Question #48552
Write a program in C++ to store the price list of n items and print the largest price as well as sum of all prices by using class?
1
Expert's answer
2015-01-19T03:59:09-0500
main.cpp
#include "LargestPriceFinder.h"
#include <cstdlib>
int main()
{
LargestPriceFinder finder;
finder.AcceptPrices();
finder.FindMaxSum();
system("pause");
return 0;
}
LargestPriceFinder.h
#ifndef __LARGEST_PRICE_FINDER_H__
#define __LARGEST_PRICE_FINDER_H__
#include <vector>
class LargestPriceFinder
{
public:
void AcceptPrices();
void FindMaxSum();
private:
std::vector<double> store;
};
#endif /*__LARGEST_PRICE_FINDER_H__*/
LargestPriceFinder.cpp
#include "LargestPriceFinder.h"
#include <iostream>
#include <limits>
#include <algorithm>
#include <numeric>
using namespace std;
template <class T>
T AcceptDigit(char * prompt, T min, T max)
{
T result;
do
{
cout << prompt << endl;
cin >> result;
} while (result < min || result > max);
return result;
}
void LargestPriceFinder::AcceptPrices()
{
int n;
n = AcceptDigit<int>("Please enter the number of prices to enter:", 0, INT_MAX);
for (int i = 0; i < n; ++i)
store.push_back(AcceptDigit<double>("Please enter the number of prices to enter:", 0, LONG_MAX));
}
void LargestPriceFinder::FindMaxSum()
{
cout << "Max element is: " << *max_element(store.begin(), store.end()) << endl;
cout << "The sum is: " << accumulate(store.begin(), store.end(),0) << endl;
}


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
APPROVED BY CLIENTS