You are now ready to start writing the code. To average a group of numbers, you need to sum the elements of the array (ad) and then divide the total by the number of elements (lSize).
1. Type the following statements in the procedure code area:
! handle optional parameter
! if -1 or not passed then use the array size
if lSize=-1
! calc num of elements
lSize=(sizeof ad)/sizeof double)
endif
if lSize<=0
return 0
endif
! sum all elements to d
for i=0 to lSize-1 do
d=d+ad[i]
next
! calc average
d=d/lSize
return d
The first two statements determines how many array elements to calculate the average (lSize), followed by a For-Next loop starting with 0 and ending with the last element you want to average (lSize-1). d is used to store the sum of all elements. The last statement returns the average (total divided by number of elements) to the caller.
2. To
verify the code was typed correctly and that you do not have syntax errors,
click on the Checkit! on the
Build/Run toolbar. If no errors
are found, the status bar, shown at the bottom of the main window, should
display No Errors. Otherwise,
the cursor will move to the place where the error occurred and the status
bar will show a description of the compiler error, in this case, fix the
error and repeat this until No errors
displays in the status bar.
The Procedure View should now look as illustrated in the figure below:
