This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-09-26 18:26:34

arox147
New Scratcher
Registered: 2012-09-26
Posts: 1

max in a list

I'm not sure how to get my program to display the position of the largest number in a list. I can get it to say the largest number but i can't get it to say the position of it. Any help would be greatly appreciated!

Offline

 

#2 2012-09-26 18:38:16

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: max in a list

Here's the code I would use to do what you want:

set [# v] to [-999999]
set [position v] to [1]
set [count v] to [1]
repeat (length of [list v])
 if <(item (count) of [list v]) > (#)>
  set [# v] to (item (count) of [list v])
  set [position v] to (count)
 end
 change [count v] by (1)
end

Last edited by ErnieParke (2012-09-26 18:39:21)


http://i46.tinypic.com/35ismmc.png

Offline

 

#3 2012-09-27 07:59:28

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: max in a list

Or this:

set [position v] to (1)
set [counter v] to (1)
repeat((length of [list v])-(1))
change [counter v] by (1)
if<(item (counter) of [list v]) > (item (position) of [list v])>
set [position v] to (counter)
end
end
set [highest value v] to (item (position) of [list v])


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#4 2012-09-27 15:11:15

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: max in a list

MoreGamesNow wrote:

Or this:

set [position v] to (1)
set [counter v] to (1)
repeat(length of [list v])
if<(item (counter) of [list v]) > (item (position) of [list v])>
set [position v] to (counter)
end
change [counter v] by (1)
end
set [highest value v] to (item (position) of [list v])

Your code could be simplified a bit, which I did above. Other than that, I had never thought of that.

Last edited by ErnieParke (2012-09-27 15:11:43)


http://i46.tinypic.com/35ismmc.png

Offline

 

Board footer