Work on the following exercises. Complete for homework whatever you don't finish in lab. You do not have to upload and hand in any print outs of your work in lab. Just upload your completed programs to VikingWeb by next Wednesday.
You may work in pairs or groups of 3 for all parts of this assignment.
Starter files:These are files I handed out in class yesterday. Look over the main()
function in the source code file and make sure you understand how it is constructing a structure representing a family tree. Also examine the printing functions to see how they traverse the structure to print it out. In this lab, you will write some functions that make it easier to construct the family tree structure. (Compare the main()
function below with the one in the source code file that you are given. The first half of the function below is supposed to achieve the same results, but it is easier to read and understand.)
print_person()
function so it also prints the age of the person as well as their birth year. The age is approximately the current year minus the person's birth year. Use the functions and structures in the time.h
header file (see page 557 and the example on page 559 for how to use the time()
and localtime()
functions to obtain a struct tm
structure that contains information about the current date).
family.h
header file. You should probably implement and test them in the order they are listed. Feel free to introduce other functions if you think they will be helpful implementing those 7.
Given below is a sample main()
function and the output it should produce once you have finished implementing the functions. Actually, I also already include below the main()
function an implementation of the concatenate()
function, so that's one less function you have to write.
int main( void ) { person *jane, *john, *jill, *jack; person *bob; jane = new_person( "Jane", 1945, NULL, NULL ); john = new_person( "John", 1943, NULL, NULL ); set_married( jane, john ); jill = new_person( "Jill", 1977, john, jane ); jack = new_person( "Jack", 1980, john, jane ); print_person( john ); print_person( jane ); print_person( jill ); print_person( jack ); /* possible to test the code up to here after implementing the first three functions */ bob = new_person( "Bob", 1979, NULL, NULL ); set_married( jill, bob ); /* new_person adds the entry to the children of the parents automatically */ new_person( "BeeJay", 1999, bob, jill ); new_person( "Beth", 2001, bob, jill ); printf( "\nDescendents of Jane\n\n" ); print_personlist( descendents( jane ), false ); return 0; } /* provided implementation - notice its use of add_to_end, which you have to implement */ personlist* concatenate( personlist *listA, personlist *listB ) { personlist *cur = listB; while (cur) { listA = add_to_end( listA, cur->p ); cur = cur->next; } return listA; }
---------------------- Name: John Born: 1943 Age: 62 Spouse: Jane Children: Jack Jill ---------------------- ---------------------- Name: Jane Born: 1945 Age: 60 Spouse: John Children: Jack Jill ---------------------- ---------------------- Name: Jill Born: 1977 Age: 28 Father: John Mother: Jane ---------------------- ---------------------- Name: Jack Born: 1980 Age: 25 Father: John Mother: Jane ---------------------- Descendents of Jane ---------------------- Name: Jack Born: 1980 Age: 25 Father: John Mother: Jane ---------------------- ---------------------- Name: Jill Born: 1977 Age: 28 Father: John Mother: Jane Spouse: Bob Children: Beth BeeJay ---------------------- ---------------------- Name: Beth Born: 2001 Age: 4 Father: Bob Mother: Jill ---------------------- ---------------------- Name: BeeJay Born: 1999 Age: 6 Father: Bob Mother: Jill ----------------------