Arrays of structs containing arrays, searching, and sorting. This program will s
ID: 3862792 • 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 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 in the sample runs. Comments: Name block Function comments – descriptive comment before each definition Sample run 1: Enter name of input file: in6a.txt Abe Adams, 6 purchases for a total of 617.68 312.06 115.42 61.14 56.09 54.32 18.65 Edgar Edelman, 2 purchases for a total of 602.60 329.16 273.44 Kyle Kaiser, 3 purchases for a total of 343.32 290.53 35.29 17.50 Joan Jensen, 4 purchases for a total of 337.99 123.45 96.72 88.87 28.95 Tom T. Thompson, 5 purchases for a total of 211.84 85.45 75.29 25.93 18.67 6.50 Sue Swanson, 3 purchases for a total of 185.41 91.23 61.19 32.99 Ben B. Benson, 3 purchases for a total of 61.01 33.21 21.85 5.95 Abe Adams, 6 purchases for a total of 617.68 312.06 115.42 61.14 56.09 54.32 18.65 Ben B. Benson, 3 purchases for a total of 61.01 33.21 21.85 5.95 Edgar Edelman, 2 purchases for a total of 602.60 329.16 273.44 Joan Jensen, 4 purchases for a total of 337.99 123.45 96.72 88.87 28.95 Kyle Kaiser, 3 purchases for a total of 343.32 290.53 35.29 17.50 Sue Swanson, 3 purchases for a total of 185.41 91.23 61.19 32.99 Tom T. Thompson, 5 purchases for a total of 211.84 85.45 75.29 25.93 18.67 6.50 Enter a name to find: James Jones James Jones was not found in the data Sample run 2: Enter name of input file: in6b.txt Polly Partidge, 2 purchases for a total of 722.30 406.40 315.90 Abe Adams, 6 purchases for a total of 617.68 312.06 115.42 61.14 56.09 54.32 18.65
Explanation / Answer
Solution:
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
struct record
{
string name;
int number;
double totalcost;
double cost[8];
}r[10];
int getData(ifstream &in)
{
int count=0;
string line;
while(getline(in,line))
{
if(count==10)
break;
stringstream ss(line);
string str;
int k=0;
while(getline(ss,str,','))
{
if(k==0)
{
r[count].name=str;
k++;
}
else
{
stringstream ss1(str);
string str1,s;
int m=0;
getline(ss1,str1,' ');
ss1>>r[count].number;
for(int i=0;i<5;i++)
ss1>>s;
ss1>>r[count].totalcost;
for(int i=0;i<r[count].number;i++)
{
ss1>>r[count].cost[i];
}
}
}
count++;
}
return count;
}
void calculateTotal( int num)
{
for(int i = 0; i < num; i++)
{
r[i].totalcost = 0.0;
for(int j = 0; j < r[i].number; j++)
r[i].totalcost += r[i].cost[j];
}
}
void sortPur(double p[], int count)
{
for(int i = 0; i < count-1; i++)
for(int j = 0; j < count-i-1; j++)
if(p[j] < p[j+1])
{
double t = p[j];
p[j] = p[j+1];
p[j+1] = t;
}
}
void sortRecName(int num)
{
for(int i = 0; i < num-1; i++)
for(int j = 0; j < num-i-1; j++)
if(r[j].name.compare(r[j+1].name) > 0)
{
record t = r[j];
r[j] = r[j+1];
r[j+1] = t;
}
}
void sortRecordsTotalCost(int num)
{
for(int i = 0; i < num-1; i++)
for(int j = 0; j < num-i-1; j++)
if(r[j].totalcost < r[j+1].totalcost)
{
record t = r[j];
r[j] = r[j+1];
r[j+1] = t;
}
}
void print_Records( int num)
{
for(int i = 0; i < num; i++)
{
cout << r[i].name <<", " <<r[i].number << " purchases for "
<< r[i].totalcost << endl;
for(int j = 0; j < r[i].number; j++)
cout << r[i].cost[j] << " ";
cout << endl;
}
}
int search(int num, string name)
{
for(int i = 0; i < num; i++)
{
if(r[i].name==name)
{
cout << r[i].name << " " << r[i].totalcost
<< r[i].cost[0] << " " << r[i].cost[r[i].number-1] << endl;
return 0;
}
}
cout << "Name not found." << endl;
return 0;
}
int main()
{
ifstream in;
string filename;
cout<<"Enter the file name: ";
cin>>filename;
in.open(filename);
if(!in)
{
cout<<"The file is not found successfully"<<endl;
}
int num = getData(in);
calculateTotal(num);
for(int i = 0; i < num; i++)
sortPur(r[i].cost, r[i].number);
sortRecName(num);
print_Records(num);
sortRecordsTotalCost(num);
print_Records( num);
cout << "Enter the name : ";
string name;
cin>>name;
search( num, name);
system("pause");
return 0;
}
Sample Output:
Enter the file name: in6a.txt
Abe Adams, 6 purchases for 617.68
312.06 115.42 61.14 56.09 54.32 18.65
Edgar Edelman, 2 purchases for 602.6
329.16 273.44
Joan Jensen, 4 purchases for 337.99
123.45 96.72 88.87 28.95
Kyle Kaiser, 3 purchases for 343.32
290.53 35.29 17.5
Tom T. Thompson, 5 purchases for 211.84
85.45 75.29 25.93 18.67 6.5
Abe Adams, 6 purchases for 617.68
312.06 115.42 61.14 56.09 54.32 18.65
Edgar Edelman, 2 purchases for 602.6
329.16 273.44
Kyle Kaiser, 3 purchases for 343.32
290.53 35.29 17.5
Joan Jensen, 4 purchases for 337.99
123.45 96.72 88.87 28.95
Tom T. Thompson, 5 purchases for 211.84
85.45 75.29 25.93 18.67 6.5
Enter the name : anil
Name not found.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.