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

Sign In

Register
*Usernames are case-sensitive
Forgot my password
The website is currently experiencing email issues. You cannot register new accounts or change your password right now.

Print array in reverse order

  • #1 ✎ 3 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 ✎ 729 snail_ Helper Received for being very helpful around SmileBASIC Source Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Drawing I like to draw! Hobbies 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 ✎ 344 Chemicalex Chat Moderator Drawing I like to draw! Hobbies RNG Hidden Easter Eggs Forum Contributor Hidden Achievements 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 ✎ 3 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 ✎ 729 snail_ Helper Received for being very helpful around SmileBASIC Source Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Drawing I like to draw! Hobbies 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 ✎ 3 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 ✎ 1068 12Me21 Admin Helper Received for being very helpful around SmileBASIC Source Achievements Syntax Highlighter Received for creating the code syntax highlighter on SBS Winter 2016 Contest Winner I won the SmileBASICSource Winter 2016 Contest! Programming Contest You could do something like: DIM ARR$[0] WHILE 1 INPUT V$ IF V$=="." THEN BREAK PUSH ARR$,V$ WEND FOR I=... etc. Posted