Higher Computing 1B - Week 6 Laboratory Exercises

Welcome to your fifth Computing 1B laboratory.

This lab involves:

Create a separate directory (lab06 is a good name) using mkdir for this week's lab exercises.

Exercise 1: Counting Whales

Your task this week is to write a program to assist a biologist tally their counts of migrating whales.

As pods of migrating whales swim past the biologist types the number and species of whales.

Here is an example of the input the biologist will provide


15 humpbacks
3 killer whales
2 sperm whales
19 belugas
2 humpbacks
2 sperm whales
7 killer whales
4 pygmy right whales
7 humpbacks


You will notice the biologists enters one line of input for each pod. Each line contains first the number of whales in the pod followed by the species of whales.

When the end of input is reached the program then must print a tally of how many of each species of whale were seen. For the above input, it should print:

24 humpbacks
10 killer whales
4 sperm whales
19 belugas
4 pygmy right whales

Notice, tallies of species are listed in the order they were first seen.

Write a C program, whales.c, which reads input in the above format and prints output as indicated.

Assumptions

You can assume the above input format is followed. You can assume no line is longer than 80 characters. You can assume no whale name is longer than 40 characters. You can assume no more than 100 different whale species will be entered.

But your program can make no other assumptions about the whale names that will be entered. In particular, no mention of particular whales names can be made within your program.

Hints

Define two arrays one for whale names, and one for whale tallies. The value at a particular index in each array should correspond to each other. So if element 10 of the whale names array contains "humpbacks" then element 10 of the whale tallies array should contain the tally for "humpbacks".

Altenatively read ahead about structs and define a struct which can store the tally for a single species of whale. It will need two fields, one for the whale name, one for the tally of that whale so far seen. Declare an array of structs to hold tallies for all the whales so far seen.

The C library functions strcpy, strcmp and atoi may be useful.

Finished?

Make sure you test each program thoroughly. When you are sure all are working show them to your tutor to receive this week's lab mark.

After your tutor has checked that you answers are correct and given you the lab mark, you must also submit your answers using give. Here is the command to use:

% /home/cs1721/bin/classrun -give lab06 whales.c