L-35 MCS 260 Fri 9 Nov 2001

In this lecture we introduced arrays. The first program illustrate the differences between the index type and the type of the elements in the array. The following programs manage frequency tables.
/* L-35 MCS 260 Fri 9 Nov 2001 : illustration of an array 

The goal of this program is to illustrate the difference between
the indices and the entries of an array.  In this example we use
an array to hold the revenues for every work day in the week.
The indices of the array are days in the week, implemented by
values of an enumeration type.  The entries of the array are
dollar amounts, implemented by floats.                        */

enum workday { monday, tuesday, wednesday, thursday, friday };
typedef  enum workday  workday;

#include<stdio.h>
#define  N  5

void print_day ( workday d );
/* prints the name of the day */

int main ( void )
{
   float revenues[N] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
   int i;

   revenues[monday] = 123.42;
   revenues[tuesday] = 233.21;
   revenues[wednesday] = 32.91;
   revenues[thursday] = 421.13;
   revenues[friday] = 212.31;

   printf("\nThe revenues for each day of the week are: \n");
   for (i = 0; i < N; i++)
   {
      print_day(((workday) i));
      printf("  $ %6.2f\n", revenues[i]);
   }

   return 0;
}

void print_day ( workday d )
{
   switch(d)
   {
      case monday    : printf("Monday   "); break;
      case tuesday   : printf("Tuesday  "); break;
      case wednesday : printf("Wednesday"); break;
      case thursday  : printf("Thursday "); break;
      case friday    : printf("Friday   ");
   }
}
/* L-35 MCS 260 Fri 9 Nov 2001 : frequency table to test random numbers

The use of this program is to test how evenly spaced random numbers are.
For example, if we generate 5000 random numbers and do mod 10 each time,
then we would expect to find approximately 500 times 0, 500 times 1,
500 times 2, etc..

With this program we test the spacing of the random numbers, using
a frequency table for the random numbers generated.

Two issues are worth noting :

1) The declaration of the array is done inside a function.
   In this fashion, the compiler automatically allocates sufficient
   memory for this local variable.  This technique allows to build
   flexible programs: the size of the array is determined at run time
   and does not have to be fixed in advance.

2) When distributing the random numbers over the frequency table,
   we use the % operator as index inside the bracket.  As we calculate
   modulo the size of the array, we are guaranteed that the index stays
   inside the range of the array.                                   */

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void random_test ( int n, int number_of_buckets );
/* generates n random numbers and places each random number is a bucket,
   the size of the distribution is given in number_of_buckets,
   the frequency table and the deviation from the expected value,
   that is n/number_of_buckets, is printed.                         */

int main ( void )
{
   int N;   /* how many random numbers to generate */
   int d;   /* size of distribution */

   printf("\nTesting the distribution of random numbers\n");
   printf("  How many random numbers to generate ? ");
   scanf("%d", &N);
   printf("  How many buckets in the distribution ? ");
   scanf("%d", &d);

   random_test(N,d);

   return 0;
}

void random_test ( int n, int number_of_buckets )
{
   int bins[number_of_buckets];    /* frequency table */
   double dev[number_of_buckets];  /* deviation from expected value */
   double expected;                /* expected value in each bucket */
   int i;                          /* counter to enumerate elements */
   int r;                          /* some random number */

   for (i = 0; i < number_of_buckets; i++) 
      bins[i] = 0;                 /* initialize frequency table */

   srand(time(NULL));
   for (i = 0; i < n; i++)         /* distribute random numbers */
      bins[rand() % number_of_buckets]++;

   printf("\nThe distribution of the random numbers :\n");
   for (i = 0; i < number_of_buckets; i++)
      printf("  %d", bins[i]);     /* print the frequency table */
   printf("\n");

   expected = ((double) n) / ((double) number_of_buckets);
   for (i = 0; i < number_of_buckets; i++)
      dev[i] = bins[i] - expected;

   printf("\nDeviation from the expected value %.2f :\n", expected);
   for (i = 0; i < number_of_buckets; i++)
      printf("  %.2f", dev[i]);
   printf("\n\n");
}
/* L-35 MCS 260 Fri 7 Nov 2001 : counting letters in text 

In the program below we use a frequency table to count the
number of occurrences of each lower case letter in a text.
The indexing of the frequency table is made easy by
the consecutive arrangement of the ASCII codes.

The program is best run via redirection, as a.out < file.    */

#include<stdio.h>

#define  NUMBER_OF_LETTERS   26

int main(void)
{
   int freq[NUMBER_OF_LETTERS];
   int i,c;

   for (i = 0; i < NUMBER_OF_LETTERS; i++) 
      freq[i] = 0;

   while ((c = getchar()) != EOF)
      if (('a' <= c) && (c <= 'z')) freq[c-'a']++;

   for (i = 0; i < NUMBER_OF_LETTERS; i++) 
   {
      printf("    #%c = %4d", i + 'a', freq[i]);
      if (i % 5 == 4) printf("\n");
   }
   printf("\n");

   return 0;
}