Find Maximum Algorithm

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
stevewhyte
Posts: 25
Joined: Mon Oct 18, 2010 6:32 pm

Find Maximum Algorithm

Post by stevewhyte » Thu Feb 17, 2011 3:09 pm

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:

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Find Maximum Algorithm

Post by dunbarx » Thu Feb 17, 2011 3:59 pm

Is the "max" function a bit easier?

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

Craig Newman

stevewhyte
Posts: 25
Joined: Mon Oct 18, 2010 6:32 pm

Re: Find Maximum Algorithm

Post by stevewhyte » Thu Feb 17, 2011 4:16 pm

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.

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Find Maximum Algorithm

Post by Klaus » Thu Feb 17, 2011 5:02 pm

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

stevewhyte
Posts: 25
Joined: Mon Oct 18, 2010 6:32 pm

Re: Find Maximum Algorithm

Post by stevewhyte » Thu Feb 17, 2011 5:16 pm

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 :)
Attachments
Student Marks.livecode.zip
Student Marks LiveCode file
(10.15 KiB) Downloaded 274 times

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Find Maximum Algorithm

Post by Klaus » Thu Feb 17, 2011 5:43 pm

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

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Find Maximum Algorithm

Post by BvG » Thu Feb 17, 2011 6:38 pm

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
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

Post Reply