CPCWiki forum

General Category => Programming => Topic started by: DRobert on 18:29, 09 November 20

Title: BASIC program that prints numbers in descending order
Post by: DRobert on 18:29, 09 November 20
Hi to all
I have only just signed up to this forum and looking to learn from experience of others more proficient in these matters.
After discovering my old Amstrad 464+ (that had been mothballed over 25 years ago) I was surprised to find that my very simple programmes (saved on a cassette tape) still worked.   This has fired my enthusiasm for the Amstrad and wondered if anyone could help me with regards to my tentative attempts at programming.
I enjoy sports and some of my simple programmes are based around soccer etc.  All have been on screen text based images as opposed to graphics.   Much of what I have done so far is based on "GOTOs" which I realise from comments on here are generally frowned upon.   However my main stumbling block at the moment is finding an appropriate block of "BASIC" coding that can read variable numbers (linked to STRING$) and then reproduce them / PRINT in a descending or ascending order.  I thought I had read many years ago that, for example, a business could prepare a list of employees and their salaries in some sort of number order?
I have managed a simple BASIC program with 3 variables (ie a,b,c) but have had to type this in a 'long hand" fashion.  Even for just 3 variables there is a fair amount of coding and to increase it by one or two more increases this exponentially (let alone having a list of 20 variables).   I have googled for the last week or so and although there are programs in other computing languages which carry out this function I cannot find anything in Amstrad BASIC which performs this SORT function.

I would appreciate any comments which will lead to my solution of this issue.   Thanks for reading.
Title: Re: BASIC program that prints numbers in descending order
Post by: eto on 21:50, 09 November 20
You are looking for a sorting algorithm. The easiest ist probably Bubble Sort and you can use Arrays (DIM) in Basic to achieve that.

I also found this thread where a DSK image with lots of BASIC examples is included and one of the examples seems to be a Bubble Sort implementation.
http://www.cpcwiki.eu/forum/cpcwiki-discussion/acu-type-ins-project/msg138542/#msg138542

Title: Re: BASIC program that prints numbers in descending order
Post by: SRS on 21:57, 09 November 20
And here are more of them:
https://cpcrulez.fr/GamesTest/applications_util-selection_sort__cwta.htm?t=Uw==
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 22:01, 09 November 20
Thanks very much for your prompt replies.  I'll give them a try and let you know how I go on. 
Title: Re: BASIC program that prints numbers in descending order
Post by: GeoffB17 on 17:30, 10 November 20
Hello,

Happy to help if I can.

I spent many years doing 'business' type programming, mainly using dbase/Clipper type systems, for accounts/stock/wholesale type businesses.   Not in BASIC, but I keep on using various BASIC variants for specific jobs.

Yes, things are a LOT easier when the language you're using has the right things built in, esp sorting/indexing etc.   However, the Bubble Sort is easy to implement and is fine for smaller data sets, but if your data gets bigger it's well worth trying the more complex Quick Sort method.   There are variants of both readily available in BASIC that will work fine for you.

Don't worry about GOTO - I still use them now.   yes, they can lead to spaghetti code that jomps agout all over the place making a program much more difficult to follow, and fix when it goes wrong or needs improving, but they're perfectly OK if used with care, and with suitable comments.   It's just about style.

As for your program, I'd suggest that you create a data file as a .txt file.   Put your formatted data into this file, order doesn't matter.   Formatting DOES.   Then write your prog(s) to read the data from the file, process it however you need, and then maybe write the data back to a new file.   You can add extra data to the file as required, you may need to read ALL the data into your prog, but you don't need to do anything with more than you need to.   Depending on the data, you might use more than one file to store it (main or header records, with separate 'transactions').

Geoff
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 20:11, 10 November 20
Thank you Geoff for your kind response and encouraging comments.


I am very much at a preliminary stage in all of the above but have spent a lot of time reading and watching you tube clips trying to "find my level".  I've spent time today trying to understand the principles of "bubblesort" in conjunction with swotting up on the simple BASIC commands that I last used over 25 years ago.  Its tempting to try and run before walking but that is virtually impossible to do without putting in the effort initially. 


Thanks once again to you and the other guys above for helping me towards the solutions I am looking for.
Title: Re: BASIC program that prints numbers in descending order
Post by: GUNHED on 00:34, 11 November 20
Using GOTOs work, but for BASIC the 464 Plus has the nice command ON x GOTO line1, line2, line3 ... see manual. Also GOSUB is helpful.
Title: Re: BASIC program that prints numbers in descending order
Post by: goksteroo on 08:40, 11 November 20
The most efficient method is going to depend on the type and range of data you are sorting - is it purely numerical? Is it integer or real numbers? What is the range (or potential range) of the numbers? How are the array/s holding all the data organized?
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 13:06, 11 November 20
Quote from: GUNHED on 00:34, 11 November 20
Using GOTOs work, but for BASIC the 464 Plus has the nice command ON x GOTO line1, line2, line3 ... see manual. Also GOSUB is helpful.


Ah thanks,  I'll have a play around with those options.  I will read and review the manual to get my knowledge up to speed from what is currently a slow start.
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 13:15, 11 November 20
Quote from: goksteroo on 08:40, 11 November 20
The most efficient method is going to depend on the type and range of data you are sorting - is it purely numerical? Is it integer or real numbers? What is the range (or potential range) of the numbers? How are the array/s holding all the data organized?


Thanks for your comments.  To be honest I'm only just discovering the potential for writing simple programs for a bit of fun and to keep my brain cells active now I have retired (can only do so much soduko etc).   


I cant honestly say exactly what data will be sorted.   I would imagine that any sports type programs would involve statistics and figures such as "teams and league tables" plus "individual names plus goals scored etc".  It would be useful to be able to include a few stats alongside any game play.  I'm only at an early stage of discovering what programs I can write so start with a simple idea and then think about adding other things when they spring to mind.  I know this isn't the ideal way to plan a program as an efficient structure is important.   But I cant really plan too far ahead until I can see what is available  / possible.    I think the term "trying to run before I can walk" is appropriate for me at the moment.   :)    Thank you for your support.   It's good that people on this forum are prepared to assist and encourage beginners.
Title: Re: BASIC program that prints numbers in descending order
Post by: GeoffB17 on 00:51, 12 November 20
Aaargh!

Just been thinking about some of the things I as saying about starting data in a file.  my mind is of course too set in the ways allowed by access to floppy disks, disk files, etc.   What I was thinking is of course FAR from so easy if you've got a cassette based system, and I suspect that OP is so placed.

You'd need to have a prog, a type of text editor, that could allow the creation of a text file which could then be saved to the tape, and then re-loaded, edited/added to, then resaved.   I don't know if there is such a thing.   Obviously, something could be written.

Geoff
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 11:26, 05 December 20
Hi I've manged to set up a simple soccer program which generates results and I can list a few teams and scores.  The one thing I am struggling with is the league table in descending order based on team points.  I have spent the last month reviewing many "bubblesort" programmes (as recommended above) and practising different procedures but due to my limited experience I cannot grasp how to take my program to the next step.  The following link was interesting and the presenter explained things in a manner that I just about got a grasp of but I cannot convert his example program to meet my needs.   https://www.youtube.com/watch?v=lfGv-KjnyHU&ab_channel=learn4free 


To give a simple example of what I would like to achieve is an AMSTRAD BASIC coding to convert the following example data into a league table based upon points attained.  If I can get my head around that I can then expand to include more teams. 


Aston Villa 2 Liverpool 4
Leeds Utd 1 Chelsea 0


In long hand the table would show as follows
1st place -  Liverpool  Played 1, won 1, drawn 0, lost 0, goals scored 4, goals conceded 2, points attained 3
2nd place - Leeds Utd Played 1, won 1, drawn 0, lost 0, goals scored 1, goals conceded 0, points attained 3
3rd place -  Chelsea    Played 1, won 0, drawn 0, lost 1, goals scored 0, goals conceded 1, points attained 0
4th place - Aston Villa Played 1, won 0, drawn 0, lost 1, goals scored 2, goals conceded 4, points attained 0

Ive written a program to print out the above league table but the amount of coding is considerable and had to type in every possible option that could occur in long hand (with GOTO's etc as opposed to getting the computer to generate the table for me). If I was to try to increase the number of teams to 20 it would take me a few years to get it all typed out which defeats the object really.    All the "bubblesort" programs just show how a series of random numbers can be sorted which looks great but doesnt help me to link numbers to team names etc.  If anyone could find a few minutes to type up an example or refer me to an online link where the code has already been done I would be grateful.  My wife is on the point of banning me from my computer as Im getting frustrated at not being able to sort what I want to do.  I repeat I am only a beginner and have not gone through all the learning processes that many of you will have done.  Trying to learn as I go.  Any help would be gratefully received. 
Title: Re: BASIC program that prints numbers in descending order
Post by: menegator on 22:06, 05 December 20
You should sort first by wins (descending), then by (goals for - goals against)(descending) then by goals for (descending) then by name (ascending)

If you want to make it more realistic before sorting by name you should check the game(s) between the two or more teams having the same rank after the first three sorts


Have in mind that bubbleshort has O(n²) cost, depending on the set maybe you should consider another sorting method since in your case the sorting needs to be cascading.
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 17:23, 18 December 20
I've not made much progress on my "sorting" coding I'm sorry to say.   I understand the above "bubblesort" program which can sort a series of numbers but I cant see how to apply that to my needs.    Random numbers mean nothing really - its more a case of having a list of names / teams etc with associated numbers and then sorting them in order.  I tried to use a "football" example in my previous post as an example of what I need but it could apply to anything. 


If anyone could provide me with a bit of code that would help me.


Just a simple program to list say names of employees at a firm with different salaries and to sort and then print in descending order.


ie


Smith - £35000 , Jones - £40000,  Hall - £37500,  Lee - £41000


The above data may vary regularly so the program would needed to be able to take account of this and update accordingly.


If I could get this in BASIC I might be able to adapt it to my own games.   ie a soccer game where points and goals vary each game?    Im not making progress at the moment Im sorry to say.  Sorry to be a pain.  Im only a beginner.
Title: Re: BASIC program that prints numbers in descending order
Post by: AMSDOS on 12:02, 19 December 20
My example merely works as a Bubble sort by working out the Lowest to Highest of Incomes with Smith, Jones, Hall & Lee and making sure the Right Person is reassigned with the Right Income. All of that takes places in Line 180 which compares an income higher stored in one array v(p%) and the next position within the array v(p%+1). In order to preserve when things are shifted, variables 's' and 'w$' are used to temporary store the income (variable 's') and the name (variable 'w$'), and once the other data has been moved, the other data stored in variable 's' and variable 'w$' is put into it's new position.




100 MODE 1
110 DIM n$(4),v(4)
120 FOR p%=1 TO 4
130   READ n$(p%),v(p%)
140   LOCATE 10,p%:PRINT n$(p%):LOCATE 15,p%:PRINT v(p%)
150 NEXT p%
160 WHILE done=0
170   FOR p%=1 TO 3
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 WEND
210 FOR p%=1 TO 4
220   LOCATE 10,p%+5:PRINT n$(p%):LOCATE 15,p%+5:PRINT v(p%)
230 NEXT p%
240 END
1000 DATA "Smith",35000,"Jones",40000,"Hall",37500,"Lee",41000
Title: Re: BASIC program that prints numbers in descending order
Post by: andycadley on 13:33, 19 December 20
The trick is to realize they're really the same thing, but you're sorting an index of elements in an array by comparing the values of the relevant scores in a different array. It's difficult to find common examples of this because more modern programming languages let you put all the related fields together into records or objects.


So you need three arrays:


Teams$(n) which holds the names of all the teams.


Scores(n) which holds the corresponding score for a team.


Then Positions(n) which holds the numbers corresponding to teams in position order. I.e. if Positions (2) has a value of 5, then the teams in second place is Teams$(5) with a score of Scores(5). The positions array should be initialised with the numbers 1..n


When it comes to sorting, you sort the values in the Positions array, but you do it by instead comparing the scores in the Scores array. So if you're comparing elements in positions a and b the comparison would looks something like:


If Scores(Positions (a)) < Scores(Positions (b)) Then swap values in Positions(a) and Positions(b)


Title: Re: BASIC program that prints numbers in descending order
Post by: AMSDOS on 13:47, 19 December 20
Yes, I made some stuff-ups in my previous programme, though have since worked at it, so it's now a true sorting routine, it can be altered from Decending to Asending order or Visa-versa by simply adjusting the less than or to greater than signs in lines 180 and 210. Of course the size of sorting reflects the size of the Loops-1 for lines 170 and 200, so the more sorting needs to be rechecked, which is where the loop at 200 is needed.




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
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 19:33, 19 December 20
Thank you AMSDOS for a brilliant effort and for spending time helping me with my program.  :)   I had tried typing in your initial program earlier today and it worked well apart from a couple of glitches.  Great that you spent time perfecting the program which now appears to work fine.  Also thanks to "andycadley" for the explanations provided  :) .  This gives me plenty to think about and work upon.  I am learning lots along the way.  "andycadley" - your comments about there being few modern examples rings true and also reminded me that I am trying to enhamce my simple programs created in the 1990's when I was thirty odd years old.  Im now in my early 60's and finding that the brain cells arent as agile as they used to be, but I will keep plodding on.


Im am now trying to work out how I can integrate this within my soccer game program.  If the game produces new data after each set of games how do I get the computer to read this "updated data".   The DATA in the above program has been typed in by the programmer so how can I integrate the above with my own ongoing program.  Whether its employees' salaries or points achieved by a soccer team I want the DATA to update without having to "INPUT" whilst running the program or editing line 1000 everytime things change.  I realise I am light years behind you guys but I am learning a lot and hope to improve a little as time goes by.  I also appreciate that you have spent time looking at my queries.
Title: Re: BASIC program that prints numbers in descending order
Post by: AMSDOS on 00:30, 20 December 20
Moving from DATA to Inputting the data only just needs slight adjusting. Essentually I'm now writing a Simple Database, the main problem now is how big does it need to be. Earlier in this thread @eto (https://www.cpcwiki.eu/forum/index.php?action=profile;u=3625) posted a link to the BASIC Tutorial I typed in from ACU, which also contained a simple Database with Sorting routine, though I'll post another here:


100 MODE 2
110 DIM n$(10),v(10)
120 WHILE done=0 AND n%<>10
130   INPUT"Name:";n$(n%):INPUT"Wage:";v(n%)
140   n%=n%+1
150   IF n$(n%-1)="end" THEN n%=n%-2:done=1
160 WEND
170 done=0
180 WHILE done=0
190   FOR p%=0 TO n%-1
200     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
210 NEXT p%
220 FOR p%=0 TO n%-1
230   IF v(p%)<v(p%+1) THEN done=0
240 NEXT p%
250 WEND
260 FOR p%=0 TO n%
270   LOCATE 20,p%+6:PRINT n$(p%);" ";v(p%)
280 NEXT p%
290 END



This one works on the assumption there's 10 workers and once 10 people have been entered, their Salaries are sorted, however if there's less than 10 workers, the user can type-in end when Name: comes up, naturally Wage will also appear, so I just type-in 0 and the sorting takes place. The only crunch here is if only one worker is entered, followed by end, the sorting won't work and fall into an infinite loop, obviously when 'end' is entered, there's no need to display it, which is why n%=n%-2 in line 150.


It's possible to play around, so for example if 'end' is entered it'll skip asking for a wage as well:
130   INPUT"Name:";n$(n%)
140   IF n$(n%)<>"end" THEN INPUT"Wage:";v(n%):n%=n%+1 ELSE done=1
150 <delete>
170 n%=n%-1:done=0
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 18:12, 20 December 20
Thank you AMSDOS for your further efforts which will help in my learning and keep me going for another month or so.  Ive have already amended your initial coding to incorporate a "20 team" English Premier League soccer table which works perfectly thank you.  I think your second program will enable me to do similar whilst INPUTting details.   My next thing is working out how to integrate my 1995 soccer program with your above coding to constantly update each time a team gets 3 points for a win, 1 point for a drawn game etc without having to physically type it in myself.  For example If say I have 4 teams in a mini league and my existing program generates the number of points next to each team how can I carry these details into the "bubblesort" as you have shown me above without me physically typing them in and more importantly updating with new points totals after each round of games played?  For instance the program will already know that Team A  has say 10 points; Team B has 18 points; Team C has 11 points and Team D has 14 points.   My existing program will simply add more points to each of these Team totals as appropriate after each round of games is complete.  Ideally if these figures can then be put into the "bubblesort mix" then the mini league table can be printed on screen as per your above two previous example codings.   Sorry to be a pain but I feel I am almost there on the direction I could go for some of my programming and mini games.  These examples are of great use to me.  Your help is greatly appreciated as are the hints and tips from all other users on this forum. 
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 13:35, 11 August 22
Hi

You guys were a great help a while back when you helped me to create a few programmes which have provided lots of enjoyment in my spare time.  Ive just got back to having another go at my Amstrad stuff and wondered if you could help me on an issue which is blocking my progress.  Apologies for being a rank amateur at this sort of thing and hopefully someone will provide a simple answer to my query.

I've create a few mini sports games including football and tennis mock ups using the randomiser facility.  Such as x=0+INT (RND*7).   I've found it quite easy to add up running totals where the increase is a fixed number on each occasion *ie  g=g+1 or g=g+3........... etc.

However I'm struggling to provide a separate running total for x where x is the random number which changes on each loop of the program.   This is on a footy game where x is the number of goals scored by a team in one game but then is likely to be different in subsequent games (or loops of the system).  What I need is a bit of code so that the running total of x goals increases agter each game.  I've tried various things like x=xg etc but I'm struggling to grasp what I need to do next.   If any kind person could type out a simple bit of code to achieve this I would be very grateful.  (my game is very simple and involves a number of goto's and gosubs to progress each stage).   
Title: Re: BASIC program that prints numbers in descending order
Post by: andycadley on 14:27, 11 August 22
If X is your random number, can you not just do something like:

LET x_total = x_total + x

On each pass of the loop? Or am I missing something?
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 14:43, 11 August 22
Hi Andy thanks for getting back to me. 

Yes that looks like the answer although I'm not sure how to print the running total on screen.

I inserted a line    -   print  x_total        and it came back with syntax error.   Same with   x_total+x

In fact the line you provided me has also come up with syntax error.  Am I typing something incorrectly?

I think we are close to what I need - just me poor at the programming stuff!

Appreciate your help.
Title: Re: BASIC program that prints numbers in descending order
Post by: andycadley on 15:36, 11 August 22
Probably aren't allowed underscores in variable names, it's been a very long time since I programmed BASIC. Try something like:

LET xtotal = xtotal + x

Instead.
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 16:05, 11 August 22
Brilliant Andy

Looks like that will work.  Just a case of me finding the correct place in the program to insert this.

Used a mini test program inserting your line and worked well.   It was so simple really but even "simple" was too much for me!  :)

Thank you so much.
Title: Re: BASIC program that prints numbers in descending order
Post by: ZbyniuR on 17:03, 11 August 22
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
Title: Re: BASIC program that prints numbers in descending order
Post by: marcm200 on 20:17, 11 August 22
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.




Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 03:03, 27 September 22
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
Title: Re: BASIC program that prints numbers in descending order
Post by: Jean-Marie on 05:03, 27 September 22
I guess you wanna use PRINT USING (https://www.cpcwiki.eu/index.php/Locomotive_BASIC#USING).
Ex.: ? using "#.##";PI
3.14
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 12:51, 27 September 22
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

Title: Re: BASIC program that prints numbers in descending order
Post by: poulette73 on 13:39, 27 September 22
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
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 16:06, 27 September 22
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
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 01:23, 30 September 22
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.   
Title: Re: BASIC program that prints numbers in descending order
Post by: andycadley on 12:24, 30 September 22
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.
Title: Re: BASIC program that prints numbers in descending order
Post by: GeoffB17 on 23:50, 30 September 22
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
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 02:29, 01 October 22
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. 
Title: Re: BASIC program that prints numbers in descending order
Post by: andycadley on 12:44, 01 October 22
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.
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 13:28, 01 October 22
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
Title: Re: BASIC program that prints numbers in descending order
Post by: andycadley on 13:58, 01 October 22
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.
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 02:29, 08 October 22
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) 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.
Title: Re: BASIC program that prints numbers in descending order
Post by: DRobert on 15:50, 09 October 22
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