Page 1 of 1

Find Maximum Algorithm

Posted: Thu Feb 17, 2011 3:09 pm
by stevewhyte
Hi,

I am trying to implement a simple algorithm in LiveCode. The algorithm is Find Maximum.

In COMAL it is successfully implemented as:

PROC highest_clock_speed (REF clock_speed (), REF product_names$ (), items%)
max% := 0
position% := 0
PRINT
PRINT
PRINT "Option 2...FIND FASTEST CARD"
PRINT "============================"
max% := 0
FOR loop% := 1 TO items% DO
IF clock_speed (loop%) > max% THEN
max% := clock_speed (loop%)
position% := loop%
ENDIF
NEXT loop%
PRINT "The card with the highest clock speed is the "; product_names$ (position%); " with a clock speed of "; clock_speed (position%); " MHz."
ENDPROC highest_clock_speed


-------

Here is what I have so far in LiveCode:

// allow access to global arrays and variables set up in main card.
global arrayPercentageMark
global arrayName
global MaxStudents

// set up local variables
local StudentCount
local MaxMark
local position

// zero number of counter variables
put 0 into StudentCount
put 0 into MaxMark
put 0 into position

// find the student with the highest mark
repeat with loop = 1 to MaxStudents
IF arrayPercentageMark[loop] > MaxMark then
add 1 to StudentCount
put MaxMark into arrayPercentageMark[loop]
put position into loop
End if
end repeat

put "The student with the highest percentage is: " & arrayName[position] into line 7 of field output

Doesn't seem to be working, probably because I have done something stupid!

Can anyone help?

Regards,

Steven :roll:

Re: Find Maximum Algorithm

Posted: Thu Feb 17, 2011 3:59 pm
by dunbarx
Is the "max" function a bit easier?

answer max(5,10,15,20,3,8) --returns 20

Craig Newman

Re: Find Maximum Algorithm

Posted: Thu Feb 17, 2011 4:16 pm
by stevewhyte
Thanks for getting back to me!

It would be a lot easier but my students need to code the algorithm. in a similar way to shown above.

Re: Find Maximum Algorithm

Posted: Thu Feb 17, 2011 5:02 pm
by Klaus
Hi Steve,

if they find out, your students will KILL you! But I'm sure you know that :D

Anyway, could you be a TAD more precise please?
"Doesn't seem to be working" is definitivley not too helpful here... 8)


Best

Klaus

Re: Find Maximum Algorithm

Posted: Thu Feb 17, 2011 5:16 pm
by stevewhyte
Hi Klaus,

Thanks for getting back to me. The students would love to use the maximum function (anything to get away with doing less work) but our exam board stipulates that they must code the algorithm.

Anyway, sorry for the lack of detail. Basically when I select the button Highest Percentage Mark" the answer does not appear in the output box - only the text "The student with the highest percentage is: "

I hope you don't mind but I have attached the livecode file (mac version). It might be easier to see the program?

Its got to be something simple. :roll:

Thanks for your kind help!

Steven :)

Re: Find Maximum Algorithm

Posted: Thu Feb 17, 2011 5:43 pm
by Klaus
Hi Steve,

downloaded the stack, clicked "Highest Percentage Mark" and also got NADA!
A deeper look showed me that the "global arrayname" is empty, looks like this is
not getting initialised anywhere.

Hint:
If you use a LOT of answer dialogs like in the repeat loop in "Get Student Details"
you should check if the user has clicked "Cancel" and you should leave the handler
in that case ;-)


Best

Klaus

Re: Find Maximum Algorithm

Posted: Thu Feb 17, 2011 6:38 pm
by BvG

Code: Select all

put 0 into theCurrent
repeat for each key theKey in theArray
  if theArray[theKey] > theCurrent then
    put theKey into theHighest
    put theArray[theKey] into theCurrent
  end if
end repeat
put theHighest