Thread: Counting array elements

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    Counting array elements

    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;
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Since the numbers are limited in range to 0..100, you can just track the totals in an array:

    Code:
    int numberOfTimesSeen[101];
    
    for(i = 0; i < 101; ++i)
    {
        numberOfTimesSeen[i] = 0;
    }
    
    
    for(i = 0; i < numElements; ++i)
    {
        numberOfTimesSeen[element[i]]++;
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Many thanks brewbuck,

    I assumed there would be a simple solution to this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM