
-----------------------------------
Chayio
Fri Nov 18, 2011 11:28 am

Listing an Array backwards
-----------------------------------
I'm trying to create a program that asks for 10 integers, then lists them in an array but backwards. For example the last number you entered shows up first when you run the program, does anyone know how to do this? Any help is appreciated, this is what I have so far:


var integers : array 1 .. 10 of int


for i : 1 .. 10

    put "Input 10 intergers."

    var integer : int
    get integer 
    
    integers (i) := integer

end for

cls
for i : 1 .. 10

    put integers (i) 
    
end for





-----------------------------------
DemonWasp
Fri Nov 18, 2011 11:59 am

RE:Listing an Array backwards
-----------------------------------
This exact case is listed as one of the examples in the documentation of for, which I suggest you read through : http://compsci.ca/holtsoft/doc/for.html

-----------------------------------
Aange10
Fri Nov 18, 2011 5:58 pm

RE:Listing an Array backwards
-----------------------------------
and just to help you a bit


    integers (i) := integer 


What are you telling the computer here?


.
.
.

Okay that's what it's doing.

-----------------------------------
Tallguy
Fri Nov 18, 2011 8:15 pm

RE:Listing an Array backwards
-----------------------------------
run ur print array backwards

instead of having
[code]
for i:1..10[/code]

have it in decreasing order
[code]
for decreasing j : 10 .. 1
            put j
end for
[/code]


*srry i forget the code syntax for turing

-----------------------------------
Aange10
Fri Nov 18, 2011 8:42 pm

RE:Listing an Array backwards
-----------------------------------
An easier, and more flexible way to do this though, would be to change what we put the information into, not what information we use.

If there came a situation where you needed it to go from 1 .. 10, but also needed to store information backwards, making the array 10 .. 1 wouldn't work.

you told the computer


integers (i) := interger


Why not store it starting at the top and working your way down?


integers (upper(integers) - i) := integer


however that would start you at 9 and end you at 0, which doesn't fit your array. So we tell the computer what we want it to do (start at 10 and end at 1)

The way we do this is just through some simple math! 

We call this problem solving.


integers (upper(integers) - i + 1) := integer


Now everything starts one higher.
