jon93
New Member
Posts: 15
|
Post by jon93 on Jun 19, 2022 17:54:40 GMT
I am a novice to JB but I knew quite a lot about quick basic. I am writing a program to mimic a 2 dimensional solar system, planets revolving around sun. I have sub programs to calculate distances, forces between the planets acceleration etc. But if I load values into an array, say a 2dimensional array holding values for forces between each pair of planets/sun, the info is in the subprogram but It is not always available elsewhere. It happens unpredictably.
sometime I write the sub again and it works and I can get the values out of the sub. sometimes not.
I don't think I am entering stuff incorrectly.
Is there something to do with order of code?
I believe arrays are automatically global. JB won't let me useglobal with array.
any suggestions?
Jon
|
|
|
Post by tsh73 on Jun 19, 2022 18:17:09 GMT
Yes arrays are global They cannot be passed in/out subs functions, at all (EDIT so you cannot make array A() and pass it into other sub and use it there as B() ) If there is something working not as you expected post the code.
|
|
|
Post by davidk64 on Jun 19, 2022 22:19:23 GMT
Oh that does sound interesting! A good first project I reckon. Good luck with it How will you display the planets revolving round the sun out of interest? (A little while back I wrote a celestial navigation tool for Prepar3D in Visual Studio C#. The stars were just a small number of pixels depending on relative star brightness)
|
|
|
Post by tenochtitlanuk on Jun 20, 2022 8:44:35 GMT
You might be interested in my 'orrery' simulation of planetary motion at LB forum
|
|
|
Post by plus on Jun 20, 2022 17:59:46 GMT
Ha! I never even thought to try the Global Keyword with an Array, it's automatically global. Does the global keyword with an array raise flags or just quietly screws up or makes no difference. Let's see:
'Global arr(21) ' < this is an error, arr() is an error, arr is different variable altogether, ' Global and arrays don't mix!!! dim arr(21) for i = 1 to 21 arr(i) = i^3 next
for i = 1 to 21 print i, arr(i) next call getCubeRoot
sub getCubeRoot for i = 1 to 21 print i, arr(i)^.3333 next end sub
|
|
|
Post by davidk64 on Jun 20, 2022 22:11:02 GMT
To see Neptune requires patience! (Bottom right corner - I didn't screenshot quick enough)
|
|
|
Post by jarychk on Jun 22, 2022 7:45:11 GMT
I looked through the topic very quickly and maybe I not understand well, but if you use subroutine of the JB GOSUB kind, then the whole program sees the array values.
|
|
jon93
New Member
Posts: 15
|
Post by jon93 on Aug 6, 2022 18:42:31 GMT
Hello tenochtitlank, I like your orrery. Very elegant, not much code.
My prog is messy. It doesn't represent any actual physical bodies. Units are arbitrary. I calculated the distance between each body and all the others using x and y coordinates and pythagoras.
then Newtons f = m1m1g/d^2 to calculate force on the object in x and y directions. Then accelerated the objects according to the force on them.
object (1) in the arrays was the sun. Each iteration I moved the sun back to the centre and moved the other planets accordinglt.
The interesting thing was ow very unstable the system is. The planets were always bumping into eachother. This causes forces to be enormous and the bodies would fly off onto outer space. I think it is amazing that the solar system is still here!
Jon
|
|
jon93
New Member
Posts: 15
|
Post by jon93 on Aug 6, 2022 18:49:32 GMT
Reply about arrays. I may have posted this before (not yet used to the board. Thank you everyone for replies. I had no idea that the board was so active and that people were helpful. I was on holiday.
I solved the problem. In Quick Basic I would use a shared variable for for next loops in order to make it run more quickly. ( does not have to create a variable every time.)
So in just basic I did this.
global i dim jons_array(20)
sub PutStuffInArray for i = 1 to 20
end sub
|
|
jon93
New Member
Posts: 15
|
Post by jon93 on Aug 6, 2022 19:07:28 GMT
Oops, sent msg too soon! The problem was that I made the variable in the for/next loop to be global. (This was to increase speed because the prog didn't have to create a new variable each iteration. Thats what quickBasic suggested years ago) This will show the problem
Code:
global i 'if you comment this line out it changes the result. dim jonsarray(20) call PutStuffInArray
for k = 1 to 5 print "Printing in the main body of the program",jonsarray(k) next
sub PutStuffInArray for i = 1 to 5 jonsarray(i) = rnd(1) 'could be anything here to put a value in the array print "Printing inside the PutStuffInArray sub",jonsarray(i) 'shows that a value has been assigned to that position in the array next
end sub
'end of program
If i is global then printing in the main body of the program shows only zeros. I you comment out the global i statement, the printing in the main body shows the values that are in the array
It took a longtime to figure this out!
Jon
|
|
|
Post by tenochtitlanuk on Aug 6, 2022 22:16:34 GMT
Instability is inherent in multi-body attraction. Best you can do is minimize the effects of digital solutions to a continuous motion problem. Use smallest time step you can even though it means slow display. And use any calculus 'tricks' you know about, such as calculating at half-intervals. I have a lot of fun with LB- but can only admire the accurate, fast calculation/simulation used for calculating journeys through our 3D solar system on NASA's super-computers!
|
|
|
Post by plus on Aug 7, 2022 11:08:14 GMT
Oops, sent msg too soon! The problem was that I made the variable in the for/next loop to be global. (This was to increase speed because the prog didn't have to create a new variable each iteration. Thats what quickBasic suggested years ago) This will show the problem Code: global i 'if you comment this line out it changes the result. dim jonsarray(20) call PutStuffInArray for k = 1 to 5 print "Printing in the main body of the program",jonsarray(k) next sub PutStuffInArray for i = 1 to 5 jonsarray(i) = rnd(1) 'could be anything here to put a value in the array print "Printing inside the PutStuffInArray sub",jonsarray(i) 'shows that a value has been assigned to that position in the array next end sub 'end of program If i is global then printing in the main body of the program shows only zeros. I you comment out the global i statement, the printing in the main body shows the values that are in the array It took a longtime to figure this out! Jon Wow an unexpected result, you'd think i should up to 6! not 0, not 0 at all if i is global! global i 'if you comment this line out it changes the result. dim jonsarray(20) call PutStuffInArray
for k = 1 to 5 print "Printing in the main body of the program",jonsarray(k) next Print "I am now ";i
sub PutStuffInArray for i = 1 to 5 jonsarray(i) = rnd(1) 'could be anything here to put a value in the array print "Printing inside the PutStuffInArray sub",jonsarray(i) 'shows that a value has been assigned to that position in the array next Print "I am now ";i end sub
|
|
|
Post by plus on Aug 7, 2022 11:13:53 GMT
Apparently i is global except as an index to a FOR loop!
global i 'if you comment this line out it changes the result.
i = 10 Print "I am now ";i
dim jonsarray(20) call PutStuffInArray
Print "I am now ";i for k = 1 to 5 print "Printing in the main body of the program",jonsarray(k) next Print "I am now ";i
sub PutStuffInArray for i = 1 to 5 Print "I am now ";i jonsarray(i) = rnd(1) 'could be anything here to put a value in the array print "Printing inside the PutStuffInArray sub",jonsarray(i) 'shows that a value has been assigned to that position in the array next Print "I am now ";i end sub
|
|
|
Post by jarychk on Aug 8, 2022 1:25:29 GMT
Oops, sent msg too soon! The problem was that I made the variable in the for/next loop to be global. (This was to increase speed because the prog didn't have to create a new variable each iteration. Thats what quickBasic suggested years ago) This will show the problem Code: global i 'if you comment this line out it changes the result. dim jonsarray(20) call PutStuffInArray for k = 1 to 5 print "Printing in the main body of the program",jonsarray(k) next sub PutStuffInArray for i = 1 to 5 jonsarray(i) = rnd(1) 'could be anything here to put a value in the array print "Printing inside the PutStuffInArray sub",jonsarray(i) 'shows that a value has been assigned to that position in the array next end sub 'end of program If i is global then printing in the main body of the program shows only zeros. I you comment out the global i statement, the printing in the main body shows the values that are in the array It took a longtime to figure this out! Jon Just studying it (not even actually running it) and thinking how it goes, reminds one that arrays are global, but can still be used inside a subroutine. ( I tend to like the GOSUB kind.)
|
|
jon93
New Member
Posts: 15
|
Post by jon93 on Aug 8, 2022 1:33:15 GMT
Interesting that after the FOR loop it becomes zero inside the sub Following on the idea above I added a value of i BEFORE the FOR/next inside the sub See what the result is The I in the FOR loop is a different variable, which remains untou=ched by the FOR loop.
global i 'if you comment this line out it changes the result. dim jonsarray(20) call PutStuffInArray
for k = 1 to 5 print "Printing in the main body of the program",jonsarray(k) next Print "I am now ";i
sub PutStuffInArray
i=2 'added by jon93 on 8 7Aug for i = 1 to 5 jonsarray(i) = rnd(1) 'could be anything here to put a value in the array print "Printing inside the PutStuffInArray sub",jonsarray(i) 'shows that a value has been assigned to that position in the array next Print "I am now ";i end sub
Again thank you everyone for your help.
To davidK64: I show the planet as a dot
for i = 1 to numberofplanets print #w, "set ";xp(qi);" ";yp(i) 'puts a dot and moves the cursor next
xp(i) is array holding position of the planet on the x axis.
In quick basic there was pset (x,y) Is there such a command in justbasic?
Jon93 Jon
|
|