Question: C++ Parallel arrays and c-strings (char arrays). Note: all string operations should use c-strings, not string class or Cstring class. Read in pairs of data

C++ Parallel arrays and c-strings (char arrays). Note: all string operations should use c-strings, not string class or Cstring class.

Read in pairs of data from a text file. Each pair will include a name and an age. Store these two values in parallel arrays (an array of c-strings and an array of integers). (parallel means that the name and age should always be in the same position in their respective arrays) Sort both arrays according to names, alphabetically from first to last. Print out both arrays in the manner shown in the sample run. Sort both arrays according to age values from large to small. Print out both arrays again.

Specifically:

Declare an array of 10 c-strings with each string being 30 characters long. (Note, that this really makes it a 2-Dimensional array {i.e. arr[10][30], but after declarations, the second subscript can be left off to handle an entire c-string. For example, arr[3] would be one c-string with 30 characters of storage. You never need to address individual characters, so just one subscript except in declarations.}

Declare an array of 10 integers

Call a getData function that will read data for up to 10 pairs of names and ages from an input file, and bring back the data. The file may be opened in the main program or inside this function. This function should bring back all of the data found in the input file if there are 10 or fewer pairs, or exactly 10 if the file contains extra data. The user should be asked to enter the name of the input file

Call a function to sort both arrays according to name order from small to large (alphabetical first to last). The function should receive both arrays and the datasize. Compare only the names, but also swap the ages in the same place that you swap the names. Do not use the built-in swap() function for swapping the c-string values.

Call a print function that will write out names and ages in the format shown below

Call a function to sort both arrays according to age order from large to small. The function should receive both arrays and the datasize. Compare only the ages, but also swap the names in the same place that you swap the ages. Do not use the built-in swap() function for swapping the c-string values.

Call a print function that will write out names and ages in the format shown below.

Note about passing an array of c-strings to functions: Because the array is really a 2-Dimensional array, the type declaration in the function heading (and prototype) needs to have 2 square brackets [][], but the second one must have the maximum size for that dimension, which in this case is the length of each string (30 characters). So, a declaration in these locations will look like char x[ ][MAXSIZE], where MAXSIZE is either a numeric constant or a named constant. Once you get inside those functions, using just a single subscript will use the entire c-string (i.e. x[i] ).

Sample run using bonus.txt:

Enter name of input file: bonus.txt

Sorted by name:

Adams, Ann A. is 24 years old

Boyle, Billy is 21 years old

Gonzalez, Gary is 22 years old

Johnson, Stan is 26 years old

Johnson, Susan is 23 years old

Klingfelder, Kimberley K. is 18 years old

Persson, Peter is 20 years old

Robertson, Richard is 25 years old

Smith, John is 19 years old

Sorted by age:

Johnson, Stan is 26 years old

Robertson, Richard is 25 years old

Adams, Ann A. is 24 years old

Johnson, Susan is 23 years old

Gonzalez, Gary is 22 years old

Boyle, Billy is 21 years old Persson,

Peter is 20 years old Smith, John is 19 years old

Klingfelder, Kimberley K. is 18 years old

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!