Ceng laboratory (5)

Page 1

LAB 9 In this exercise, you will model people and their families. For the memory representation of a person, you will need something like: enum gender {M,F}; struct person { int ID; enum gender G; struct person * father; struct person * mother; } PART I (5 pts) Notice that this representation allows you to figure out the relationship between any two given persons. For example, if two persons point to the same father (a->father == b->father), then they are siblings. If, otherwise, (a->father->father == b->father->father), they a and b are cousins, etc. As such you are to complete the code by writing the following functions: int isAncestor(struct person * p1, struct person * p2) which returns 1 if p1 is an ancestor of p2, and returns 0 otherwise. int areRelated(struct person * p1, struct person * p2) which returns 1 if there is a close relationship that, for example, prevents marriage between p1 and p2 (like brother-sister, aunt-nephew, etc.) and returns 0 otherwise. PART II (5 pts) In this part, you will use the functions you have developed above to detect the errors in a family record provided in a text file 'record.txt'. (This file is given to you alongside this sheet). This file consists of several lines each of which contains information about one individual in the following format: 5 M 3 4: means that the man with ID 5 is the son of the man w/ ID 3 and the woman w/ ID 4 10 F 1 1: means that the woman with ID 10 is the daughter of the man w/ ID 1 and the woman w/ ID 1 You are to write some code that reads the information in this file and stores it in the memory (for example, you can use arrays) and looks for errors. For example, if the father and the mother of an individual appear to be related, this information is certainly wrong. Detect all the erroneous listings and report them either to the screen or to a separate .log file.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.