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

Powered by SMFPacks Menu Editor Mod