Determine the Mode of a Dataset in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to determine the mode of a dataset in C. The lab covers the following steps:

Read the Array of Numbers: You will learn how to read an array of numbers in C, which is the first crucial step in determining the mode of a dataset. The program allows input of a set of numbers and prepares them for frequency analysis.

Count Frequencies to Find Most Common Value: You will modify the previous program to count the frequencies of each number in the array and identify the most common value (mode).

Print the Mode: Finally, you will print the mode, which is the value that appears most frequently in the dataset.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/CompoundTypesGroup -.-> c/arrays("`Arrays`") c/UserInteractionGroup -.-> c/user_input("`User Input`") subgraph Lab Skills c/output -.-> lab-435172{{"`Determine the Mode of a Dataset in C`"}} c/operators -.-> lab-435172{{"`Determine the Mode of a Dataset in C`"}} c/for_loop -.-> lab-435172{{"`Determine the Mode of a Dataset in C`"}} c/arrays -.-> lab-435172{{"`Determine the Mode of a Dataset in C`"}} c/user_input -.-> lab-435172{{"`Determine the Mode of a Dataset in C`"}} end

Read the Array of Numbers

In this step, you'll learn how to read an array of numbers in C, which is the first crucial step in determining the mode of a dataset. We'll create a C program that allows input of a set of numbers and prepares them for frequency analysis.

First, let's create a new C file for our mode calculation program:

cd ~/project
nano mode_calculation.c

Now, add the following code to the file:

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int numbers[MAX_SIZE];
    int n, i;

    // Input the number of elements
    printf("Enter the number of elements (max %d): ", MAX_SIZE);
    scanf("%d", &n);

    // Input array elements
    printf("Enter %d integers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &numbers[i]);
    }

    // Print the entered array to verify input
    printf("Entered array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");

    return 0;
}

Compile and run the program:

gcc mode_calculation.c -o mode_calculation
./mode_calculation

Example output:

Enter the number of elements (max 100): 5
Enter 5 integers:
3 4 2 4 1
Entered array: 3 4 2 4 1

Let's break down the key parts of this code:

  1. #define MAX_SIZE 100 sets a maximum limit for the array to prevent overflow.
  2. scanf() is used to input the number of elements and the array values.
  3. We print the array to verify that the input was correctly captured.

The code demonstrates basic array input in C, which is essential for our mode calculation process. In the next steps, we'll build upon this to count frequencies and determine the mode.

Count Frequencies to Find Most Common Value

In this step, we'll modify our previous program to count the frequencies of each number in the array and identify the most common value (mode).

Open the existing file and update the code:

cd ~/project
nano mode_calculation.c

Replace the previous code with the following implementation:

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int numbers[MAX_SIZE];
    int frequencies[MAX_SIZE] = {0};
    int n, i, j, mode = 0, max_frequency = 0;

    // Input the number of elements
    printf("Enter the number of elements (max %d): ", MAX_SIZE);
    scanf("%d", &n);

    // Input array elements
    printf("Enter %d integers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &numbers[i]);
    }

    // Count frequencies of each number
    for (i = 0; i < n; i++) {
        int count = 1;
        for (j = 0; j < n; j++) {
            if (i != j && numbers[i] == numbers[j]) {
                count++;
            }
        }
        frequencies[i] = count;

        // Track the mode
        if (count > max_frequency) {
            max_frequency = count;
            mode = numbers[i];
        }
    }

    // Print frequencies
    printf("\nFrequencies:\n");
    for (i = 0; i < n; i++) {
        printf("Number %d appears %d time(s)\n", numbers[i], frequencies[i]);
    }

    // Print the mode
    printf("\nMode: %d (appears %d times)\n", mode, max_frequency);

    return 0;
}

Compile and run the program:

gcc mode_calculation.c -o mode_calculation
./mode_calculation

Example output:

Enter the number of elements (max 100): 6
Enter 6 integers:
2 3 4 2 2 5

Frequencies:
2 appears 3 time(s)
3 appears 1 time(s)
4 appears 1 time(s)
2 appears 3 time(s)
2 appears 3 time(s)
5 appears 1 time(s)

Mode: 2 (appears 3 times)

Key points in this implementation:

  1. We create a frequencies array to store the count of each number.
  2. Nested loops count the occurrences of each unique number.
  3. We track the mode by keeping track of the maximum frequency.
  4. The program handles cases with a single mode.

Print the Mode

In this final step, we'll enhance our mode calculation program to handle multiple modes and provide a more comprehensive output of statistical information.

Open the existing file and update the code:

cd ~/project
nano mode_calculation.c

Replace the previous code with the following implementation:

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int numbers[MAX_SIZE];
    int frequencies[MAX_SIZE] = {0};
    int unique_numbers[MAX_SIZE];
    int n, i, j, unique_count = 0, max_frequency = 0;

    // Input the number of elements
    printf("Enter the number of elements (max %d): ", MAX_SIZE);
    scanf("%d", &n);

    // Input array elements
    printf("Enter %d integers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &numbers[i]);
    }

    // Find unique numbers and their frequencies
    for (i = 0; i < n; i++) {
        int is_unique = 1;
        for (j = 0; j < unique_count; j++) {
            if (numbers[i] == unique_numbers[j]) {
                is_unique = 0;
                break;
            }
        }

        if (is_unique) {
            unique_numbers[unique_count] = numbers[i];

            // Count frequency for this unique number
            int count = 0;
            for (j = 0; j < n; j++) {
                if (numbers[i] == numbers[j]) {
                    count++;
                }
            }
            frequencies[unique_count] = count;

            // Update max frequency
            if (count > max_frequency) {
                max_frequency = count;
            }

            unique_count++;
        }
    }

    // Print detailed mode information
    printf("\nStatistical Analysis:\n");
    printf("Total Numbers: %d\n", n);
    printf("Unique Numbers: %d\n", unique_count);

    printf("\nFrequency Distribution:\n");
    for (i = 0; i < unique_count; i++) {
        printf("Number %d: %d time(s)\n",
               unique_numbers[i], frequencies[i]);
    }

    // Print modes
    printf("\nMode(s):\n");
    for (i = 0; i < unique_count; i++) {
        if (frequencies[i] == max_frequency) {
            printf("- %d (appears %d times)\n",
                   unique_numbers[i], frequencies[i]);
        }
    }

    return 0;
}

Compile and run the program:

gcc mode_calculation.c -o mode_calculation
./mode_calculation

Example output:

Enter the number of elements (max 100): 7
Enter 7 integers:
2 3 4 2 2 5 5

Statistical Analysis:
Total Numbers: 7
Unique Numbers: 4

Frequency Distribution:
Number 2: 3 time(s)
Number 3: 1 time(s)
Number 4: 1 time(s)
Number 5: 2 time(s)

Mode(s):
- 2 (appears 3 times)

Key improvements in this version:

  1. Handles multiple modes if they exist
  2. Provides a comprehensive statistical overview
  3. Identifies unique numbers in the dataset
  4. Displays frequency distribution

Summary

In this lab, you will learn how to read an array of numbers in C and count the frequencies of each number to determine the mode, which is the most common value in the dataset. First, you will create a C program that allows input of a set of numbers and prepares them for frequency analysis. Then, you will modify the program to count the frequencies of each number and identify the mode. Finally, you will print the mode to the console.

The key learning points from the completed steps are:

  1. How to read an array of numbers in C using the scanf() function.
  2. How to print the entered array to verify the input.
  3. How to count the frequencies of each number in the array to find the most common value (mode).

Other C Tutorials you may like