Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

...oo T-Mobile F 23% 1:06 AM 1 of 3 CPSa 121 Spring 2017 Program W7, due Thurs 3

ID: 3802149 • Letter: #

Question



...oo T-Mobile F 23% 1:06 AM 1 of 3 CPSa 121 Spring 2017 Program W7, due Thurs 3/23/17 by 11pm on Titanium Repeat program A6, but using vectors instead of arrays (Instructions that have been changed Vectors of structs containing vectors searching, and sorting. This program will serve to keeptrack of people's purchase records. Specifically: Create a struct type that will represent a purchase record, where each PurchaseRecord has a name (a string that may contain blank spaces, the number of purchases (1-8, the total cost of all of the purchases combined, and avector of the individual costs for each purchase (maximum of8 doubles) Create avetog ofanunknown number of PurchaseRecord structs. Ask the user for the name of an input file and open that file. Write out a "not found" message and quit if the file is not found successfully. Call a getData function that will read data for an unknown number of PurchaseRecord structs from the open input file. This function should bring back the vector Containing the input data (no count is needed as a parameter since the vector Note that the total cost values are not in the input, but wil different function. be calculated later in a Call a calcTotals function that will receive the entire vector of PurchaseRecords and wil calculate and store the total cost values for each record that has data. In a loop, call a function to sort the purchase vector for each PurchaseRecord struct. This function should receive avector of doubles and bring back the sorted version. The sorting order should be from large to small. Call a function to sort the vector of P by the name field. The function should receive the vector of PurchaseRecords, sort them from small tolarge by the name field, and bring back the sorted version. Calla print function that will write out the data for every PurchaseRecord. The function should receive avector of PurchaseRecords and write out the data in the format shown in the sample runs. Calla function to sort the vector of PurchaseRecords by the total cost field. The function should receive the entire vector of PurchaseRecords, sortit from large to small by the total cost field, and bring back the sorted version. Call the same print function again to write out the data in the new order. Ask the user for a name that will be searched for, and read in that name (it may contain blank spaces. Call a function that wilperform a search for the specified name. The function should receive the vector of PurchaseRecords and the search name, perform the search, and then write out a "not found type of message or name, total cost, and largest and smallest purchases if it is found, as shown the sample runs. in

Explanation / Answer

#include <iostream>

#include <fstream>

#include <iomanip>

#include <string>

using namespace std;

struct PurchaseRecord

{

string name;

int numOfPurchases;

double totalCostOfPurchases;

std::vector<double> costs(8);

};

int searchForName(std::vector<PurchaseRecord> records, int numOfRecords, string name);

void printRecords(std::vector<PurchaseRecord> records ,int numOfRecords);

void sortRecordsByTotalPurchases(std::vector<PurchaseRecord> records, int numOfRecords);

void sortRecordsByName(std::vector<PurchaseRecord> records ,int numOfRecords);

void sortPurchases(std::vector<double> costs ,int count);

void calcTotals(std::vector<PurchaseRecord> records ,int numOfRecords);

int getData(ifstream &fin, std::vector<PurchaseRecord> records);

int main()

{

std::vector<PurchaseRecord> records;

string fileName;

cout << "Enter the name of the file: ";

cin >> fileName;

ifstream fin;

fin.open(fileName);

if (!fin.is_open())

{

cout << "File not found." << endl; return 0;

}

int numOfRecords = getData(fin, records);

for (int i = 0; i < numOfRecords; i++)

sortPurchases(records[i].costs, records[i].numOfPurchases);

sortRecordsByTotalPurchases(records, numOfRecords);

printRecords(records, numOfRecords);

cout << endl;

sortRecordsByName(records, numOfRecords);

PurchaseRecord. printRecords(records, numOfRecords);

cout << "Enter the name to be searched for in the records: ";

string name;

getline(cin, name);

cin.clear();

cin.ignore();

int location;

location = searchForName(records, numOfRecords, name);

if (location != -1)

{ cout << location; }

}

int getData(ifstream &fin, std::vector<double> costs)

{

int count = 0;

char ch;

while (count < 10 && fin >> ch)

{

fin.putback(ch);

getline(fin, records.name);

fin >> records.numOfPurchases;

for (int i = 0; i < records.numOfPurchases; i++)

{ fin >> records.costs[i]; } count++; }

return count;

}

void calcTotals(std::vector<PurchaseRecord> records ,int numOfRecords)

{

for (int i = 0; i < numOfRecords; i++)

{

records.totalCostOfPurchases = 0.0;

for (int j = 0; j < records.numOfPurchases; j++)

records.totalCostOfPurchases += records.costs;

}

}

void sortPurchases(std::vector<double> purchases, int count)

{

for (int i = 0; i < count - 1; i++)

for (int j = 0; j < count - i - 1; j++)

if (purchases[j] < purchases[j + 1])

{

double temp = purchases[j];

purchases[j] = purchases[j + 1];

purchases[j + 1] = temp;

} }

void sortRecordsByName(std::vector<PurchaseRecord> records ,int numOfRecords)

{ for (int i = 0; i < numOfRecords - 1; i++)

for (int j = 0; j < numOfRecords - i - 1; j++)

if (records[j].name.compare(records[j + 1].name) > 0)

{ PurchaseRecord temp = records[j]; records[j] = records[j + 1]; records[j + 1] = temp; }

}

void sortRecordsByTotalPurchases(std::vector<PurchaseRecord> records, int numOfRecords)

{

for (int i = 0; i < numOfRecords - 1; i++)

for (int j = 0; j < numOfRecords - i - 1; j++)

if (records[j].totalCostOfPurchases < records[j + 1].totalCostOfPurchases)

{ PurchaseRecord temp = records[j]; records[j] = records[j + 1]; records[j + 1] = temp;

} }

void printRecords(std::vector<PurchaseRecord> records, int numOfRecords)

{

for (int i = 0; i < numOfRecords; i++)

{ cout << records[i].name << ", " << records[i].numOfPurchases << " purchases for a total of " << fixed << setprecision(2) << records[i].totalCostOfPurchases << endl;

for (int j = 0; j < records[i].numOfPurchases; j++)

cout << fixed << setprecision(2) << records[i].costs[j] << " "; cout << endl;

} }

int searchForName(std::vector<PurchaseRecord> records, int numOfRecords, string name)

{

int location = -1;

bool wr = false;

for (int i = 0; i < numOfRecords && !wr; i++)

{

if ((records[i].name.compare(name) == 0))

{

location = i;

cout << records[i].name << ", " << fixed << setprecision(2) << records[i].totalCostOfPurchases << endl << "Largest and smallest purchases: " << records[i].costs[0] << " " << records[i].costs[records[i].numOfPurchases - 1] << endl; wr = true;

}

}

return location;

}