Search code examples
cbinning

binning integer values in C


I've been at this for about two months by myself and here is my first stand alone program that i wrote by myself. It models the behavior of random variables. It draws SIZEA number of random values between 1 and 100 then enters them into an array and calculates mean and standard deviations. This random experiment can be repeated SIZEB number of times. Mean values of all repeated experiments are again entered into an array and used to calculate expect value of the mean and standard error. Finally, values of each individual experiment and mean values of each experiment are binned (bin size 10 and 5 respectively) and then used to calculate frequencies and histogram. To bin the values I used nested if / else statements producing a relatively large amount of lines of code. I am wondering if anyone can come up with a more elegant solution to binning these values?

Here is the code binning is in lines 27 to 74 and lines 99 to 200.

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

//define system constants
#define SIZEA 100
#define SIZEB 300

//i, j, n  are indices

int
main(void)
{
    size_t i = 0;
    int A[SIZEA];
    float B[SIZEB];
    int C[SIZEB];

    srand(time(NULL));
    // outer loop for SIZEB number of individual sampling events
    while (i < SIZEB) {
        printf("\n");
        size_t j = 0;
        int FREQUENCY[10] = { 0 };
        // inner loop for SIZEA number of drawings constituting each sampling
        // event
        while (j < SIZEA) {
            int grade = rand() % 100 + 1;

            A[j] = grade;
            printf("%3d\n", A[j]);

            // bin values of each sampling event
            if (A[j] > 0 && A[j] <= 10) {
                ++FREQUENCY[0];
            }
            else {
                if (A[j] > 10 && A[j] <= 20) {
                    ++FREQUENCY[1];
                }
                else {
                    if (A[j] > 20 && A[j] <= 30) {
                        ++FREQUENCY[2];
                    }
                    else {
                        if (A[j] > 30 && A[j] <= 40) {
                            ++FREQUENCY[3];
                        }
                        else {
                            if (A[j] > 40 && A[j] <= 50) {
                                ++FREQUENCY[4];
                            }
                            else {
                                if (A[j] > 50 && A[j] <= 60) {
                                    ++FREQUENCY[5];
                                }
                                else {
                                    if (A[j] > 60 && A[j] <= 70) {
                                        ++FREQUENCY[6];
                                    }
                                    else {
                                        if (A[j] > 70 && A[j] <= 80) {
                                            ++FREQUENCY[7];
                                        }
                                        else {
                                            if (A[j] > 80 && A[j] <= 90) {
                                                ++FREQUENCY[8];
                                            }
                                            else {
                                                if (A[j] > 90 && A[j] <= 100) {
                                                    ++FREQUENCY[9];
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            ++j;
        // inner loop end
        }
        for (size_t n = 0; n < 10; ++n) {
            printf("\n%3d", FREQUENCY[n]);
        }
        int sum = 0;

        for (j = 0; j < SIZEA; ++j) {
            sum += A[j];
        }
        printf("\n");
        float mean = (float) sum / SIZEA;

        printf("\nMean of Array values is: %1f\n", mean);
        int sumsq = 0;

        for (j = 0; j < SIZEA; ++j) {
            sumsq = sumsq + pow((A[j] - mean), 2);
        }
        printf("Sum of squares is: %1d\n", sumsq);
        int stdev;

        stdev = sqrt(sumsq / SIZEA);
        printf("STDEV is:  %1d\n", stdev);
        B[i] = mean;
        C[i] = stdev;
        ++i;
    // outer loop end
    }
    int FREQUENCYMEAN[20] = { 0 };
    // bin mean values of sampling events
    for (i = 0; i < SIZEB; ++i) {
        if (B[i] > 0 && B[i] <= 5) {
            ++FREQUENCYMEAN[0];
        }
        else {
            if (B[i] > 5 && B[i] <= 10) {
                ++FREQUENCYMEAN[1];
            }
            else {
                if (B[i] > 10 && B[i] <= 15) {
                    ++FREQUENCYMEAN[2];
                }
                else {
                    if (B[i] > 15 && B[i] <= 20) {
                        ++FREQUENCYMEAN[3];
                    }
                    else {
                        if (B[i] > 20 && B[i] <= 25) {
                            ++FREQUENCYMEAN[4];
                        }
                        else {
                            if (B[i] > 25 && B[i] <= 30) {
                                ++FREQUENCYMEAN[5];
                            }
                            else {
                                if (B[i] > 30 && B[i] <= 35) {
                                    ++FREQUENCYMEAN[6];
                                }
                                else {
                                    if (B[i] > 35 && B[i] <= 40) {
                                        ++FREQUENCYMEAN[7];
                                    }
                                    else {
                                        if (B[i] > 40 && B[i] <= 45) {
                                            ++FREQUENCYMEAN[8];
                                        }
                                        else {
                                            if (B[i] > 45 && B[i] <= 50) {
                                                ++FREQUENCYMEAN[9];
                                            }
                                            else {
                                                if (B[i] > 50 && B[i] <= 55) {
                                                    ++FREQUENCYMEAN[10];
                                                }
                                                else {
                                                    if (B[i] > 55 && B[i] <= 60) {
                                                        ++FREQUENCYMEAN[11];
                                                    }
                                                    else {
                                                        if (B[i] > 60 && B[i] <= 65) {
                                                            ++FREQUENCYMEAN[12];
                                                        }
                                                        else {
                                                            if (B[i] > 65 && B[i] <= 70) {
                                                                ++FREQUENCYMEAN[13];
                                                            }
                                                            else {
                                                                if (B[i] > 70 && B[i] <= 75) {
                                                                    ++FREQUENCYMEAN[14];
                                                                }
                                                                else {
                                                                    if (B[i] > 75 && B[i] <= 80) {
                                                                        ++FREQUENCYMEAN[15];
                                                                    }
                                                                    else {
                                                                        if (B[i] > 80 && B[i] <= 85) {
                                                                            ++FREQUENCYMEAN[16];
                                                                        }
                                                                        else {
                                                                            if (B[i] > 85 && B[i] <= 90) {
                                                                                ++FREQUENCYMEAN[17];
                                                                            }
                                                                            else {
                                                                                if (B[i] > 90 && B[i] <= 95) {
                                                                                    ++FREQUENCYMEAN[18];
                                                                                }
                                                                                else {
                                                                                    if (B[i] > 95 && B[i] <= 100) {
                                                                                        ++FREQUENCYMEAN[19];
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    printf("\n");
    int sumE = 0;

    for (i = 0; i < SIZEB; ++i) {
        printf("Mean %1d: %1f\n", i, B[i]);
        sumE = sumE + B[i];
        printf("STDEV %1d: %1d\n", i, C[i]);
    }
    float expectvalue = (float) sumE / SIZEB;

    printf("\nExpect value of mean is: %1f", expectvalue);
    int sumsqE = 0;

    for (i = 0; i < SIZEB; ++i) {
        sumsqE = sumsqE + pow((B[i] - expectvalue), 2);
    }
    int SE;

    SE = sqrt(sumsqE / SIZEB);
    printf("\nSE of Mean is: %1d", SE);
    printf("\n");
    printf("\nFrequency of Expect value of mean:");
    printf("\n%5s%11s%11s", "Bin", "Frequency", "Histogram");
    for (size_t k = 0; k < 20; ++k) {
        printf("\n%4d%9d       ", k, FREQUENCYMEAN[k]);
        for (size_t n = 1; n <= FREQUENCYMEAN[k]; ++n) {
            printf("%c", '.');
        }
        puts("");
    }
}

Solution

  • You can find the bin a number resides in by dividing it by the size of the bin

    int bin = (A[j] - 1)/ 10;
    ++FREQUENCY[bin];
    
    int bin = (B[i] - 1) / 5;
    ++FREQUENCYMEAN[bin];