Question
You have a phone book. The phone book is an array of a structures containing the names and the phone numbers of various people. Use
You have a phone book. The phone book is an array of a structures containing the names and the phone numbers of various people. Use the c library qsort to sort this phone book alphabetically. When you write your compare function do not use strcmp. After you sort, clean up the phone book memory.
#include
#include
#include
typedef struct {
char * pNames;
int iPhoneNum;
}PhoneEntries;
int main() {
int i = 0; PhoneEntries *pPhoneBook; pPhoneBook = (PhoneEntries*)malloc(sizeof(PhoneEntries)*5);
pPhoneBook[0].pNames = (char*)malloc(sizeof(char)*20);
pPhoneBook[1].pNames = (char*)malloc(sizeof(char)*20);
pPhoneBook[2].pNames = (char*)malloc(sizeof(char)*20);
pPhoneBook[3].pNames = (char*)malloc(sizeof(char)*20);
pPhoneBook[4].pNames = (char*)malloc(sizeof(char)*20);
strcpy(pPhoneBook[0].pNames,"Zoo Keeper");
pPhoneBook[0].iPhoneNum = 100;
strcpy(pPhoneBook[1].pNames,"Zebra");
pPhoneBook[1].iPhoneNum = 200;
strcpy(pPhoneBook[2].pNames,"Aardvark");
pPhoneBook[2].iPhoneNum = 300;
strcpy(pPhoneBook[3].pNames,"Python");
pPhoneBook[3].iPhoneNum = 400;
strcpy(pPhoneBook[4].pNames,"Kangaroo");
pPhoneBook[4].iPhoneNum = 500;
printf("Unsorted phone book ");
for(i=0; i < 5; i++) {
printf("Name %s ", pPhoneBook[i].pNames);
printf("Phone %d ", pPhoneBook[i].iPhoneNum);
} printf("Sorted phone book ");
for(i=0; i < 5; i++) {
printf("Name %s ", pPhoneBook[i].pNames);
printf("Phone %d ", pPhoneBook[i].iPhoneNum);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started