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

Arrays of structs containing arrays, searching, and sorting. This program will s

ID: 3862135 • Letter: A

Question

Arrays of structs containing arrays, searching, and sorting. This program will serve to keep track 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 an array of the individual costs for each purchase (maximum of 8 doubles). Create an array of 10 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 up to 10 PurchaseRecord structs from the open input file. This function should count (and bring back) the number of records actually found in the file and should stop input at 10 if there is data for more than 10 records in the file. Note that the total cost values are not in the input, but will be calculated later in a different function. Call a calcTotals function that will receive the entire array of PurchaseRecords and will calculate and store the total cost values for each record that has data In a loop, call a function to sort the purchase array for each PurchaseRecord struct. This function should receive an array of doubles and bring back the sorted version. The sorting order should be from large to small Call a function to sort the array of PurchaseRecords by the name field. The function should receive the array of PurchaseRecords, sort them from small to large by the name field, and bring back the sorted version. Call a print function that will write out the data for every PurchaseRecord. The function should receive an array of PurchaseRecords and write out the data in the format shown in the sample runs Call a function to sort the array of PurchaseRecords by the total cost field. The function should receive the entire array of PurchaseRecords, sort it 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 will perform a search for the specified name. The function should receive the array of PurchaseRecords and thesearch 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, a shown in the sample runs. Comments: Name block Function comments descriptive comment before each definition

Explanation / Answer

Here is the code for you:

#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;

typedef struct
{
    string name;
    int numOfPurchases;
    double totalCostOfPurchases;
    double costs[8];
}PurchaseRecord;

int getData(ifstream &fin, PurchaseRecord records[])
{
    int count = 0;
    while(!fin.eof())
    {
       getline(fin, records[count].name);
       fin >> records[count].numOfPurchases;
       for(int i = 0; i < records[count].numOfPurchases; i++)
           fin >> records[count].costs[i];
       count++;  
    }
    return count;
}

void calcTotals(PurchaseRecord records[], int numOfRecords)
{
    for(int i = 0; i < numOfRecords; i++)
    {
       records[i].totalCostOfPurchases = 0.0;
       for(int j = 0; j < records[i].numOfPurchases; j++)
           records[i].totalCostOfPurchases += records[i].costs[j];
    }
}

void sortPurchases(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(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(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(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(PurchaseRecord records[], int numOfRecords, string name)
{
    for(int i = 0; i < numOfRecords; i++)
    {
       if(records[i].name.compare(name) == 0)
       {
          cout << records[i].name << " " << fixed << setprecision(2) << records[i].totalCostOfPurchases
              << records[i].costs[0] << " " << records[i].costs[records[i].numOfPurchases-1] << endl;
          return 0;     
       }
    }
    cout << "Name not found." << endl;
    return 0;
}
  
int main()
{
    //Create an array of 10 purchase record structs.
    PurchaseRecord records[10];
    //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.
    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;
    }
    //Call a getData function that will read data from upto 10 PurchaseRecords.
    int numOfRecords = getData(fin, records);
   
    //Call a calcTotals function that will receive the entire array of PurchaseRecords
    //and will calculate and store the total cost values for each record that has data.
    calcTotals(records, numOfRecords);
   
    //In a loop, call a function to sort the purchase array for each PurchaseRecord.
    for(int i = 0; i < numOfRecords; i++)
        sortPurchases(records[i].costs, records[i].numOfPurchases);
   
    //Call a function to sort the array of PurchaseRecords by the name field.
    sortRecordsByName(records, numOfRecords);
   
    //Call a print function that will write out the data for every PurchaseRecord.
    printRecords(records, numOfRecords);  
   
    //Call a function to sort the array of PurchaseRecords by the total cost field.
    sortRecordsByTotalPurchases(records, numOfRecords);
   
    //Call the same print function again to write out the data in the new order.
    printRecords(records, numOfRecords);
   
    //Ask the user for a name that will be searched for.
    cout << "Enter the name to be searched for in the records: ";
    //Read that name.
    string name;
    getline(cin, name);
    //Call a function that will perform a search for the specified name.
    searchForName(records, numOfRecords, name);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote