SET /a calctotal=0
for /l %%i in (1, 1, 100) do (
set /a result = %%i %% 5
set /a calctotal += result + 3
)
set /a average = %calctotal%/100
echo %average%
The problem to me is the interpretation of "calculate the sum via the arithmetic expression %%variable mod 5 + 3"
Now does this mean the sum of (%%variable mod 5 + 3) or (the sum of (%%variable) mod 5 + 3 ??
Anyhow, there are some problems with your code (well, if there wasn't, you'd not be asking, would you?)
First, in a 'block' (parenthesised series of lines) any %var% is replaced by the value of var at the time the block was encountered, and does not change as the block is executed. The classic way to deal with this is to invoke delayedexpansion.
Hence, in your code, you were executing set /a calctotal = + 3 100 times, as %result% would have been replaced by its value at the start of the loop (presumably undefined, hence nothing)
Next problem is that you are attempting to set calctotal to the latest value of result - you are not accumulating the values.
however in this case, you can use the set /a syntax, which works on the current value of the variables - provided you don't invoke them as %var%.
set /a calctotal += result + 3 uses += to accumulate the values. You could also use set /a calctotal = calctotal + result + 3 which may be more obvious.
I set calctotal to zero before starting just in case it's aready set.
And no doubt you could remove the need for result by using set /a calctotal += %%i %% 5 + 3
;are unnecessary for instance - and you actually don't need delayedexpansion (in this instance.)%calctotal%is being initialized inside of a code block; how do you not need delayed expansion?set /asyntax works on therun-timevalue, not theparse-timevalue...