This assignment involves arrays, structs, strings and files. Write an application that can read data records from a file to an array of structs. The data must be formatted to be in the proper case whereas the first letter of each word must be capital and the remaining in lower case as it’s read into the array of structs. Also write a function to sort the array by the last name and then by the first name. A simple way to do this is to make a temporary array of strings as long as the array of structs so it can concatenate the last name and first name into one string. (example: “David” “Riverra” would create a concatenated string such as “Riverra” “David”) – Use strcmp() to sort it.

You must swap the data in both arrays simultaneously. Also use a swap function to swap the structs and strings simultaneously as well. The application will need to find a person’s record in an array of structs by using a binary search. The structs will have to store only digits of the birthdays, SSN and phone numbers. Write functions so that you can format the birthday, SSN and phone number make it appear more legible (example: 01/25/1986, 483-99-7754 and 236-880-1212) Last part of the assignment is to write the data out to a file as mailing labels.

Data required for struct is:

id – An integer value representing the person’s unique id within the system

• first_name – A string representing the first name (should be 25 chars)

• last_name – A string representing the last name (should be 25 chars)

• address – A string representing the address (should be 35 chars)

• city – A string representing the city (should be 25 chars)

• state – A string representing the state (should be 3 chars)

• zip – A string representing a Zip code (should be 6 chars)

• phone – A string representing the phone number (should be 11 chars)

• birth – A string representing the birth date (should be 9 chars)

• ssn – A string representing the Social Security number (should be 10 chars)

• gender – A char representing gender (should be 1 chars)

These are the functions that are needed to create this application.

main – Main function with menu and data declarations.

• proper – Convert a string to proper case.

• read – Read the file into the array of structs (make sure to convert to proper case).

• copy – Copy all of the data from one struct to another.

• swap – Swap the data in two structs.

• sort – Sort the array of structs by last then first name (you can use selection or bubble sort).

• isSorted – Test an array to see if it is sorted.

• find – Find a person’s data in the array by first and last name. Should return the index of the person’s record if found, -1 otherwise. This should use a binary search.

• datef – Format as a date. Returns a string in a readable format for a date (i.e. 01/01/2000).

• ssnf – Format as a SSN. Returns a string in a readable format for an SSN (i.e. 190-89- 6543).

• phonef – Format as a phone number. Returns a string in a readable format for a phone number (i.e. 215-555-1212).

• print – Print a record to screen using the birth date, ssn and phone formats as shown in Figure 1

• write – Writes the records out to a file to be used as mailing labels as shown in the figures below. There should be a blank line separating each mailing label in the file.

Just an example of format. This is screen output for a struct. Figure 1

------

/

ID:1/

Name:John Summers/

Address: 678 S Morland St/

City: California/

State: CA/

Zip:90012/

Phone:234-878-1234/

SSN:123-345-6543/

Birth Date:01/23/1976/

Gender: M/

------

Figure 2---- As a mailing Label

------

John Summers

678 S Morland St

California, CA 90012

------

There is no data missing from the struct, It’s okay to use strtok() to express the records as they are read from the file. Since there are spaces within the data, you should use fegts() to read the lines of text instead of Fscanf() to read the formatted text from the files. This application will require a specific menu to navigate its primary functions. They are:

1: read the file

2: sort the records

3: Find a record

4: write mailing labels

5: exit

Write read function

Read a line from the file (and print it to screen)

Tokenize the line (and print data to screen)

Copy data to a struct (May want to write print to test this)

Copy all data to an array of structs

Write function to convert to proper case and test and use in read function

Write format date, ssn and phone functions

Write print function and test

Write copy function and test

Write the sort function and test

Write find function and test

Write the write mailing label function and test

Pseudo Code for Binary Search

binary search is more efficient than a selection search but requires a sorted array. A selection search is ?(?), meaning that in the worst case, you have to check all ? elements in a ? -length array. The binary search is ?(log ?), meaning that in the worst case, you only have to check log ? elements. That’s 8 comparisons for an array with 256 elements or only 30 for an array with 1G elements.

// begin = 0

// end = length-1 o

// If array is not sorted –> sort it

// While begin is less than or equal to end

// mid = (begin+end)/2

// If query value is greater than mid value

begin = mid + 1

// If query value is less than mid value

end = mid -1

// Else (they are equal)

Return mid

// Return -1

Pseudo Code for toProperCase Method

- Initially consider last char was a space (at beginning of string)

-Convert string to lower case (loop through string using tolower())

Do

if current char is between a and z (can also use isalpha())

If last char was a space

Convert char to upper case (can use toupper())

Else if current char is a space (can use isspace())

Set last char was a space to true

Else

Set last char was a space to false

Until the last char in string is processed

Pseudo Code for Bubble Sort

Note: i is the boundary for the integers that are already sorted and n is the number of elements in the array. The inner loop iterates from end of array to beginning pulling the smallest value to the 0 index on the first pass.

// i is the outer loop index o

// j is the inner loop index o

// From i = 0, while i < n-1, increment i ▪

// From j = n-1, while j > i, decrement j •

// If the jth element is less than the j-1th element o

// Swap them

The Data File

The data file for this lab is “dataWdups.csv”. It is in “comma separated values” or csv format. Each piece of data is separated by a comma. You will have to use fgets() to read a line of text and use strtok() to separate the data into fields. Open the file in Notepad to view the data.