BASIC program that prints numbers in descending order

Started by DRobert, 18:29, 09 November 20

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ZbyniuR

Much shorter and faster way to sort, than bubble way:

20 FOR n=x TO 2 STEP-1:p$="":FOR m=1 TO n:IF a$(m)>p$ THEN p$=a$(m):p=m
30 NEXT:t$=a$(n):a$(n)=a$(p):a$(p)=t$:NEXT
In STARS, TREK is better than WARS.

marcm200

As far as I remember, string comparisons do not consider length of the string. So numbers in string form like "30" might be sorted on the wrong side of "200" or "400".

14 B$="30":C$="200":D$="400"
15 IF B$>C$ THEN PRINT B$;">";C$ ELSE IF B$<C$ THEN PRINT B$;"<";C$
16 IF B$>D$ THEN PRINT B$;">";D$ ELSE IF B$<D$ THEN PRINT B$;"<";D$
19 END

results in output "30>200" and "30<400". So unless all numbers in array A$ are of equal length, the sorting might not work properly.

It might be faster not to reassign strings but rather work on a secondary array IDX% and only swap the indices, but I haven't timed what's more efficient: short string swapping of two A$() entries or nested array access like A$(IDX%(P)) and swapping two IDX%() values.





DRobert

Hi

Ive made good progress with my programming thanks to users on this board.

Please could someone help with a simple query which I cant find in the manuals / online etc.

I am asking the program to multiply figures (examples below).

0.0000156 x 16000

I would like the computer to then print the answer to 2 decimal places (ie 0.24 in this example).   As it stands now the program prints out a lot longer answer which then distorts the screen prints and are too long to be of real use in any case.

If someone could quickly type up a few lines of code I will be able to make progress.

Any help would be appreciated as usual.

Regards

Jean-Marie


DRobert

Thank you Jean-Marie.

I have tried various attempts at typing in your suggestion but not with any success (syntax errors and type mismatch etc)  It will be my limited experience not helping me but perhaps you could type out exactly what I need to put into my program??

20 x=2300000/7
40 print x

My answer for above is 328571.429

How do I get it to print x as 3.28 only?

Thanks once again


poulette73

#30
328571.429 and 3.28 are not the same numbers.

If you directly try to use PRINT USING you are not going to get a result as x.xx
You must divide x by 100000 before.

Like this :

20 x=2300000/7
30 x=x/100000
40 PRINT USING "#.##";x
Result 3.29

Another less sexy solution is to use an alphanumeric string.

20 x=2300000/7
30 x$=STR$(x)
40 PRINT LEFT$(x$,2,2);".";MID$(x$,3,2)
Result 3.28

DRobert

Thanks Poulette and Jean Marie - you have shown me how to do the coding I need for my program.  It was simple really but I dont always do simple.  Excellent support.  

Kind ergards

DRobert

Please could somebody help me with a couple of queries.

I've enjoyed making progress with my "football" game and Ive reached a stumbling block.  I may not explain this very well but here goes.

If one of my football teams is represented throughout the game by the code - mc$ is it possible to convert this temporarily to a different code (example - op$) for a couple of stages.    If someone could write a couple of lines of code to this effect I would be most grateful.  

Also I am using the WinApe emulator and I think I have the settings correct for maximum RAM etc.   After running my program through about 35 cycles it crashes and refers to "memory full".   Any tips on how to continue my program.  Ive used a number of GOTO's and GOSUBS and there are a few stages that I can re-write to simplify the procedure and maybe free up some RAM.

Thanks for your help and support in this matter.   

andycadley

On the first issue, you can just copy the value from one variable to the other e.g.

op$ = mc$

Note that they are copies however, changing op$ won't change the value of mc$ (which may or may not be what you want).

You can always copy the value back again later with:

mc$ = op$

As to the memory issue: it's important to realise that BASIC won't ever use anything other than the main 64K of RAM, so that is all you have to work with (you can add RAM expansions in the emulator but BASIC will never use that memory). 

You may need to go through your code and make sure you aren't keeping data in variables that you no longer use (temporary arrays etc). Removing comments from BASIC might also help if you've got lots of them. Ultimately if you're holding too much data you might have to start thinking about ways to hold it using less space, but that's starting to wander into much more advanced territory.

GeoffB17

Your memory problem is not likely to have anything to do with GOTO or GOSUB.   But it may well be to do with strings, esp if you have a lot of $ variables that are frequently changing.   Most BASICs change a $ by creating the new one using a new piece of RAM, then they disconnect the old version and abandon it, but it MAY be still there, and maybe taking up memory.   The BASIC should have a process called 'garbage collection', which tidies up the RAM and recovers the space from discarded variables, etc.   BUT, garbage collection affects performance.

The emulator settings you refer to may have a setting for garbage collection, and you might have turned it off?  Some programmes might be perfectly OK with this, and much prefer the performance.   This new prog you're doing clearly MUST have the garbage collection enabled.  You might check this out.

Geoff

DRobert

Thank you Andy & Geoff B for very useful insight. 

