|
Post by honky on Nov 7, 2024 12:35:42 GMT
Hello, Put this code in a Folder. The code generates N files (here: 4) Of x datas (here: 10) The challenge consists in extracting the minimum and the maximum of the set of 4 files Only one mini, one maxi. Sub authorized I try do it without sub, it's a real hassle
dim array(20) for x=1 to 4 for y=1 to 10 array(y)=int(rnd(1)*20) + 1 next y open "test";str$(x);".txt" for output as #f 'Ouverture/création fichier en écriture for u=1 to 10 print #f, array(u) next u close #f next x
And sort code (for memory)
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
Thank. you for
|
|
|
Post by plus on Nov 7, 2024 17:31:26 GMT
Sorry honky this seems too easy to write up in JB code. The only challenge is what to dim the array to hold everything before looking into the files. Could just pre-count file lines and suppose each contains 1 element. If you know ahead of time there is just x files with exactly y elements in each then dim MyArray(x*y) easy peasy. And why not use built in sort JB provides instead of that awlful slow Bubble Sort? If this seems hard to you, then by all means practice, practice practice! b = b + ...
|
|
|
Post by honky on Nov 7, 2024 22:22:08 GMT
I just have to wait for this interest someone who will pass by here. With a little luck and some skull pain, I should get there. But what wasted time.
|
|
|
Post by plus on Nov 8, 2024 0:02:52 GMT
Honky instead of #f try a real number not 0
dim array(20) ' why 20 why not 10? for x=1 to 4 for y=1 to 10 array(y)=int(rnd(1)*20) + 1 ' for numbers 1 to 20 next y open "test";str$(x);".txt" for output as #1 'Ouverture/création fichier en écriture for u=1 to 10 print #1, array(u) next u close #1 next x print "files ready!"
b = b + ...
|
|
|
Post by plus on Nov 8, 2024 0:12:57 GMT
That's funny JB doesn't have FreeFile Function? usually it is f = FreeFile
Also Open in Wiki never says explicitly that #f has to be a non zero number.
b = b + ...
|
|
|
Post by plus on Nov 8, 2024 0:35:14 GMT
In for a penny, in for a pound...
Part II
dim array(40) index = 1 for file = 1 to 4 open "test";str$(file);".txt" for input as #1 for element = 1 to 10 input #1, fline$ array(index) = val(trim$(fline$)) index = index + 1 next element close #1 next file print "files loaded!" 'check load Print "Unsorted File data:" for index = 1 to 40 print array(index);" "; if index mod 10 = 0 then print next sort array() 1, 40 Print: print "Sorted File data:" for index = 1 to 40 print array(index);" "; if index mod 10 = 0 then print next print " and there we are!"
b = b + ...
|
|
|
Post by Rod on Nov 8, 2024 8:24:23 GMT
For the task as described you don’t need the array, just write the rnd() no to the file, you might also remember the highest and lowest number written. No need for sorting. So no need to read back in the file, in fact no need for the files at all. Or am I missing something in the task?
|
|
|
Post by honky on Nov 8, 2024 9:35:25 GMT
@: "plus": thank you very much "plus". For this material to be studied.
@: ROD "files" because the problem concerns existing files.
|
|
|
Post by honky on Nov 8, 2024 9:48:40 GMT
Follow: 11 minutes ago @: "plus": Your code does not meet the request. I think we have to extract the mini and the maxi from the 4 files, and store them in an array. Then extract the mini maxi from this array. To obtain a single mini and a single maximum.
|
|
|
Post by tsh73 on Nov 8, 2024 12:19:26 GMT
'enumerate files "test";str$(x);".txt"
dim info$(10, 10) files DefaultDir$, "test*.txt", info$()
nFiles = val( info$(0, 0))
mn1 = 1e10: mx1 =0-mn1
print "Files found: ";nFiles for i = 1 to nFiles file$= info$(i, 0) print i, file$, call getMinMax file$, mn, mx mn1=min(mn,mn1) mx1=max(mx, mx1) print mn, mx next print "-------------------------------------------------" print ,,mn1, mx1
sub getMinMax file$, byRef mn, byRef mx mn = 1e10: mx =0-mn open file$ for input as #f while not(eof(#f)) input #f, a mn=min(a, mn) mx=max(a, mx) wend close #f end sub
|
|
|
Post by honky on Nov 8, 2024 13:29:12 GMT
Oh la la, a tsh73 code, a code of another world. And who works really well, and without "sort", gloups ! ? !. It will not be easy to understand how it works.
|
|
|
Post by plus on Nov 8, 2024 13:43:33 GMT
Follow: 11 minutes ago @: "plus": Your code does not meet the request. I think we have to extract the mini and the maxi from the 4 files, and store them in an array. Then extract the mini maxi from this array. To obtain a single mini and a single maximum. Sorry, I was going by this in the original post: "The challenge consists in extracting the minimum and the maximum of the set of 4 files Only one mini, one maxi." That sounds to me like once you sort all the data from the 4 files you automatically see the "Only one mini, one maxi," Rod correct don't need arrays to set up the 4 files, I was correcting Honky's code to get it to work. tsh73 nice files work, don't have to know how many, just use base name. b = b + ...
|
|
|
Post by tsh73 on Nov 8, 2024 14:13:32 GMT
|
|
|
Post by honky on Nov 8, 2024 14:27:21 GMT
tsh73, "basic stuff", basic for you, not for me. Thank for link.
|
|
|
Post by honky on Nov 11, 2024 23:17:15 GMT
In fact, my problem is several files (4) containing several columns (8-1) and several lines. And the goal is to extract the minimums and maximums from each cell on the 4 files I thought I could adapt your code from another world (your fist msg of this thread. But despite my efforts, I did not succeed. So I did this, which work for all the cells of one line, but no way to go further on all the lines. The handling of arrays is not easy.
A little help would be welcome. Thank you for. global min,max,a dim col$(40),colo$(40) ' for r=1 to nRows for c=2 to nCols a$="": d=0: dd$="" for x=1 to nFiles open do$;fich$(x) for input as #g 'Ouverture fichier en lecture ' for r=1 to nRows line input #g, donn$ a$=a$+word$(donn$,c)+" " call tri a$,nFiles,g$,c,valu,min,max ' next r close #g next x d=d+1: col$(d)=str$(min);" ";str$(max) ' print "col$ ";c;"/ ";col$(d)'********** colo$=colo$+col$(d)+" " next c print "min max ";colo$'*************** ' next r return '----------------------------- sub tri a$,nFiles,g$,c,byref valu,byref min,byref max aa=0 for a=1 to nFiles aa=aa+1 if word$(a$,a)=g$ then aa=aa-1: goto [passaa] valu(aa)=val(word$(a$,aa)) [passaa] next a sort valu(),1,nFiles min=valu(1) max=valu(nFiles) end sub
|
|