View Full Version : newbie question
I am having trouble with array's and learning the tbasic language so my question is this. Using the console module, what would be the correct way to write an application that allows you to enter 10 numbers and display them in descending order?
Michael Hartlef
05-11-2009, 21:44
Hi steiny.
Welcome to the forum! :) Just to get it right. You want to enter 10 numbers, store them in an array. Then sort them descending? Adn then print them in the console window?
Best wishes
Michael
ErosOlmi
05-11-2009, 21:57
Ciao steney and welcome to thinBasic community forum.
I just write down a little example that can give you a start.
Uses "Console"
Dim HowManyNumbers As Integer
Dim MyNumber As Number
Dim MyArray() As Number
Dim Counter As Long
Do
Print "How many numbers (1 to 10): "
HowManyNumbers = Console_ReadLine
Loop While Outside(HowManyNumbers, 0, 10)
If HowManyNumbers > 0 Then
ReDim MyArray(HowManyNumbers)
For Counter = 1 To HowManyNumbers
Print "Enter number " & Counter & ": "
MyArray(Counter) = Console_ReadLine
Next
PrintL "---Printing out sorted ---"
Array Sort MyArray
For Counter = 1 To HowManyNumbers
PrintL Format$(Counter, "00") & ": ", MyArray(Counter)
Next
PrintL "--------------------------"
PrintL "<Press a key to finish>"
Console_WaitKey
Else
PrintL "---Ok, no numbers---------"
PrintL "<Press a key to finish>"
Console_WaitKey
End If
Ciao
Eros
Thanks for the help Eros.
catventure
05-11-2009, 22:05
Hi Steiny,
Also (from helpfile) to sort an array in descending order:
ARRAY SORT ArrayVariable([StartIndex]) [FOR nElements] [,{ASCEND | DESCEND}]
catventure.
ErosOlmi
05-11-2009, 22:14
oops, sorry :oops: I forgot about descending order.
Just change one line in script from
Array Sort MyArray
to
Array Sort MyArray, DESCEND
Eros
Lionheart008
05-11-2009, 22:16
...may be this help too.
I have tried this very short way, but with no descend way, I have never tried it before :)
' Empty GUI script created on 11-05-2009 20:47:57 by (ThinAIR)
uses "console"
dim n as long
DIM arrays( 20 ) AS INTEGER
arrays(1) = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
ARRAY SORT arrays(20) FOR 1, descend
' -- Print line of array items joined by space
PRINTL JOIN$( Arrays, " " )
WAITKEY
welcome steiny and have fun with thinbasic :)
bye, frank