Thread: Problem, counting the number of occurrences of each number in an array

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Problem, counting the number of occurrences of each number in an array

    As part of finding the mode of an array of float numbers, the first step is to count the number of occurrences of each number in the array. I am trying to do this starting on line 166, but something is going horribly wrong. Someone please tell me what my error is:
    Code:
    ////  main.c
    //  statistics3
    //
    
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define MAXLINES 10
    
    
    int compare (const void * a, const void * b);
    
    
    //calculate mean prototype
    float calculate_mean(float function_data[],int number_of_data);
    
    
    int main (void)
    {
        int counter, number_of_data, printer_counter=1;
        float data[MAXLINES], standard_deviation_array[MAXLINES];
        int array_index=0;
        int x;
        int menu_selection = 0;
        int program_continue = 1;
        float mean, standard_deviation, variance, median, mode;
        //int input = 0;
        //char c;
        
    
    
        while (program_continue == 1)
        {
            printf("This program will perform the following:\n1. Enter data.\n2. Display the data and the following statistics: the number of data items, the high and low values in the data, the mean, median, mode, variance and standard deviation.\n3. Quit the Program\n");
            printf("select 1,2 or 3\n");
            
            //menu_selection = getchar();
            scanf("%d",&menu_selection);
            while(getchar() != '\n');
            
            switch (menu_selection)
            {
                case 1:
                    counter=1;
                    
                    printf("You have selcted #1. Please enter d when done. Could not get EOF to work.\n");
                    while ( (array_index <= MAXLINES-1) )
                    {
                        printf("enter data line #%d:",counter);
                        scanf(" %f",&data[array_index]);
                        array_index++;
                        counter++;
                        
                        if ( ( x=getchar() ) == 'd')
                        {
                            array_index = MAXLINES+1;
                        }
                    }
                    /*if (counter > MAXLINES)
                        printf("\n\nNumber of data lines: %d\n\n\n",counter-1);
                    if (counter < MAXLINES)
                        printf("\n\nNumber of data lines: %d\n\n\n",counter-2);*/
                    
                    array_index = 0;
                    
                    break;
                    
                case 2:
                    
    //printing the number of data lines:
                    if (counter > MAXLINES)
                        printf("\n\nNumber of data lines: %d\n\n\n",counter-1);
                    if (counter < MAXLINES)
                        printf("\n\nNumber of data lines: %d\n\n\n",counter-2);
                    
                    number_of_data=counter;
                    
                    while ( (array_index < MAXLINES) || printer_counter < counter)
                    {
                        printf("data line #%d:",printer_counter);
                        printf("%f\n",data[array_index]);
                        array_index++;
                        printer_counter++;
                    }
                    array_index = 0;
                    printer_counter = 1;
                    printf("\n");
                    
                    
                    int index=0;
                    float sum=0;
                    
    //Calculating the mean:
                    //mean=calculate_mean(data,number_of_data);
                    while (index<(number_of_data))
                    {
                        mean+=data[index];
                        index++;
                    }
                    
                    mean/=(number_of_data-1);
                    
                
                    printf("\n\nThe mean: %f\n\n",mean);
    
    
    //Calculating the standard deviation:
                    
                    index=0;
                    while (index<(number_of_data-1)) 
                    {
    //subtract the mean from every number to get the list of deviations. Next, square the resulting list of numbers:
                        
                        standard_deviation_array[index]=powf((data[index]-mean), 2);
    
    
    //Add up all of the resulting squares to get their total sum:
                        
                        sum+=standard_deviation_array[index];
                        index++;
                    }
                    variance=sum;
            //Calculating the variance:
                    variance/=index;
    
    
    //Divide the result by one less than the number of items in the list.
    //To get the standard deviation, just take the square root of the resulting number
                    
                    standard_deviation=sqrt(sum/(number_of_data-1));
        
                    printf("\n\nThe standard deviation: %f\n\n",standard_deviation);
                    
                    printf("\n\nThe variance: %f\n\n",variance);
                    
                    
    //finding the median
                    
    //First, sort the numbers from lowest to highest
                    qsort(data, MAXLINES, sizeof(float), compare);
    //then find the middle number
    
    
    //if the number of data items is odd, it is the middle number. 
                    
                    if (index%2 != 0)
                    {
                        median = data[(index/2)];
                    }
                    
    //But if the number of items is even, take the average of the two middle numbers.
                    
                    if (index%2 == 0)
                    {
                        median = (data[ (index/2) ]+data[ (index/2)+2 ])/2;
                    }
                    
                    printf("\n\nThe median: %f\n\n",median);
                    
                    
    //Calculating the MODE:
    //find the most frequently occuring number:
                    
    //start with the first cell , and compare to second, then third...compare to all the other cells
                    int index2=0, index3=0, mode_counter[MAXLINES];
    
    
    //index is equal to the number of data items:
                    
                    while (index2 < index )
                    {
                        while (index3 < index )
                        {
                            if (data[index2]==data[index3])
                            {
                                mode_counter[index2]++;
                            }
                            index3++;
                        }
                        index2++;
                        index3=0;
                    }
    //mode_couner array should now contain a tally of the number of occurences of each number in data array.
      
                    
                    index2=0;
                    index3=0;
                    
                    printf("mode tally:\n");
                    printf("%d\n",mode_counter[0]);
                    printf("%d\n",mode_counter[1]);
                    printf("%d\n",mode_counter[2]);
                    printf("%d\n",mode_counter[3]);
                    printf("%d\n",mode_counter[4]);
                    printf("%d\n",mode_counter[5]);
                    printf("%d\n",mode_counter[6]);
                    printf("%d\n",mode_counter[7]);
                    printf("%d\n",mode_counter[8]);
                    printf("%d\n",mode_counter[9]);
                    
                    break;
                    
                case 3:
                    printf("goodbye!");
                    program_continue = 0;
                    break;
                    
                default: printf("That is not a valid entry.\n");
            }
    
    
        }
        return 0;
    }
    
    
    //calculating the mean
    float calculate_mean(float function_data[],int number_of_data)
    {
        int index;
        float total=0;
        for(index=0;index<=number_of_data;index++)
            total=total+function_data[index];
        return (total/number_of_data);
    }
    
    
    int compare (const void * a, const void * b)
    {
        return ( *(int*)a - *(int*)b );
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Charlie Lesoine
    As part of finding the mode of an array of float numbers, the first step is to count the number of occurrences of each number in the array.
    Mode of a single number or modal class? I worry that floating point inaccuracy poses a problem with "finding the mode of an array of float numbers".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    I am just trying to find the standard mathematical mode. The number or numbers which appear most frequently

    For example:

    1,1,1,2,2,3,9,4,6

    The mode is 1

    or
    1,1,1,2,2,2,3,8,6,5,0

    The mode is 1 and 2

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Charlie Lesoine
    I am just trying to find the standard mathematical mode.
    In that case, I suggest that you work with integers first. The basic idea is to map the numbers to their corresponding frequencies. The simplest way is to just create a huge array of unsigned int, such that there is a direct mapping from the number to an array index (and thus, to a frequency). Another way is to define something like:
    Code:
    typedef struct
    {
        int number;
        unsigned int frequency;
    } Occurrence;
    Upon which you search for the number and update its frequency. If the number cannot be found, you add it to the array with a frequency of 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Well the array that I am using to count the frequency (mode_counter) is an integer array. The other numbers are floats because I have to calculate other things with them, but I am not sure why that would effect the counting of frequency.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Charlie Lesoine
    Well the array that I am using to count the frequency (mode_counter) is an integer array.
    Yes.

    Quote Originally Posted by Charlie Lesoine
    The other numbers are floats because I have to calculate other things with them, but I am not sure why that would effect the counting of frequency.
    The problem is that to update a number's frequency, you need to look it up. However, the problem when looking up a floating point number is that, due to floating point inaccuracy, a straightforward equality comparison may give a false negative. One way out is to compare within a very small range, but then if you have different numbers in the data that fall within the same range that you assumed for comparison, you would end up with a false positive.

    EDIT:
    Plus once you starting comparing with a range, your mode would really be a modal class.
    Last edited by laserlight; 12-04-2011 at 10:07 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    I just did a google search for "equality comparison with float" And found a page that provided a solution to this issue. instead of using an equality comparison something like this instead:

    if (fabs(result - expectedResult) < 0.00001)

    I will try this.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Quote Originally Posted by
    [COLOR=blue
    if[/COLOR] (fabs(result - expectedResult) < 0.00001)

    I will try this.

    This did not work...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of items in an [][]array
    By A_User in forum C Programming
    Replies: 5
    Last Post: 11-20-2011, 07:54 PM
  2. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  3. Counting Occurrences in an array
    By aggie324 in forum C Programming
    Replies: 4
    Last Post: 11-19-2010, 11:17 PM
  4. Counting number of strings in an array
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 06-21-2010, 11:25 AM
  5. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM

Tags for this Thread