Rules to follow:
Get input in the button from the user for the new value - iNum := StrToInt(edtNum.text);
Add this value to a running total - iTotal := iTotal + iNum;
Count how many values you are adding - iCount := iCount + 1;
If a variable appears on both side of the := the variable, in the same line of code, it needs to be initialised. Remember inc(iCount) is the same as the code in number 3 and therefore needs to be initialised.
Where? At this stage grade 10 learners will initialise the variable in Form OnActivate. Once they know loops this variable will be initialised above the loop to allow the user to click on the same button twice and calculate a new average.
iTotal :=0;
iCount := 0;
If a variable is used in more than one event (Procedure for the Button and OnActivate in this instance) the variable needs to be declared as a global variable under Private.
private
iCount, iTotal : integer;
If a variable is declared as global you may NOT re-declare it as a local variable. You will lose the value of the global variable. This is a common mistake.
The average might be calculated in the same button or in another. The average variable will always be local as it is only used by one event. The average is always real because of the divide, unless the paper asks you to round it off. (See basic Rules)
rAve := iTotal / iCount;
Make sure learners understand the difference between an average and a percentage.