Answer to Question #77721 in C++ for Phil

Question #77721
A supermarket wants to reward its best customer of each day, showing the customer’s name on a screen in the supermarket. For that purpose, the customer’s purchase amount is stored in a vector<double> and the customer’s name is stored in a corresponding vector<string>.

Implement a function

string name_of_best_customer(vector<double> sales,

vector<string> customers)

that returns the name of the customer with the largest sale.
1
Expert's answer
2018-06-01T04:54:27-0400
#include <iostream>
#include <vector>
#include <string>

using namespace std;

string name_of_best_customer(vector<double> sales, vector<string> customers)
{
size_t bestIndex = 0; // best customer index

// find index of the largest sale
for (size_t i = 0; i < sales.size(); ++i)
{
if (sales[i] > sales[bestIndex])
bestIndex = i;
}

return customers[bestIndex];
}

int main()
{
vector<double> sales;
vector<string> customers;

sales.push_back(100.0);
customers.push_back("A");

sales.push_back(300.0);
customers.push_back("B");

sales.push_back(200.0);
customers.push_back("C");

cout << "The best customer of the day is ";
cout << name_of_best_customer(sales, customers) << endl;

return 0;
}

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