Skip to content

Homework 5: Groups

Due Tuesday, July 25, 2023 at 11:59pm

In this assignment, you will write a C program that groups a list of names by their keys.

  • Compile and test frequently
  • Read the entire assignment first before you start
  • Start early and do not do all of the assignment in one sitting; coding is fun but fighting for hours with broken code is not
  • Do not hesitate to seek help if you are stuck

Synopsis

In this homework, you will work in the hw5 directory.

hash.{h,c}: This file implements the cmp function and hash function for string keys. You can use the functions to create a hash table like this: table_create(hint, strcmp_, strhash);.

Written: You will answer some simple questions at the end.

Learning Objectives:

  • Put everything we talked about in the first half of the quarter into use.
  • Writing and debugging C programs from scratch.

Getting started

See homework 3.

This homework depends on having a working CS143 library lib. If yours does not work, you can use the solution library provided in solution-lib branch of the starter repository.

Specification

You will write a program called groups, which when given a list of names and their keys identifies groups of the names that share the same key.

Your input is given by a command-line argument. If the input is stored in a file input.txt, we will invoke your program by ./groups input.txt. The output of your program should be on standard output stdout.

The input is a sequence of lines of the following format:

  • The line begins with one or more characters, which may contain whitespaces other than '\n'; this is the key.
  • The key is followed by a single tab character '\t', separating the key and the name.
  • The name begins with the next character and continues through the end of the line. The name may contain any whitespaces other than '\n'.

The output of your program is specified as follows:

  • If a key is associated with exactly one name, ignore the key (and the name);
  • If a key is associated with two or more names, the key and the names constitute a group.

For each group, your program should print in the following format:

  • The first line is the key of the group, immediately followed by a colon :.
  • Print each name in the group in its own line.
  • Print one additional blank line at the end.

You may print the groups in any order, and you may assume the input file is well formatted.

If groups is invoked with the wrong number of arguments or if groups is provided with an invalid file, a helpful error message should be printed and groups should terminate with error code 1.

A reference implementation in Python is provided in groups.py.

An example input:

F   Bolotin Ramsayer
C   Defrancisco Nunn
F   Worman Nilakantan
F   Bradwell Viehweg
D   Kensler Callaghan
A   Donatelli Morocz
E   Leisy Caskey
A   Ruud Roots
A   Stidham Kushnir
A   Hamre Carrillo
A   Applin Solman

Assuming the above content is stored in a file named example.txt, running ./groups example.txt should produce the following to the standard output:

A:
Donatelli Morocz
Ruud Roots
Stidham Kushnir
Hamre Carrillo
Applin Solman

F:
Bolotin Ramsayer
Worman Nilakantan
Bradwell Viehweg


Written

You need to answer some questions in hw5/WRITTEN.txt.

Submission checklist

In hw5/:

  • make produce no errors and produce an executable called groups.

All changes are committed and pushed to your github repository. Submit your program to Gradescope by selecting your coursework directory and the correct branch.

Grading

Percentage
Correctness 70%
Style 20%
Written 10%

Warning: If your program cannot be compiled using the commands above without error or warning, you will receive 0 points in correctness since there is no executables for us to run.