Andy you have helped me sort out my coding problem.  I had tried op$=mc$ on my main program (prior to my post yesterday) but without success.  After your reassurance I ended up trying again by doing a few separate stand alone mini program lines of code which were successful.  I take on board your informative comments and will attempt to tidy up my main program now and ditch any surplus stuff in there to maximise the RAM capability. 

Thanks once again.



Geoff B

thanks once more for your comments which are of great use to an inexperienced amateur plodder like me.  I will revisit the program and see if I can cut out unnecessary code / words.   Interesting to read your comments regarding a garbage collection. 

andycadley

Locomotive BASIC should free up RAM whenever you change variable contents or use ERASE to clear out arrays. I very much doubt you'll find any emulator options that would interfere with that. I'm also not aware of any bugs there which would cause memory leaks over time.

Another things to consider is making sure you use integer variables if you are only storing whole numbers. They take less space and are faster as well.

DRobert

Thanks again Andy for useful tips etc Ive really learned a lot from building my programme and practising some of the suggestions made by contributors to this forum.

Ive almost finished the main part of my football game but find myself coming back to the original issue that led me to post on this forum back in 2020 - that is printing a "League table" with teams listed with their points achieved in descending order.

Ive cut and pasted AMSDOS's mini program from 19 December 2020 which Ive adapted to fit in my program.   It works fine with football teams  /different names and data.  However in my game  the teams and their points update after every round of games.  In the program below the READ command takes data from line 1000.   That is fine but the DATA is only there because its been typed into the program line 1000.   My program prodcues DATA after each round of games which vary every time so Im not sure how I can convert my DATA to fit into and be READ by the program???

My DATA would be held in the program in the following form  -  

Team 1 (tm1$) , Team 2 (tm2$), Team 3 (tm3$), Team 4 (tm4$)  and the Team points would be numeric ie tm1=7, tm2=11, tm3=6, tm4=13  -  any help as usual would be much appreciated.

100 MODE 1
110 DIM n$(4),v(4)
120 FOR p%=0 TO 3
130   READ n$(p%),v(p%)
140   LOCATE 10,p%+1:PRINT n$(p%):LOCATE 15,p%+1:PRINT v(p%)
150 NEXT p%
160 WHILE done=0
170   FOR p%=0 TO 2
180     IF v(p%)<v(p%+1) THEN s=v(p%):w$=n$(p%):v(p%)=v(p%+1):n$(p%)=n$(p%+1):v(p%+1)=s:n$(p%+1)=w$ ELSE done=1
190   NEXT p%
200 FOR p%=0 TO 2
210   IF v(p%)<v(p%+1) THEN done=0
220 NEXT p%
230 WEND
240 FOR p%=0 TO 3
250   LOCATE 10,p%+6:PRINT n$(p%):LOCATE 15,p%+6:PRINT v(p%)
260 NEXT p%
270 END
1000 DATA "Smith",35000,"Jones",40000,"Hall",37500,"Lee",41000

andycadley

You probably don't want every team in a different variable, rather you want an array that can be indexed by a team number.

So something like:

Dim teamsnames$(10)
Dim points%(10)

So that e.g. teamnames$(4) holds the name of team 4 and points%(4) holds their score. This way you can find any given bit of data about a team by using a teamnumber variable rather than having to have separate routines per team.

READ/DATA are really only meant for hard coded data, such as that when initializing a program, you wouldn't want to be using them for dynamic data under usual circumstances.

DRobert

I've had a good week on my game construction but run into a few glitches when testing my league table for which I have used the basis of AMSDOS's example of a sorting program.

I have 5 teams in my league and have allocated a couple of these for random scorelines which produce points of 3pts for a win, 1 for a draw and 0 for a loss (for testing purposes).   Having tested the program numerous tines it generally works perfectly.  However occasionally (particularly in the first 5 games or so) if the teams at positions 1 & 2 are swapped over due to the points process the figures shown are inaccurate.  The team moving from 2nd poistion back to 1st position suddenly gains 6 points and the other team loses 3 pts.

I've posted below 3 screen shots of my mini program (apologies for poor quality)You cannot view this attachment. and would be grateful if someone should have a quick look and see if they can spot why this should happen.  Once 10 games or so have passed the results are perfect.   As always any help is much appreciated.  Thank you.

DRobert

Sorry to be a pian but Im nearly there on my League table for my footy gae.  Spent a few hours last night testing and checking where the problem is.  Using the aforementioned AMSDOS's "sorting codelines" the problem seems to occur as below;

When Team A  (     tn$(0)   linked to    pts%(0)     )   is in a higher position than Team B (     tn$(1)      linked to    pts%(1)        )  all runs well.  

However when Team B gains points and pulls back level with Team the problem then seems to occur and although the teams switch position once Team B moves into a higher position the associated numbers go haywire???

Is it because there is no provision in the program coding for the teams being LEVEL / =  to each other?   

If anyone could help me by adjusting the coding in line 280 above (or any other suggestions) it would be much appreciated. 


Thank you


Powered by SMFPacks Menu Editor Mod