Telephone directory
Make a program that can sort a list of names and telephone numbers alphabetically. Persons are sorted alphabetically by their last names. Persons with the same last name are sorted by their first names.
Input: names and telephone numbers,
Output: list of names and telephone numbers ordered alphabetically.
Define a structure named person that contains a person's first name, last name, and telephone number.
Define a function that compares two structures of type person according to the following prototype:
int compare(person * a, person * b);
This function should return a negative value if person a comes before b, and a positive value if b comes before a. (You may use the function stricmp(name1,name2) to compare two strings).
Define a function that swaps the contents of two structures of type person:
void swap(person * x, person * y);
Use the following function to sort an array of structures of type person:
void sort(person * list, int NumberOfPersons)
{ // sort list using bubble sort method (page 96):
int a, b;
for (a=1; a<NumberOfPersons; a++)
{
for (b=NumberOfPersons-1; b>=a; b--)
{
if (compare(list+b-1, list+b) > 0)
{
swap(list+b-1, list+b);
}
}
}
}