Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Algorithm
Declare count As Integer
Control
Declare num As Integer
Declare total As Integer
Declare avg As Real
count = 0
total = 0
num = 90
avg = Process AvgSoFar( total, num )
num = 95
avg = Process AvgSoFar( total, num )
Print “num = ” + num
Print “total = ” + total
Print “count = ” + count
Print “avg = ” + avg
End
AvgSoFar( sum As Integer&,
val As Integer )As Real
Declare answer As Real
count = count + 1
val = val + 5
sum = sum + val
answer = sum / count
Return answerOK, I understand which are global and local variables and the call-by-value and call-by-reference for the AvgSoFar module. What I don't understand is how to work the num variable so that the total and num (for Process AvgSoFar) are inserted for val and sum and how the answer returns. It is my understanding the answer returns to avg value. However, I cannot make sense of the total being the value for sum when the original total value is 0 ... not to mention I haven't a clue about what is counted. Can anyone explain the workings of this and supply the variable values with explanation?
I'm learning.

You have:
count = 0
total = 0
num = 90
avg = Process AvgSoFar( total, num )
which is the same as:
avg = Process AvgSoFar( 0, 90 )the function AvgSoFar takes 0 as sum and 90 as val therefore, sum now = 0 and val now = 90.
add 1 to count
add 5 to val
add val to sumcount = 1
val = 95
sum = 95answer = (sum / count) = (95/1) = 95
avg = answer (answer is the result of the function)
Then the function is called again
Hope this helps
--Mike

I thought I was doing it right. It just made no sense to me to not return and continue recalculating. I suspect I was trying to make this simple bit far more complicated, LOL. Thank you for being my second brain!
I'm learning.

![]() |
MSVC++ 6.0 Compiler
|
help file
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |