Skip to content

How to Calculate the Mean, Median, and Mode of a Set of Numbers in Batch Script

Learn how to compute mean (average), median (middle value), and mode (most frequent value) in Windows Batch scripting using summation, sorting, and frequency analysis.

In statistics, the Mean, Median, and Mode are the three principal measures of "Central Tendency", they tell you what a "typical" value in your dataset looks like. The Mean is the average, the Median is the middle value when sorted, and the Mode is the most frequently occurring value. Calculating all three in Batch requires a combination of summation, sorting, and frequency counting.

In this guide, we will demonstrate how to compute all three measures from an array of numbers.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Define the Dataset
set "size=7"
set "ARR_1=4"
set "ARR_2=7"
set "ARR_3=2"
set "ARR_4=7"
set "ARR_5=9"
set "ARR_6=4"
set "ARR_7=7"

echo Dataset: 4 7 2 7 9 4 7

:: =====================
:: MEAN (Average)
:: =====================
set "total=0"
for /L %%i in (1,1,%size%) do set /a "total+=!ARR_%%i!"
set /a "mean=total / size"
echo.
echo [MEAN]   Sum=!total!  Count=%size%  Mean=!mean!

:: =====================
:: MEDIAN (Middle Value after Sort)
:: =====================
:: Bubble Sort the array (copy to SORT_ to preserve original)
for /L %%i in (1,1,%size%) do set "SORT_%%i=!ARR_%%i!"

for /L %%p in (1,1,%size%) do (
    for /L %%j in (1,1,%size%) do (
        set /a "nxt=%%j + 1"
        if !nxt! LEQ %size% (
            for %%N in (!nxt!) do (
                if !SORT_%%j! GTR !SORT_%%N! (
                    set "tmp=!SORT_%%j!"
                    set "SORT_%%j=!SORT_%%N!"
                    set "SORT_%%N=!tmp!"
                )
            )
        )
    )
)

:: Find the middle index
set /a "midIdx=(size + 1) / 2"
set "median=!SORT_%midIdx%!"
echo [MEDIAN] Middle Index=!midIdx!  Median=!median!

:: =====================
:: MODE (Most Frequent Value)
:: =====================
set "maxFreq=0"
set "mode="

for /L %%i in (1,1,%size%) do (
    set "val=!SORT_%%i!"

    :: Skip if this value was already counted
    set "skipFlag=0"
    set /a "prevIdx=%%i - 1"
    if !prevIdx! GEQ 1 (
        for %%P in (!prevIdx!) do (
            if !SORT_%%P! EQU !val! set "skipFlag=1"
        )
    )

    if !skipFlag! EQU 0 (
        :: Count how many times this value appears
        set "freq=0"
        for /L %%j in (1,1,%size%) do (
            if !SORT_%%j! EQU !val! set /a "freq+=1"
        )
        if !freq! GTR !maxFreq! (
            set "maxFreq=!freq!"
            set "mode=!val!"
        )
    )
)

echo [MODE]   Most Frequent=!mode! (appears !maxFreq! times)

echo.
echo ==========================================
echo SUMMARY: Mean=!mean!, Median=!median!, Mode=!mode!
echo ==========================================
pause

Why Calculate Mean, Median, and Mode?

  1. Performance Analysis: The mean gives the "overall" picture, the median avoids distortion by outliers, and the mode shows the most common result.
  2. Resource Planning: If the mode of daily disk usage is "85%", you know that 85% is the most common state, even if the mean is lower.
  3. Quality Control: In manufacturing or testing scripts, these three measures help identify consistency vs. spikes.

Important Limitations

Conclusion

Calculating the Mean, Median, and Mode provides a comprehensive statistical summary of any numeric dataset. By combining summation, sorting, and frequency counting, you turn raw data into actionable intelligence. These three measures are the cornerstone of data analysis, enabling your Batch scripts to perform professional-grade statistical reporting directly from the command line.

Navigation