logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

you will create a struct person and use it to create a struct node so that you can store a linked list of persons.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

The goal of this assignment is threefold: work with structs, work with linked lists, implement a “database-like” program that stores records that can be queried. Specifically, in this assignment, you will create a struct person and use it to create a struct node so that you can store a linked list of persons. You will then create a struct family and finally an array of struct family. A single struct family will store individual person structs in a linked list. You will implement various linked list functions and some other functions (described later).

A person will consist of:

  • first name (string (no more than 10 chars))
  • age (int)
  • sex (char)

A node will consist of:

  • a person struct
  • a next pointer (struct node *)

Assume that the struct person member has the name me (as used below).

A family will consist of

  • a last name (string (no more than 12 chars))
  • size (int) (number of people in the family, or the number of structs currently in the linked list)
  • a pointer to the front of the linked list of family members (struct node *)

You will create an array of your struct family, e.g., struct family families[10]; You will need no more than 10 array elements. To access a family, use families[i]. To access the linked list, use families[i].front. Given a struct node *temp, which is a node in a linked list, access that person’s name, age and sex using temp->me.first, temp->me.age, temp->me.sex. You are free to name your structs, members and array variable anything you like, the examples here are just to help you understand the structure of these three struct types and how to access into them.

Your program may consist of a single file or multiple files, your choice. You may but do not need a header file. Either way, make sure you comment your function prototypes! Your struct definitions should either be at the top of your single .c file if you only have one or in your .h file if you choose to use a header file. NOTE: define the structs in the order listed above because person is used in node and node is used in family.

Aside from main, you will need the following functions (at a minimum):

  • input – input a person’s data from disk
  • findFamily – given a person’s last name, determine the index i in the families array that matches that last name, or use a special value if that family does not yet exist (I use -1).
  • insertPerson – insert a new person (first name, age, sex) into families[i]. This is an ordered insert based on first name. This function should receive families[i].front (the pointer to this family’s linked list), and the new person’s first name, age and Allocate

a new node, copy into this node the person’s first name, age, sex, and insert the node into the linked list, returning a node pointer to the front of the list (needed when inserting into an empty list or at the front of a list).

  • outputFamily – receives a family’s front pointer (e.g., families[i].front) and the family’s last name (e.g., families[i].last) and outputs the family’s last name and then the first names of each person in the family; this is a traversal function to print out the elements of a linked list but only prints first names, not ages or
  • destroy – a function which receives the entire array of families and n (the number of families in the array), and destroys each family’s linked list, resetting each families[i].size to 0 and families[i].front to NULL
  • ageSearch – receives the front pointer of a family (families[i].front), last name (families[i].last) and an int (an age) and finds and outputs the first and last name of all people in the given linked list who are of the given
  • smallestLargest – receives the entire array of families and n, and determines the two families who have the least and most people, outputting those families, calling upon the outputFamily function to actually output the smallest and largest families. If two families have the same size, say families[i] and families[j], then output the one with the smaller index. That is, when searching, if families[i].size == families[firstfound].size then we ignore families[i].

You may write other functions if or as needed.

In main, declare your array of struct family elements (no more than 10 are needed). Declare the array statically (do not use a pointer/calloc), and declare any variables you need. Initialize your array of family elements by setting each element’s size to 0 and front pointer to NULL. You do not need to initialize the last names but if you do make sure you assign them using strcpy, not = as in strcpy(families[i].last, “”); Next in main, use a while loop to iterate through the contents of the input file until you reach EOF. This might look like what you implemented in program 2, for instance while(getInput(…)!=EOF) {…} or you can test for feof. The input function will need to input one row of the input text file which will consist of a first name, last name, age and sex.

  • Call the findFamily function to locate the array index in families that is currently storing the given last name, if there is one. In my program, findFamily returned -1 if no such family was
  • If there was no family, then you will add a new array element to families as follows:
    • assume n is the current number of families, then families[n] is the first unused array location. Set families[n].size to 1, set families[n].last to the last name as input (using strcpy), and call your insert function passing families[n].front. Remember that insert will return a front pointer, so this should be part of an assignment statement. Finally, increment
    • if a family was found, call insert passing it the new person info and that array element’s front pointer (e.g., families[location].front = insert(families[location.front], …) where … are the parameters for the new person. Finally, add 1 to that family’s
  • The insert function will be an ordered insert based on first name. Remember to implement the three cases (empty list, inserting at the front, inserting elsewhere). You will have to malloc a new node. If that node is called temp, to assign temp its first name, age and sex, the code will look something like this:

strcpy(temp->me.first, first); temp->me.age=age;

temp->me.sex=sex;

After exiting the while loop having inserted all new people into an appropriate linked list, do the following. Make sure you close the input file when done with it

Use a for-loop to iterate through all n families and print out all of the families using your outputFamily     function.    This function receives families[i].front and families[i].lastName so that the function can print out the family’s last name and then each first name in the linked list.

 

After the for-loop, prompt the user to enter an age (I’m calling it inputAge). Use a while loop that iterates while inputAge > 0. Search all of the families for anyone whose age is equal to the input age and output that person’s first and last names. This requires a for-loop inside the while loop to iterate through all n families, passing inputAge, families[i].lastName and families[i].front to the ageSearch function. If a family member is found for the given age, output that person’s first and last name. This while loop ends with another input for the next inputAge value. Upon entering 0 (or negative), exit the loop

 

Next, call the smallestLargest function, passing it the entire families array and n. This function locates and outputs the entire family information for the smallest family and the largest family. Make sure you output which is which.

Finally, pass to a destroy function the families array and n. This function uses a for-loop to iterate through each family[i] and an inner while loop to iterate through each node of this family’s linked list, freeing each node. After destroying a linked list, reset families[i].size to 0 and families.lastName to “” (again, using strcpy).

Run your program on the two data files on the website. An example of output from the first run is shown below. To fully test yours, use for inputAge these values: 53, 29, 57, 54 and 0.

 

Printing all families

Howe family: Dylan, Georgia, Janet, Steve, Virgil, Underwood family: Ian, Ruth,

Zappa family: Diva, Dweezil, Frank, Gail, Moon, Wakeman family: Jemma, Oliver, Rachel, Rick, Hackett family:       Jo, John, Steve,

Enter an age to search for: 53 Searching for all people of age 53:

Frank Zappa, Rachel Wakeman,

(5/5)
Attachments:

Expert's Answer

755 Times Downloaded

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

755 Times Downloaded

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

804 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

695 Answers

Hire Me
expert
Husnain SaeedComputer science

802 Answers

Hire Me
expert
Atharva PatilComputer science

941 Answers

Hire Me