Data Structure - Coursework
Data Structure - Coursework
Make a program that discovers the level of relatedness between two people in a family. For
this, a structure (struct) must be created to represent the Person type. The Person type must store
values according to the table below:
two. Then read the data from these no people. Data reading should be done as
follows:
2.1 each of the no following lines represents a different person;
2.2 Each line has three pieces of information: the person's name, the person's age, and your name.
direct ascendant (father's or mother's name);
2.3 Note that it is not necessary to create an attribute for the “direct ascendant” in the Person struct.
ALTHOUGH, the direct ascendant must have their sons and daughters registered in the “children”
attribute. To find the direct ascendant, use the functionsearch person described in item 5;
2.4 Also note that the number of descendants the Person has is not informed. The ideal
is to reallocate the “children” attribute using the realloc function whenever a new child is
informed by input.
3. After registering people, you must store in an integer m the number of kinship-level
queries to be done;
4. Then read the parameters of the m queries. The reading of queries should be done
as follows:
4.1 Each row represents a different query;
4.2 Each query is represented by a pair of names, the first name being that of the
descendant (son/grandson/great-grandson…) and the second name is that of the ascendant (father/grandfather/great-grandfather…);
4.3 For each query, you must print the level of relatedness between the name pair.
mentioned in the consultation. To do this, use the searchPerson function described below by passing the
parameter printLevel with value 1.
struct Person* searchPerson (struct Person *Ascperson, char searchname[], int level, int printLevel);
The second parameter is the name of the person being searched, as entered in the data entry.
After finding the person you are looking for, the findPerson function should return a pointer to the struct
of the person you are looking for. However, before returning the person found, you must test the value
of the “imprimeNivel” parameter. If “printLevel” is equal to 1, print the level of relationship between the
ascendant and descendant involved in the query. If “printLevel” is equal to zero, don't print the level and
just return the struct of the person found. Thus, the searchPerson function can be
used whenever you have the person's name but don't have the person's struct. In this case, just use the
parameter “imprimeNivel” with a value equal to 0 and the function will search and return the person's struct
without printing other information.
The third parameter is the level of relatedness between the ascendant and descendant
of the query. This level of kinship must be increased throughrecursion as you walk through the
family tree.
The fourth parameter informs whether the relatedness level should be printed or not.
the level of kinshipshould not be printed when you are just looking for a person by the family
tree (for example, to comply with 2.3). And the level of kinshipmust be printed
when executing the queries requested by data entry, as described in item 4.3.
6. Finally, you must free the allocated memory using the “free()” command.
Input: 5
John 50 NULL
Mary 27 John
Joana 6 Mary
Carlos 30 João
Gisele 2 Maria
3
Gisele João
Carlos João
Joana Maria
Exit: two
1
1
In Moodle test cases it is guaranteed that, with the exception of the first Person in the data
entry, all next persons are descendants of someone previously informed. Test cases about uncle or
aunt will not be included. And test cases with two or more people with the same name will not be
included.