Question

Vectors of structs containing vectors, searching, and sorting. This program will serve to keep track of...

Vectors of structs containing vectors, 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 a vector of the individual costs for each purchase (maximum of 8 doubles). Create a vector of an unknown 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 knows its own size). 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 vector 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 vector for each PurchaseRecord struct. This function should receive a vector 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 PurchaseRecords by the name field. The function should receive the vector 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 a vector of PurchaseRecords and write out the data in the format shown in the sample runs.

Call a function to sort the vector of PurchaseRecords by the total cost field. The function should receive the entire vector 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 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 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

file1:

```Kyle Kaiser
317.50 290.53 35.29
Joan Jensen
428.95 123.45 96.72 88.87
Tom T. Thompson
525.93 18.67 6.50 75.29 85.45
Edgar Edelman
2273.44 329.16
Sue Swanson
332.99 61.19 91.23
6312.06 115.42 61.14 54.32 56.09 18.65
Ben B. Benson
333.21 5.95 21.85
```

A vector is a sequence of data elements of the same basic type. Members in a vectorare officially called components. Nevertheless, we will just call them members in this site. Here is a vector containing three numeric values 2, 3 and 5.

ector (molecular biology) ... In molecular cloning, a vector is a DNA molecule used as a vehicle to artificially carry foreign genetic material into another cell, where it can be replicated and/or expressed. A vector containing foreign DNA is termed recombinant DNA.

A vector is a sequence of data elements of the same basic type. Members in a vector are officially called components. Nevertheless, we will just call them members in this site.

Here is a vector containing three numeric values 2, 3 and 5.

> c(2, 3, 5)
[1] 2 3 5

And here is a vector of logical values.

> c(TRUE, FALSE, TRUE, FALSE, FALSE)
[1]  TRUE FALSE  TRUE FALSE FALSE

A vector can contain character strings.

> c("aa", "bb", "cc", "dd", "ee")
[1] "aa" "bb" "cc" "dd" "ee"

Incidentally, the number of members in a vector is given by the length function.

> length(c("aa", "bb", "cc", "dd", "ee"))
[1] 5

Earn Coins

Coins can be redeemed for fabulous gifts.