CS157

PmWiki

edit SideBar

Homework 3: Sorting, Arrays of Structures, Pointers

Objectives

  • Array of structures
  • Sorting
  • Pointers

Description

For this assignment, you will complete a mostly-written program. The program contains the roster for the Club of Heroes, namely:

NameAliasAppeared
Spider-ManPeter Parker1962
Wonder WomanDiana Prince1941
Captain AmericaSteve Rogers1941
SupermanClark Kent1938
Buffy the Vampire SlayerBuffy Summers1992
ZorroDon Diego de la Vega1919

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 “name”, “alias”, or “year”.

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

  • If no arguments are given, emit a usage message. (I did that for you.)
  • If an invalid sort key is given, emit an error message. This message must mention what the invalid key is. Alas, main() will then print the unsorted roster, which is OK.
  • For each sort key (name, alias, year) you must use a different sorting algorithm. Use insertion sort of one of them, bubble sort for another, and selection sort for a third. You pick which sort to use for which key.
  • When sorting by year, it doesn’t matter which 1941 hero comes first.
  • When sorting by year, the most recent hero (Buffy) comes first.
  • When sorting by alias, simply sort by the entire string. This means that “Buffy Summers” comes before “Clark Kent”, even though “Kent” comes before “Summers” in the phone book. That’s ok. Frankly, I have no idea where “Don Diego de la Vega” would go in a phone book, and this avoids that issue.

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

Skeleton

Most of the program has already been written—you just have to fill in a few functions. Look for “YOUR CODE GOES HERE”, and add your code there. Don’t change anything else in the program. Here it is:

#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
}

Grading

This assignment is worth 10 points. You’ll lose points if you submit a file that:

  • changes any part other than that marked “YOUR CODE GOES HERE
  • doesn’t compile
  • doesn’t work like the sample run
  • isn’t named hero.c
  • uses language features that we have not yet covered
  • changes any of the prompts or messages
  • contains poor code:
    • cryptic variable names
    • missing/useless comments
    • extra { } for no reason
  • and many, many, more!

How to submit your homework:

Go to the same directory as the hero.c file, and do this:

    ~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.
Edit - History - Print - Recent Changes - Search
Page last modified on November 30, 2008, at 05:11 PM