Hi all,

I'm trying to further my knowledge of C by linking some of my class work together so I get a better understanding of building more complete programs.

At the moment I've got two small programs, the first opens a file for writing then writes 500 instances of random numbers between 0 and 100 to that file.

The second, calls that first file and opens the second file for writing, it then allocates each number to an array then does a bubble sort of that array and writes the sorted list to the second file.

I've got all of this working fine, and what I want to do is to count each time a number occurs so I can output something like:

There are x instances of number 0,
There are x instances of number 1,
etc, etc.

Usually when doing something like this in my course work I would use a switch command but having 100 case arguments isn't particularly efficient coding. So I am wondering what would be the best way to proceed?

I've included both programs below:

First program:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

# define NUMCOUNT 500

int square (int);

void main()
{
   FILE *fpin;
   int count = 0, rand();

   if ((fpin = fopen("H:\\numbers.dat", "w")) == NULL)
      printf("Cannot open file");
   else
   {
      for (count = 1; count <= NUMCOUNT; ++count)
         fprintf(fpin, "%d\n", rand() % 101);
      if (fclose (fpin) == EOF)
         printf("Cannot close file");
   }
   printf("%d random numbers below or equal to 100\n", NUMCOUNT);
   printf("have been written to: \"H:\\numbers.dat\"");
   getch();
}
Second Program:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define SIZE 500

void fill_array(int, int list[]);
void print_array(int, int list[]);
void sort_array(int, int list[]);

FILE *fpin, *fpout;
int n, list[SIZE];
void main()
{

   fpout = fopen("H:\\SP3 misc. work\\sorted_list.dat", "w");
   fpin = fopen("H:\\numbers.dat", "r");

   if ((fpin == NULL) || (fpout == NULL))
   {
      printf("There is an error opening one of the files");
   }
   else
   {
      fill_array(n,list);
      sort_array(n,list);
      print_array(n, list);
   }

   if ((fclose(fpin) == EOF) || (fclose(fpout) == EOF))
   printf("There is an error closing on of the files");
   else
   printf("File numbers.dat has been sorted\n");
   printf("\n\n*************************\n");
   puts("Press any key to continue");
   getch();
}

void fill_array(int, int list[])
{
   int count =0;
   char cr;
   while  (fscanf(fpin, "%d%c", &list[count], &cr) != EOF)
   {
      count++;
   }
   return;
}

void print_array(int, int list[])
{
   int count;
   for (count=0; count<=SIZE; count++)
      fprintf(fpout, "%d\n", list[count]);
   return;
}

void sort_array(int, int list[])
{
   int temp;                 // Temp varible used in swapping process
   int count1, count2;       // Counters for two nested loops
   for (count1=0; count1<=(SIZE-1); count1++)
   {
      for (count2=(count1+1); count2<=SIZE; count2++)
      {
         if (list[count1] > list[count2])
         {
            temp = list[count1];             // Swapping process
            list[count1] = list[count2];     // Swapping process
            list[count2] = temp;             // Swapping process
         }
      }
   }
   return;
}