Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:15 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Highest value in an array

Author Message
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 07:35pm 20 Sep 2018
Copy link to clipboard 
Print this post

I can't seem to see the forest fore the trees moment...

I have a simple 10 element array Score(1) thru Score(10). Each element has a different number in it. I am trying to do two things, first find which element has the highest number in it and then which elements are the same as the highest number.

I have tried this...

For Zz = 1 to 9
If Score(Zz) > Score(Zz + 1) Then
WinTemp = Zz
Else
WinTemp = Zz + 1
End If
Next Zz
Print "High Score = ";Score(WinTemp)

Big fat fail. Any ideas on finding the high score and then which elements are tied with the high score?
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 08:36pm 20 Sep 2018
Copy link to clipboard 
Print this post

Ok played a bit more... found highest value, now to find ties... need to untie my brain a bit.


Dim Score(10) = (140, 13, 99, 2, 22, 210, 37, 20, 120, 40)
Max = 0
For Zz = 1 to 10
If Score(Zz) > Max Then
Max = Score(Zz)
MaxLoc = Zz
End If
Next Zz
Print "max = ";Max;" in location ";MaxLoc
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 10:32pm 20 Sep 2018
Copy link to clipboard 
Print this post

This is one way
  Quote   DIM Score(10) = (140, 13, 99, 2, 22, 210, 37, 20, 120, 40, 210)' needed extra value for location zero
Max = 0
Maxes =
0
FOR Zz = 1 TO 10
IF Score(Zz) > Max THEN
Max = Score(Zz)
'MaxLoc = Zz
END IF
NEXT Zz
PRINT "max = ";Max
FOR Zz = 1 TO 10
IF Max = Score(Zz) THEN
PRINT "In location ";Zz
Maxes = Maxes +
1
ENDIF
NEXT Zz
PRINT "Total sharing max = ";Maxes

I had to put an extra value in the array initialization line to fill location Score(0)
That would depend on what OPTION BASE you have set.

I often find that doing a 'sort' on the array lets you pull the various rankings out easily. It depends on what the goal is.

> RUN
max = 210
In location 5
In location 10
Total sharing max = 2
>


Jim
VK7JH
MMedit
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 05:36am 22 Sep 2018
Copy link to clipboard 
Print this post

Excellent, thank you TassyJim!
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025