View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
Dave O Dave O is offline
external usenet poster
 
Posts: 427
Default error in programme

I made some changes to your code, below. An apostrophe in a line
causes the compiler to disregard everything on that line after the
apostrophe, so I entered notes that way. First, I assumed your a(10)
should be an array of 100 entries, so I changed that; then I changed
the variables to include a capital letter. (That's a personal
preference, not required; it allows me to enter code in all lower case
letters. The compiler recognizes variables and capitalizes them the
way they are declared, so I see right away if there's a typo.) I
deleted the Average variable, since Sum / 100 is that value; next in
the code I populated the array- your code will need to find a way to
scoop up 100 entries to populate the array.

I deleted some rows ("Max = a(1)", for instance) because they can be
handled by the main body of the code, changed some of your logic as
noted, and provided message boxes as an output mechanism.

This code will work, assuming you will always have 100 entries to work
on; you might consider letting your input mechanism determine the
upper bound of your array. Let us know if you need help getting the
data into memory.

Dave O


Sub testing()
'max, min, avg of 100 entries
Dim A(1 To 100) As Single 'declare array
Dim Max As Single
Dim Min As Single
Dim Sum As Single
'deleted: Dim Average As Integer
Dim I As Integer

'added: values to populate the array
For I = 1 To 100
A(I) = I
Next I

'deleted: n = 100
'deleted: Max = a(1)
'deleted: Min = a(1)
'deleted: Sum = a(1)

For I = 1 To 100

'changed: If Max a(I) Then Max = a(I)
If A(I) Max Then Max = A(I)

'changed: If Min < a(I) Then
If A(I) < Min Then Min = A(I)
'deleted: End If

Sum = Sum + A(I)
Next I
'deleted: Average = Sum / n

'added:
MsgBox "Max value is " & Max
MsgBox "Min value is " & Min
MsgBox "The average is " & Sum / 100

End Sub