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?
Printable View
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?
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
That is correct.
Ciao steney and welcome to thinBasic community forum.
I just write down a little example that can give you a start.
[code=thinbasic]
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
[/code]
Ciao
Eros
Thanks for the help Eros.
Hi Steiny,
Also (from helpfile) to sort an array in descending order:
ARRAY SORT ArrayVariable([StartIndex]) [FOR nElements] [,{ASCEND | DESCEND}]
catventure.
oops, sorry :oops: I forgot about descending order.
Just change one line in script from
[code=thinbasic]Array Sort MyArray[/code]
to
[code=thinbasic]Array Sort MyArray, DESCEND[/code]
Eros
...may be this help too.
I have tried this very short way, but with no descend way, I have never tried it before :)
[code=thinbasic]' 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[/code]
welcome steiny and have fun with thinbasic :)
bye, frank