ZeroCool
ZeroCool

Reputation: 477

Sorting input numbers in batch file

Im trying to create a program in which you will enter 5 numbers randomly and will automatically shows the sorted entered numbers from lowest to highest. Here is my code.

@echo off

:main

    set /p num1="Enter 1st :"
    set /p num2="Enter 2nd :"
    set /p num3="Enter 3rd :"
    set /p num4="Enter 4th :"
    set /p num5="Enter 5th :"
    set /a high=0
    set /a low=0


    set /a ave=(num1+num2+num3+num4+num5)/5

    if %num1% GTR %num2% (set /a high=%num1%) ELSE set /a high =%num2%
    if %num3% GTR %high% (set /a high=%num3%) 
    if %num4% GTR %high% (set /a high=%num4%) 
    if %num5% GTR %high% (set /a high=%num5%) 

    if %num1% LSS %num2% (set /a low=%num1%) ELSE set /a low =%num2%
    if %num3% LSS %low% (set /a low=%num3%) 
    if %num4% LSS %low% (set /a low=%num4%) 
    if %num5% LSS %low% (set /a low=%num5%) 


    ECHO.
    ECHO Average: %ave%
    ECHO Highest: %high%
    ECHO Lowest: %low%
    ECHO Input Numbers: %num1%    %num2%    %num3%    %num4%    %num5%

:end

set /p return="Continue?"

    if "%return%"=="yes" GOTO main 
    GOTO exit   

:exit   

Upvotes: 0

Views: 3602

Answers (2)

sambul35
sambul35

Reputation: 1098

I was looking for an answer to a similar "sort numbers" question, and want to add example of using SORT command as an alternative for completeness. Note that using SORT requires writing a file to disk and seems less efficient:

@echo off
setlocal EnableDelayedExpansion
set "num_1=2" & set "num_2=5" & set "num_3=1" & set "num_4=18" & set "num_5=10"

for /f "tokens=2 delims==" %%i in ('set num_') do (set /a "var=1000000+%%i"
    echo !var! >> nums.txt)
for /f %%j in ('sort nums.txt') do (set /a "var=%%j-1000000"
    set "nums=!nums! !var!")
del /q nums.txt & echo !nums!

:eol

Upvotes: 0

Aacini
Aacini

Reputation: 67326

There are several ways to solve this problem. In the "one by one" method each variable must be compared vs. another one; this method is too much complex and is the one you used to read the values. You may also use sort command to sort the numbers (as Rafael tried in his answer); however, the numbers must be padded at left side with zeros in order to be correctly sorted.

When a certain process is repeated over several elements, the usual way to solve such a problem is via an array. You may find descriptions about array concept in several sites, like in Wikipedia. You may read how to use arrays in a Batch file at this post.

The Batch file below make good use of the fact that environment variables are kept automatically sorted; this way, the program just insert the numbers in an array and then take out its elements, so the sort process itself is not required:

@echo off
setlocal EnableDelayedExpansion

set N=5

:main

    rem Delete array elements from previous cycle, if any:
    for /F "delims==" %%a in ('set array[ 2^>NUL') do set "%%a="

    rem Read the numbers; at same time, accumulate they in "ave" variable
    rem and create the array with the proper index (with left zeros)
    set ave=0
    for /L %%i in (1,1,%N%) do (
        set /P num="Enter number %%i :"
        set /A ave+=num
        set index=000000000!num!
        set array[!index:~-10!]=!num!
    )

    set /A ave=ave/N

    rem Assemble the output line and get low and high values
    set "output="
    set "low="
    for /F "tokens=2 delims==" %%a in ('set array[') do (
        if not defined low set low=%%a
        set high=%%a
        set output=!output!   %%a
    )

    ECHO/
    ECHO Average: %ave%
    ECHO Highest: %high%
    ECHO Lowest: %low%
    ECHO Input Numbers: %output%

    set /p return="Continue?"

if "%return%"=="yes" GOTO main 

Upvotes: 3

Related Questions