C Program to Calculate the Deciles

In this article, we will learn how to calculate the deciles and implement it into C programming. Decile is a mathematical study in statistics. Where it indicates the measure of central tendency at a specific location or position in an ungrouped and sorted data set.

Finding deciles are calculating one kind of average value of an ungrouped data set. The term deciles come from decimal. Because in the deciles method a data set is divided into ten equals part that makes 9 breakpoints in the data set. Which indicates the position of data.

Deciles are labeled as D1, D2, D3, … D9, and each decile holds 10%, 20%, 30%, … 90% of data distributions.

deciles measure of position

The following formulas are used to calculate deciles:

Deciles formula

Where;

  • n = the number of observations or the number of elements in the data set.
  • D1 = first deciles or 10% of distributions
  • D2 = second deciles or 20% of distributions
  • D3 = third deciles or 30% of distributions
  • … D9 = 9th decile or 90% of distributions

Algorithm – Calculate the Deciles

  1. Declare an extended size of the array arr[100] to store the data set and declare the required variables.
  2. Ask the user to input elements up to n.
  3. After getting the unsorted data, sort the data set using any sorting algorithm (Bubble sort used here).
  4. Process the raw data and prepare to use it in the deciles formula using necessary business logic.
  5. Apply the decile formula for finding each decile using a loop.

Program – Calculate the Deciles

#include <stdio.h>
int main() {
    int i, n, j, tmp, arr[100], r[9];
    double d[9];

    printf("Number of data in the data set: ");
    scanf("%d", &n);

    printf("Enter %d elements: ", n);
    for(i=0; i<n; i++) {
        scanf("%d", &arr[i]);
    }

    //using bubble sort
    for(i=0; i<n; i++) {
        for(j=0; j<n-i-1; j++) {
            if(arr[j] > arr[j+1]) {
                tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tmp;
            }
        }
    }

    printf("\nSorted data: ");
    for(i=0; i<n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    for(i=1; i<=9; i++) {
        d[i] = (n*i)/10.0;
        r[i] = (n*i)/10;
    }

    for(i=1; i<=9; i++) {
        if(d[i]-r[i] == 0) {
            printf("%dth Decile(D%d): %d", i, i, arr[r[i]-1]);
        } else {
            float d1 = arr[r[i] - 1] + (d[i] - r[i])*(arr[r[i]]-arr[r[i]-1]);
            printf("%dth Decile(D%d): %.2f", i, i, d1);
        }
        printf("\n");
    }
    return 0;
}
Sample InputSample Output
Number of data in the data set: 7
Enter 7 elements: 10 40 60 37 42 82 76
Sorted data: 10 37 40 42 60 76 82
1th Decile(D1): 322618016.00
2th Decile(D2): 20.80
3th Decile(D3): 37.30
4th Decile(D4): 39.40
5th Decile(D5): 41.00
6th Decile(D6): 45.60
7th Decile(D7): 58.20
8th Decile(D8): 69.60
9th Decile(D9): 77.80

NOTE: Deciles can be applied only to the ungrouped data set. Data set must be sorted before calculations

Happy coding 🙂

Posted in