computer science hw

profile47290v3o0
example.txt

//Lab Assignment #7: Vectors //Program Name: lab7part1 //Purpose of the program: working with vectors //Authors: Wei Zhou //Date: 03/03/2016 #include<iostream> #include<iomanip> #include<vector> #include<string> using namespace std; //enter the number of boxes sold for each type of cookie void numberSell(vector<string> cookienames, vector<int> &amount) { int numberSales; for (int i = 0; i < cookienames.size(); ++i) { cout << "Please enter the total sales for " << cookienames[i] << ": "; cin >> numberSales; while (numberSales < 0) { cout << "Error: total sales cannot be a negative number." << endl; cout << "Please enter the total sales for " << cookienames[i] << ": "; cin >> numberSales; } amount.push_back(numberSales); } cout << endl; } //calculate total sales int totalSales(vector<int> amount) { int total = 0; for (int i = 0; i < amount.size(); ++i) { total += amount[i]; } return total; } //find the lowest selling cookie string findBest(vector<string> cookienames, vector<int> amount) { int highest = amount[0], exponent = 0; for (int i = 1; i<amount.size(); ++i) { if (amount[i] >= highest) { highest = amount[i]; exponent = i; } } return cookienames[exponent]; } //find the highest selling cookie string findWorst(vector<string> cookienames, vector<int> amount) { int lowest = amount[0], exponent = 0; for (int i = 1; i<amount.size(); ++i) { if (amount[i] <= lowest) { lowest = amount[i]; exponent = i; } } return cookienames[exponent]; } //calculate percentages of sales vector<double> findPercentage(vector<int> amount, int total) { double percentage; vector<double> line; for (int i = 0; i < amount.size(); ++i) { percentage = amount[i] * 100 / total; line.push_back(percentage); } return line; } //display report void finalResult(vector<string> cookienames, vector<int> amount, int total, vector<double> percentage, string best, string worst) { cout << left << setw(30) << "Type of Cookie" << setw(25) << "Boxes Sold" << "Percentage" << endl; cout << "---------------------------------------------------------------" << endl; for (int i = 0; i < cookienames.size(); ++i) { cout << fixed << setprecision(2); cout << left << setw(30) << cookienames[i] << setw(25) << amount[i] << setw(9) << percentage[i] << "%" << endl; } cout << endl; cout << "Total number of boxes sold this month: " << total << endl; cout << left << setw(45) << "Best seller cookie: " << best << endl; cout << left << setw(45) << "Worst seller cookie: " << worst << endl; cout << endl; } int main() { cout << "Cookie sales tracking program" << endl; cout << "-------------------------------" << endl; //declare and initialize vectors as needed vector<string> cookienames = { "Thin Mints", "Tagalongs", "Shortbread", "Lemonades", "Caramel deLites", "Do-si-dos", "Thanks-A-Lot", "Rah-Rah Raisins", "Toffee-tastic", "Cranberry Citrus Crisps", "Trios", "Savannah Smiles" }; void numberSell(vector<string> cookienames, vector<int>& amount); int totalSales(vector<int> amount); string findBest(vector<string> cookienames, vector<int> amount); string findWorst(vector<string> cookienames, vector<int> amount); vector<double> findPercentage(vector<int> amount, int total); void finalResult(vector<string> cookienames, vector<int> amount, int total, vector<double> percentage, string best, string worst); vector<int> amount; int total; string best, worst; vector<double> percentage; numberSell(cookienames, amount); total = totalSales(amount); worst = findWorst(cookienames, amount); best = findBest(cookienames, amount); percentage = findPercentage(amount, total); finalResult(cookienames, amount, total, percentage, best, worst);