Compute the Median of a Dataset in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the median of a dataset in C programming. The lab covers the following steps: reading and sorting an array of numbers, finding the middle element or the average of the two middle elements, and printing the median. The step-by-step instructions guide you through the process of implementing these tasks using C programming, including functions for reading, sorting, and printing the array. This lab provides a practical approach to understanding and applying statistical analysis techniques in C.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/CompoundTypesGroup -.-> c/arrays("`Arrays`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-435162{{"`Compute the Median of a Dataset in C`"}} c/for_loop -.-> lab-435162{{"`Compute the Median of a Dataset in C`"}} c/arrays -.-> lab-435162{{"`Compute the Median of a Dataset in C`"}} c/function_declaration -.-> lab-435162{{"`Compute the Median of a Dataset in C`"}} end

Read and Sort the Array

In this step, you will learn how to read an array of numbers and sort it in preparation for calculating the median. We'll use C programming to accomplish this task.

First, let's create a C file to implement array reading and sorting:

cd ~/project
nano median_calculator.c

Now, add the following code to the file:

#include <stdio.h>

#define MAX_SIZE 100

// Function to read array elements
void readArray(int arr[], int *n) {
    printf("Enter the number of elements (max %d): ", MAX_SIZE);
    scanf("%d", n);

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

// Function to sort array using bubble sort
void sortArray(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

// Function to print array
void printArray(int arr[], int n) {
    printf("Sorted array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[MAX_SIZE];
    int n;

    readArray(arr, &n);
    sortArray(arr, n);
    printArray(arr, n);

    return 0;
}

Compile and run the program:

gcc median_calculator.c -o median_calculator
./median_calculator

Example output:

Enter the number of elements (max 100): 5
Enter 5 elements:
42
15
7
23
11
Sorted array: 7 11 15 23 42

Let's break down the code:

  1. readArray() function allows user input for array elements
  2. sortArray() uses bubble sort algorithm to arrange elements in ascending order
  3. printArray() displays the sorted array
  4. The main() function ties these functions together

The bubble sort algorithm has a time complexity of O(nÂē), which is simple to understand but not the most efficient for large datasets.

Find the Middle Element or Average of Two Middles

In this step, you will extend the previous program to calculate the median by finding the middle element or the average of two middle elements in a sorted array.

Open the existing file and modify the code:

cd ~/project
nano median_calculator.c

Update the code with a new function to calculate the median:

#include <stdio.h>

#define MAX_SIZE 100

// Previous functions (readArray, sortArray, printArray) remain the same

// New function to calculate median
float calculateMedian(int arr[], int n) {
    // If number of elements is odd, return middle element
    if (n % 2 != 0) {
        return arr[n / 2];
    }

    // If number of elements is even, return average of two middle elements
    int mid1 = arr[(n / 2) - 1];
    int mid2 = arr[n / 2];
    return (mid1 + mid2) / 2.0;
}

int main() {
    int arr[MAX_SIZE];
    int n;

    readArray(arr, &n);
    sortArray(arr, n);
    printArray(arr, n);

    // Calculate and print median
    float median = calculateMedian(arr, n);
    printf("Median: %.2f\n", median);

    return 0;
}

Compile and run the updated program:

gcc median_calculator.c -o median_calculator
./median_calculator

Example output for odd number of elements:

Enter the number of elements (max 100): 5
Enter 5 elements:
42
15
7
23
11
Sorted array: 7 11 15 23 42
Median: 15.00

Example output for even number of elements:

Enter the number of elements (max 100): 6
Enter 6 elements:
42
15
7
23
11
8
Sorted array: 7 8 11 15 23 42
Median: 13.00

Key points about median calculation:

  1. For odd number of elements, median is the middle element
  2. For even number of elements, median is the average of two middle elements
  3. The array must be sorted before calculating the median

The calculateMedian() function handles both cases:

  • Uses integer division to find middle index
  • Checks if number of elements is odd or even
  • Returns appropriate median value

Print the Median

In this final step, you will enhance the median calculation program to provide more detailed output and demonstrate different ways of presenting the median.

Open the existing file to make final modifications:

cd ~/project
nano median_calculator.c

Update the code with improved output formatting:

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

#define MAX_SIZE 100

// Previous functions (readArray, sortArray, printArray, calculateMedian) remain the same

void printDetailedMedianInfo(int arr[], int n, float median) {
    printf("\n--- Median Calculation Details ---\n");
    printf("Total number of elements: %d\n", n);
    printf("Array elements: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n\nMedian Calculation:\n");

    if (n % 2 != 0) {
        printf("Odd number of elements\n");
        printf("Middle index: %d\n", n / 2);
        printf("Middle element: %d\n", arr[n / 2]);
    } else {
        printf("Even number of elements\n");
        printf("Two middle indices: %d and %d\n", (n / 2) - 1, n / 2);
        printf("Middle elements: %d and %d\n", arr[(n / 2) - 1], arr[n / 2]);
    }

    printf("\nFinal Median: %.2f\n", median);
}

int main() {
    int arr[MAX_SIZE];
    int n;

    readArray(arr, &n);
    sortArray(arr, n);

    float median = calculateMedian(arr, n);

    // Print sorted array
    printArray(arr, n);

    // Print detailed median information
    printDetailedMedianInfo(arr, n, median);

    return 0;
}

Compile and run the updated program:

gcc median_calculator.c -o median_calculator
./median_calculator

Example output for odd number of elements:

Enter the number of elements (max 100): 5
Enter 5 elements:
42
15
7
23
11
Sorted array: 7 11 15 23 42

--- Median Calculation Details ---
Total number of elements: 5
Array elements: 7 11 15 23 42

Median Calculation:
Odd number of elements
Middle index: 2
Middle element: 15

Final Median: 15.00

Example output for even number of elements:

Enter the number of elements (max 100): 6
Enter 6 elements:
42
15
7
23
11
8
Sorted array: 7 8 11 15 23 42

--- Median Calculation Details ---
Total number of elements: 6
Array elements: 7 8 11 15 23 42

Median Calculation:
Even number of elements
Two middle indices: 2 and 3
Middle elements: 11 and 15

Final Median: 13.00

Key improvements in this step:

  1. Added a new function printDetailedMedianInfo()
  2. Provides comprehensive information about median calculation
  3. Shows different scenarios for odd and even number of elements
  4. Enhances user understanding of median computation

Summary

In this lab, you learned how to read an array of numbers, sort them using the bubble sort algorithm, and prepare the data for calculating the median. The sorted array is then printed to the console, demonstrating the successful completion of the first step. The bubble sort algorithm, while simple to understand, has a time complexity of O(nÂē), which may not be the most efficient for large datasets. The subsequent steps will focus on finding the median of the sorted array.

The next step involves identifying the middle element or the average of the two middle elements, depending on the size of the array. This step will ensure that the median is accurately computed and presented to the user.

Other C Tutorials you may like