On Jan 27, 2007, at 6:52 PM, Lennox Jacob wrote:

I would like to calculate the minimum, maximum and average of a set of editfields values.
I have 11 editfields - Editfield2 to Editfield12

The minimum value goes in an editfield named Min, The maximum value goes in an editfield named Max and the average value goes in an editfield named average.

So I have this code:
Average.Text = Str(Val(Days1.text) + (Val(Days2.text) + Val (Days3.text) + Val(Days4.text)+ Val(Days5.text) + Val(Days6.text)+ Val(Days7.text)+ Val(Days8.text)+ Val(Days9.text)+ Val(Days10.text) + Val(Days11.text))/11) Results.Text = "The minimum is “ + Min.text + “ days and the maximum is “ + Max.text + “ days, with an average of " + average.text + " days."

I can calculate average but I will need some help for minimum and maximum. I would like the app to find the minimum and maximum values that were inserted into the Editfield2 to Editfield12

Assuming all of your editfields were named "efDays" giving you a control array of 11 editfields, with indexes from 0 to 10, the following code would get you the average, minimum and maximum. Note that I have used different names for the other editfields for the reason given at the end of this reply.

theMin = 999999999 // assuming your numbers will always be less than this theMax = -999999999 // assuming your numbers will always be more than this
theAvg = 0
numVals = 0
For i = 0 to 10
  // a good program would verify that each editfield contains a valid
  // number, and, if it is valid to leave one or more empty then only
  // process the non-empty editfields
  if efDays(i) <> "" then // ignore blank editfields
    numVals = numVals + 1
    thisVal = val(efDays(i))
    theAvg = theAvg + thisVal
    if thisVal < theMin then theMin = thisVal
    if thisVal > theMax then theMax = thisVal
  end if
next i
if numVals > 0 then theAvg = theAvg/numVals
efMin.text = str(theMin)
efMax.text = str(theMax)
efAvg.text = str(theAvg)

Results.Text = "The minimum is “ + efMin.text + “ days and the maximum is “ _
    + efMax.text + “ days, with an average of " + efAvg.text + " days."


I have not dimmed any of these variables in the example since I do not know what kind of values you are working with, integers, doubles, or whatever.

One note...it bothers me that you are naming editfields the same as function names that exist within RB. From your original email it looks like you have editfields named min and max which are also the names of functions. This may not cause a problem but I sure do my best to avoid things like this and therefore usually use names like efMin and efMax. The ef for editfield followed by the name I desire. Looking at the code I know, due to my naming conventions, that these are referring to editfields because they start with ef. You certainly don't have to follow this convention but I would sure stay away from using the same name as functions or other internal items.


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to