CS157 |
Homework 3: Sorting, Arrays of Structures, PointersObjectives
DescriptionFor this assignment, you will complete a mostly-written program. The program contains the roster for the Club of Heroes, namely:
The program’s purpose is to sort this information in various ways. The program will take a variable number of arguments. Each
argument is either “ The program will first print out the unsorted list of heroes. Then, for each argument given, the program will sort the list of heroes according to that key, and print out the newly-sorted list of heroes. Requirements
Sample Runs % gcc -Wall -std=c99 hero.c -o hero
% ./hero
Usage: ./hero sort-key ...
% ./hero year
*** Unsorted:
Name Alias Year
Spider-Man Peter Parker 1962
Wonder Woman Diana Prince 1941
Captain America Steve Rogers 1941
Superman Clark Kent 1938
Buffy the Vampire Slayer Buffy Summers 1992
Zorro Don Diego de la Vega 1919
*** Sorted by "year":
Name Alias Year
Buffy the Vampire Slayer Buffy Summers 1992
Spider-Man Peter Parker 1962
Captain America Steve Rogers 1941
Wonder Woman Diana Prince 1941
Superman Clark Kent 1938
Zorro Don Diego de la Vega 1919
% ./hero name alias
*** Unsorted:
Name Alias Year
Spider-Man Peter Parker 1962
Wonder Woman Diana Prince 1941
Captain America Steve Rogers 1941
Superman Clark Kent 1938
Buffy the Vampire Slayer Buffy Summers 1992
Zorro Don Diego de la Vega 1919
*** Sorted by "name":
Name Alias Year
Buffy the Vampire Slayer Buffy Summers 1992
Captain America Steve Rogers 1941
Spider-Man Peter Parker 1962
Superman Clark Kent 1938
Wonder Woman Diana Prince 1941
Zorro Don Diego de la Vega 1919
*** Sorted by "alias":
Name Alias Year
Buffy the Vampire Slayer Buffy Summers 1992
Superman Clark Kent 1938
Wonder Woman Diana Prince 1941
Zorro Don Diego de la Vega 1919
Spider-Man Peter Parker 1962
Captain America Steve Rogers 1941
SkeletonMost of the program has already been written—you just have to fill in a
few functions. Look for “ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_SIZE 26 struct hero { char name[MAX_NAME_SIZE]; char alias[MAX_NAME_SIZE]; int year; }; void printRecords(struct hero *club); void sortRecords(char *key, struct hero *club); int main(int argc, char *argv[]) { struct hero heroes[] = { // name alias year {"Spider-Man", "Peter Parker", 1962}, {"Wonder Woman", "Diana Prince", 1941}, {"Captain America", "Steve Rogers", 1941}, {"Superman", "Clark Kent", 1938}, {"Buffy the Vampire Slayer", "Buffy Summers", 1992}, {"Zorro", "Don Diego de la Vega", 1919}, {"", "", 0000} }; if (argc == 1) { printf("Usage: %s sort-key ...\n", argv[0]); return 1; } printf("*** Unsorted:\n"); printRecords(heroes); for (int i=1; i<argc; i++) { char *key = argv[i]; sortRecords(key, heroes); printf("\n*** Sorted by \"%s\":\n", key); printRecords(heroes); } return 0; } // Print the records, with a nice header. void printRecords(struct hero *club) { // YOUR CODE GOES HERE } // Sort the array of records, // according to the parameter key. // // That is, if key is "name", then sort the records // by the name of the hero: Batman first, Zorro last. // // If key is "alias", then sort by the alter ego: // Buffy Summers is first, Steve Rogers is last. // // If key is "year", then sort in DESCENDING ORDER by year. // // Anything else, print an error message. // // You must use each type of sorting that we discussed in class. // Three sort keys, three different types of sorting. void sortRecords(char *key, struct hero *club) { // YOUR CODE GOES HERE } GradingThis assignment is worth 10 points. You’ll lose points if you submit a file that:
How to submit your homework:Go to the same directory as the ~cs157/bin/checkin HW3 hero.c How to check your answer after you turn it in: ~cs157/bin/peek HW3 hero.c How to receive negative points: Turn in someone else’s work.
|