Programming Assignment 1 - Counting N-Tuples

Advanced Programming II, Fall 2001


Due Date

This assignment is due by 2:00 p.m. on Thursday, 20 September.

See the assignment turn-in page for instructions on turning in your assignment.

Background

A white-space character is any character that causes the <cctype> predicate isspace() to be true. A word as a maximal sequence of non-white-space characters. An n-tuple is a sequence of n words, n >= 1; words in a tuple are separated from one another by at least one white-space character.

The Problem

Write a program that reads words from standard-in and writes to standard-out the n-tuples found along with a count of how many times each n-tuple appeared. Letter case and white-space characters are not significant when comparing n-tuples; the 2-tuple

Joe

  Blow

is the same as the 2-tuple

joe blow

The format for each line of output written is

count n-tuple
where count is the number of times the n-tuple appeared in input and n-tuple is the n-tuple itself. The output does not have to be arranged in any particular order.

The program should accept zero, one, or two numeric command-line arguments. If no command-line arguments are given, the program should count 1-tuples (that is, single words). If a single, numeric command-line argument n is given, the program should count n-tuples. If a pair of numeric command-line arguments min, max is given, the program should count n-tuples between min-tuple and max-tuple inclusive. Any other command-line arguments should cause an error.

For example, suppose the file song contains the text

Yes banana no
No yes no bananas today
We gotta no bananas
Yes we gotta no bananas today
and the name of the program is count-tuples. Then the command

count-tuples < song

is equivalent to the command

count-tuples 1 < song

and produces the output

2  gotta
2  We
2  today
3  bananas
5  no
1  banana
3  Yes

The command

count-tuples 2 < song

is equivalent to the command

count-tuples 2 2 < song

and produces the output

2  gotta no
2  We gotta
1  today We
1  bananas Yes
2  bananas today
3  no bananas
1  no yes
1  no No
1  banana no
1  Yes we
1  Yes no
1  Yes banana

The command

count-tuples 3 4 < song

is equivalent to the command

count-tuples 4 3 < song

and produces the output

2  gotta no bananas
1  gotta no bananas today
1  gotta no bananas Yes
2  We gotta no
2  We gotta no bananas
1  today We gotta
1  today We gotta no
1  bananas Yes we
1  bananas Yes we gotta
1  bananas today We
1  bananas today We gotta
1  no bananas Yes
1  no bananas Yes we
2  no bananas today
1  no bananas today We
1  no yes no
1  no yes no bananas
1  no No yes
1  no No yes no
1  banana no No
1  banana no No yes
1  Yes we gotta
1  Yes we gotta no
1  Yes no bananas
1  Yes no bananas today
1  Yes banana no
1  Yes banana no No

The command

count-tuples 3 4 5 < song

produces the output

Command format is "count-tuples [min] [max]".


This page last modified on 13 September 2001.