06 November 2012

Part 11 - Word-length histogram I

Topics covered

Array applications
Counting word lengths
Modifying a program to perform a new task



Watch video >>


Downloadable files

histowordlen.c


Exercises

1. The program histowordlen.c should count the lengths of each word in the input and draw a histogram based on the word lengths. The program as we left it does the work of counting the word lengths, but it is missing the last part: printing out the actual histogram. Fill in the last part of the program by adding an appropriate for loop to print a visual representation of the counts array. For example, suppose the contents of counts[1] is 4, counts[2] is 8, counts[3] is 2, counts[4] is 1, and the rest are zero. Then the histogram may look like this:

0
1  xxxx
2  xxxxxxxx
3  xx
4  x
5
6
7
8
9
...

One way to do this is to use a nested for loop. For example, if counts has elements counts[0], counts[1], ..., counts[MAX-1]:

for (int i=0; i < MAX; i++) {
    // ...
    for (int j=0; j < counts[i]; j++) {
        // ...
    }
    // ...
}

The lines marked ``// ...'' are to be filled in with your own statements to output the histogram.

2. Arrays may be used with any type, not just integers. Consider the following array declaration:

char *monthname[] = {"(None)", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

Notice that if month is an integer with a numeric month 1, 2, ..., 12, then the expression monthname[month] will evaluate to a string which is that month's name. Using this array declaration, write a program printdate.c which takes three commandline arguments DAY MONTH YEAR and prints the date in the following format:

21st September, 1983

For example, entering the command printdate 2 8 2000 should produce the following result:

2nd August, 2000

3. Extend the program printdate so that it also prints the day of the week along with the output. The day of the week is to be determined automatically by your program based on the DAY, MONTH and YEAR which are provided by the user as commandline arguments. Use the following array declaration:

char *wname[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Note that if w is 0, then wname[w] refers to "Sunday". There exists a formula to calculate w based on the day, month and year. Suppose month is an integer 1, 2, ..., 12, day is an integer 1, 2, ..., 31, and year is an integer 1, 2, ..., 10000. Then the following formula will calculate w:

    int w;
    {
        int Y = (month==1 ? year-1 : month==2 ? year-1 : year);
        int m = ((month + 9) % 12) + 1;
        int y = Y % 100;
        int c = Y / 100;
        w = (day + (int)(2.6 * m - 0.2) + y + y/4 + c/4 - 2*c) % 7;
        if (w < 0) w += 7;
    }


The % operator is called the modulus operator ``mod''; it is the remainder after division. The formula to calculate w is called Gauss's Calendar Formula. An example run of the program is  printdate 6 11 2012 and should produce the result:

Tuesday, 6th November, 2012

Links

Wikipedia - Determination of the day of the week - Gaussian algorithm

Gauss' Calendar Formula for the Day of the Week

No comments:

Post a Comment