Print array in reverse order (Page 1) ● SmileBASIC Source Forums

Register

# Print array in reverse order

• #1 ✎ 5 dashaund Greetings. I'm just learning programming so maybe the thing I ask for makes no sense at all, but I couldn't solve my problem with the SmileBASIC reference so I'm asking here. I'm trying to print an array from a location higher than zero, to zero (the first item in the array) but I can't seem to get it work. This is the code I'm using to input strings c=1 for i=0 to c-1 input v\$ if v\$=="." then goto @print 'I'm using this as a parameter to end the input arr\$[i]=v\$ c=c+1 next And this is the part I use to print. If I want to print the array in order, the following works just fine: @print for i=0 to c-1 print arr\$[i] next But if I try to print the array in reverse order, like I think it is, I get no output at all. This is the code I use: @print for i=c-1 to 0 print arr\$[i] next So the idea I have of printing an array in reverse order is clearly not working, that's why I want to ask here if it's even possible to do it this way, or even at all. The solution I'm thinking of it's to input this in reverse order into another array, and then print the new array which will print in reverse order the strings from the first array. But I wanna know if there is a way easier than this! Thanks in advance. Posted Edited by dashaund
• #2 ✎ 876 snail_ Helper Received for being very helpful around SmileBASIC Source Achievements First Day Joined on the very first day of SmileBASIC Source Website Night Person I like the quiet night and sleep late. Express Yourself FOR loops count up by one by default. They aren't smart enough to figure out on their own to count backwards, so you have to tell them with the STEP keyword: FOR I=C-1 TO 0 STEP -1 PRINT ARR\$[I] NEXT With STEP, you're just telling the loop to count by a different number. Counting up by -1 is the exact same as counting down by 1, mathematically. It could be any number: 12, -5, 23.4748383, whatever. Posted Edited by snail_
• #3 ✎ 360 Chemicalex Drawing I like to draw! Hobbies RNG Hidden Easter Eggs Video Games I like to play video games! Hobbies You can easily reverse arrays by adding STEP -1 to the end of your loop. FOR I=LEN (ARRAY\$)-1 TO 0 STEP -1 PRINT ARRAY \$[I ] NEXT Posted
• #4 ✎ 5 dashaund
FOR loops count up by one by default. They aren't smart enough to figure out on their own to count backwards, so you have to tell them with the STEP keyword: FOR I=C-1 TO 0 STEP -1 PRINT ARR\$[I] NEXT With STEP, you're just telling the loop to count by a different number. Counting up by -1 is the exact same as counting down by 1, mathematically. It could be any number: 12, -5, 23.4748383, whatever.
You can easily reverse arrays by adding STEP -1 to the end of your loop. FOR I=LEN (ARRAY\$)-1 TO 0 STEP -1 PRINT ARRAY \$[I ] NEXT
FUCK. Why didn't I think of this. Thank you very much.
Posted
• #5 ✎ 876 snail_ Helper Received for being very helpful around SmileBASIC Source Achievements First Day Joined on the very first day of SmileBASIC Source Website Night Person I like the quiet night and sleep late. Express Yourself Lol no problem. While I'm here Chem used LEN() in his code and didn't mention it. It's a function that returns how long an array (or string) is. You add one to (increment) C every time you add something to the array, but LEN() keeps track of that for you. You don't have to. Posted
• #6 ✎ 5 dashaund
Lol no problem. While I'm here Chem used LEN() in his code and didn't mention it. It's a function that returns how long an array (or string) is. You add one to (increment) C every time you add something to the array, but LEN() keeps track of that for you. You don't have to.
Yeah I went and read that in the reference. Very useful. Thank you.
Posted
• #7 ✎ 1464 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website You could do something like: DIM ARR\$[0] WHILE 1 INPUT V\$ IF V\$=="." THEN BREAK PUSH ARR\$,V\$ WEND FOR I=... etc. Posted