|
Post by honky on Apr 30, 2023 15:15:06 GMT
Good afternoon, How to keep the last value of a "for ... next" loop For x = 1 to 10 I want to recover "10" at the outlet. thank you for in this case: FOR i=1 TO n FOR j=1 TO (n-1) IF bubble(j) > bubble(j+1) THEN temp=bubble(j) bubble(j)=bubble(j+1) bubble(j+1)=temp END IF NEXT j NEXT i
I should want récuper the bigger value (the last value) [/code] [/div]
|
|
|
Post by tsh73 on Apr 30, 2023 16:17:35 GMT
Why you want to do that?
Where's the problem? If you know n you could use it right away
If FOR ends normally, loop index would be (max value +1) You can subtract one if you wish.
If it ended with EXIT FOR index retain last value it had.
|
|
|
Post by honky on Apr 30, 2023 16:27:57 GMT
The code I posted sorting well. But when I do: "Print Bubble(i)" after the "next i". It gives me the second value from a list of 6, and not the last. And "print i" gives: 2
|
|
|
Post by tsh73 on Apr 30, 2023 16:35:58 GMT
It's just the way FOR loop works. To get last item, do "Print Bubble(i-1)" after the "next i".
|
|
|
Post by honky on Apr 30, 2023 17:02:23 GMT
Before sorting, there is that: [go] dim bubble(1000),bubble$(1000) for x= 1 to n a=a+1: bubble(a)=val(word$(texte$(x),4)): bubble$(a)=texte$(x) if word$(texte$(x),4)="0" then exit for print word$(texte$(x),4)'*********** It's good next x print "------------------"'**************** gosub [tri] wait
The total list contains 418 items. I sort it out on the first 6. The 6 come out well sorted If I do n = 20; 50; 100 ... it gives me the second value But if I do n = 418 it gives me the last. What should be understood?
|
|
|
Post by tsh73 on Apr 30, 2023 18:16:28 GMT
sorry could not understand that
What code prints it? It is not in code you posted.
So please put example what behaves wrong (with data if that's needed)
This code (from the 1st post) surely prints 11 for me.
n=10
FOR i=1 TO n FOR j=1 TO (n-1) IF bubble(j) > bubble(j+1) THEN temp=bubble(j) bubble(j)=bubble(j+1) bubble(j+1)=temp END IF NEXT j NEXT i
print i
|
|
|
Post by plus on Apr 30, 2023 20:49:27 GMT
dim bubble(10) for i = 1 to 10 bubble(i) = int(rnd(0)*100) + 1 print bubble(i);" "; next print n = 10 FOR i=1 TO n ' the honky way FOR j=1 TO (n-1) count = count + 1 IF bubble(j) > bubble(j+1) THEN temp=bubble(j) bubble(j)=bubble(j+1) bubble(j+1)=temp END IF NEXT j NEXT i
for i = 1 to n print bubble(i);" "; next print print "steps = ";count
This goes faster:
dim bubble(10) for i = 1 to 10 bubble(i) = int(rnd(0)*100) + 1 print bubble(i);" "; next print n = 10 FOR i=1 TO n-1 FOR j=i+1 TO n count = count + 1 IF bubble(i) > bubble(j) THEN temp=bubble(i) bubble(i)=bubble(j) bubble(j)=temp END IF NEXT j NEXT i
for i = 1 to n print bubble(i);" "; next print print "steps = ";count
Half as many steps for any n!
|
|
|
Post by honky on May 1, 2023 9:26:51 GMT
The full case: A array of 418 items of 4 words each. The items are in groups of unequal lengths, separated by zeros. The goal: to extract from these groups the maximum values of the 4th word (the last of sorting list) I was dealing with the first group when I came across the problem: The first code posted is the type of sorting used, which works well. The second is the extraction of the value of the 4th word and the stop at the first zero encountered (end of the first group). The test: "Print Bubble (I-1)" after "next i" Attempts: For i = 1 to a ---> Result-> 0 For i = 1 to a-1 ---> Result-> 0 So I counted the group ---> 6 items For i = 1 to 6 ---> Result-> 0 For i = 1 to 7, 20, 50 ... ect ---> Result-> 0 For i = 1 to 418 ----> Well !!!! (the last value, maximum value 1098595.4) Conclusion: To have the maximum value of the 4th word in the first group (6 items), we must sort the whole list. It's extremely bizarre.
EDIT: The last 4th word of the 418 items is: 638646.72
|
|
|
Post by plus on May 1, 2023 15:08:33 GMT
|
|
|
Post by honky on May 1, 2023 15:26:03 GMT
I have just verified that the maximum value of the whole list was by chance that of the first group: 1098595.4 (which would explain the phenomenon). But no, the maximum value of the list is in the 15th group 1716580.36 The mystery remains It seems that sorting the whole list stops when it meets the sequence of zeros (9 zeros)
|
|
|
Post by plus on May 1, 2023 16:37:28 GMT
If you are just looking for max over all, why waste time sorting?
And or why not find max for each group if you want that? again no sorting needed!
|
|
|
Post by tsh73 on May 1, 2023 16:47:33 GMT
(++) !!
I got same idea *after* I coded something which included sorting (and JB includes build-in SORT command!)
anyway something I got
Source data item1 has some 672 item2 has some 67 item3 has some 434 item4 has some 342 item5 has some 77 item6 has some 305 item7 has some 0 item8 has some 469 item9 has some 974 item10 has some 512 item11 has some 0 item12 has some 907 item13 has some 870 item14 has some 324 item15 has some 454 item16 has some 175 item17 has some 59 item18 has some 923 item19 has some 91 item20 has some 0
processing groups 672 67 434 342 77 305 got end of group #1 at 7 ------------------ 67 77 305 342 434 672 The biggest in a group is 672 469 974 512 got end of group #2 at 11 ------------------ 469 512 974 The biggest in a group is 974 907 870 324 454 175 59 923 91 got end of group #3 at 20 ------------------ 59 91 175 324 454 870 907 923 The biggest in a group is 923 =============================== Processing done There 3 groups in data.
' The full case: ' A array of 418 items of 4 words each. ' The items are in groups of unequal lengths, separated by zeros. ' The goal: to extract from these groups the maximum values of the 4th word (the last of sorting list)
n=20 'I'm too lazy for 418 'first froup is 6 'Questions: ' does last group has 0? ' I suppose YES for make it work the same ' does end of group used in sorting? ' I suppose it is COMPLETELY ignored
'arrays should be dimmed first so I have somewhere to READ data in dim texte$(1000)
print "Source data" for i = 1 to n read tmp$ texte$(i)=tmp$ print texte$(i) next
print print "processing groups"
groupStart=1 groupNum=0 [go] dim bubble(1000),bubble$(1000) 'btw it CLEARS array each time it cames here. 'for x= 1 to n 'let's attempt toi move to new group for x= groupStart to n a=a+1: bubble(a)=val(word$(texte$(x),4)): bubble$(a)=texte$(x) if word$(texte$(x),4)="0" then exit for print word$(texte$(x),4)'*********** It's good next x groupNum=groupNum+1 print "got end of group #";groupNum;" at ";x print "------------------"'**************** 'suppose we sort group. Group is from groupStart to groupEnd. '"x" is next lene, so groupEnd = x-1 gosub [tri] 'print sorted for k = groupStart to groupEnd print bubble(k); " " ; next print 'looks OK so far 'print "The biggest in a group is ";bubble(i-1) 'sorry has no sence with my sorting. It looks like with yours, too 'just use groupEnd print "The biggest in a group is ";bubble (groupEnd)
if x<n then groupStart=x+1: goto [go] 'for the nexrt group
print "===============================" print "Processing done" print "There ";groupNum;" groups in data."
wait
[tri] 'modify fo sort from groupStart to groupEnd 'actually JB now has SORT command, will try that later FOR i=groupEnd-1 to groupStart step -1 'data pairs to check FOR j=1 TO i 'data items, (j) (j+1) IF bubble(j) > bubble(j+1) THEN temp=bubble(j) bubble(j)=bubble(j+1) bubble(j+1)=temp END IF NEXT j NEXT i return
'--------------------------------------- data "item1 has some 672" data "item2 has some 67" data "item3 has some 434" data "item4 has some 342" data "item5 has some 77" data "item6 has some 305" data "item7 has some 0" data "item8 has some 469" data "item9 has some 974" data "item10 has some 512" data "item11 has some 0" data "item12 has some 907" data "item13 has some 870" data "item14 has some 324" data "item15 has some 454" data "item16 has some 175" data "item17 has some 59" data "item18 has some 923" data "item19 has some 91" data "item20 has some 0"
|
|
|
Post by honky on May 1, 2023 17:13:05 GMT
At: tsh73: In your code, the groups are loaded separately. In my case the groups are in the same (one) list separated by zero sequences (3, 9, 5, 11 zeros ...). We should be able to replace the zeros sequences with a single zero by sequences.
|
|
|
Post by tsh73 on May 1, 2023 17:50:30 GMT
This one reads your file and skips all zeros
BTW are you aware that val("1098595,4") is 1098595? To get 1098595.4 you should replace comma with dot first.
' The full case: ' A array of 418 items of 4 words each. ' The items are in groups of unequal lengths, separated by zeros. ' The goal: to extract from these groups the maximum values of the 4th word (the last of sorting list)
n=20 'I'm too lazy for 418 'first froup is 6 'Questions: ' does last group has 0? ' I suppose YES for make it work the same ' does end of group used in sorting? ' I suppose it is COMPLETELY ignored
'arrays should be dimmed first so I have somewhere to READ data in dim texte$(1000) dim bubble(1000),bubble$(1000)
print "Source data" goto [readFile] for i = 1 to n read tmp$ texte$(i)=tmp$ print texte$(i) next
[readFile] n=418 open "texte.txt" for input as #1 for i = 1 to n line input #1, tmp$ texte$(i)=tmp$ print texte$(i) next close #1
print print "processing groups"
groupStart=1 groupNum=0
x=1
'skip till start of a group while word$(texte$(x),4)="0" x=x+1 if x>n then exit while wend if x>n then [over]
groupStart=x print "start 0 ";groupStart
while x<n 'read group while word$(texte$(x),4)<>"0" 'a=a+1: bubble(a)=val(word$(texte$(x),4)): bubble$(a)=texte$(x) bubble(x)=val(word$(texte$(x),4)): bubble$(x)=texte$(x) print word$(texte$(x),4) x=x+1 if x>n then exit while wend if x>n then [over] groupNum=groupNum+1 print "got end of group #";groupNum;" at ";x print "------------------"'****************
'suppose we sort group. Group is from groupStart to groupEnd. '"x" is next line, so groupEnd = x-1 SORT bubble(),groupStart,groupEnd
'print sorted for k = groupStart to groupEnd print bubble(k); " " ; next print 'looks OK so far print "The biggest in a group is ";bubble (groupEnd)
'skip till start of a group while word$(texte$(x),4)="0" x=x+1 if x>n then exit while wend if x>n then [over] groupStart=x print "start 1 ";groupStart
wend
[over] print print "===============================" print "Processing done" print "There ";groupNum;" groups in data."
wait
'--------------------------------------- data "item1 has some 672" data "item2 has some 67" data "item3 has some 434" data "item4 has some 342" data "item5 has some 77" data "item6 has some 305" data "item7 has some 0" data "item8 has some 469" data "item9 has some 974" data "item10 has some 512" data "item11 has some 0" data "item12 has some 0" data "item13 has some 0" data "item14 has some 324" data "item15 has some 454" data "item16 has some 175" data "item17 has some 59" data "item18 has some 923" data "item19 has some 0" data "item20 has some 0"
|
|
|
Post by plus on May 1, 2023 18:42:25 GMT
Ah! from texte.txt sample first 19 lines of data
2 4134 553809,71 109294,4 3 4134 553809,71 261088,5 4 4134 553809,71 596546,55 5 4134 553809,71 765153,09 6 4134 553809,71 945645,88 7 4134 553809,71 1098595,4 8 269 22636,57 0 9 836 88250,65 0 10 1412 142086,2 0 11 2040 201485,57 0 12 3172 310327,05 0 13 3682 410080,51 0 14 4263 509798,61 0 15 4281 511128,92 0 16 4281 511128,92 7231,98 17 4281 511128,92 54029,89 18 4281 511128,92 148626,71 19 246 34372,23 0
tsh73 is right! those commas need to be replaced with dots AND the data is separated by spaces not zeros. (Better to use spaces IMHO!)
It looks like the first column is just an record index and it starts at 2 not 1, is something missing?
|
|