How to Calculate the Mean, Median, and Mode of a Set of Numbers in Batch Script
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
The outer loop runs size passes over the array. On each pass, adjacent elements are compared and swapped if they are out of order. After each complete pass, the largest unsorted element "bubbles" to its correct position. After size passes, the entire array is sorted in ascending order.
Why Calculate Mean, Median, and Mode?
- Performance Analysis: The mean gives the "overall" picture, the median avoids distortion by outliers, and the mode shows the most common result.
- 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.
- Quality Control: In manufacturing or testing scripts, these three measures help identify consistency vs. spikes.
Important Limitations
The mean is calculated using integer division, so decimals are truncated. A dataset summing to 33 with 7 items produces a mean of 4, not 4.71. For decimal precision, use PowerShell:
powershell -NoProfile -Command "33 / 7"
For an even number of items, the true median is the average of the two middle values. This script takes the lower-middle value for simplicity. To compute the true median for even-sized datasets, average positions size/2 and size/2 + 1.
If two or more values appear with equal highest frequency (bimodal or multimodal), this script only reports the first one encountered in the sorted array. A full implementation would need to collect all values sharing the maximum frequency.
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.