A program that can sort a list of names and telephone numbers alphabetically. Persons are sorted alphabetically by their first names. Persons with the same first name are sorted by their last names.

Following are the guidelines for program,program must include these things.

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);

Recommended Answers

All 2 Replies

Please read our rules concerning homework.

I think you should do your homework on you own, at least put up your own code and then ask about suggestion.

Here are some code just for reference.

struct Person{
 char firstname[32];
 char lastname[32];
 char telnum[32];
};

/* 
return: -1 if a<b
        0 if a==b
        1 if a>b
*/
int compare(Person *a, Person *b){
    if( strcmp(a->firstname,b->firstname) ){
        return strcmp(a->firstname,b->firstname);
    }
    if( strcmp(a->lastname,b->lastname) ){
        return strcmp(a->lastname,b->lastname);
    }
    return 0;
}

void swap(Person *x, Person *y){
    Person t;
    t = *x;
    *x = *y;
    *y = t;
}
commented: Not the best example, but a good first post! :-) +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.