Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Listing an Array backwards
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Chayio




PostPosted: Fri Nov 18, 2011 11:28 am   Post subject: 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:
Turing:


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



Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Nov 18, 2011 11:59 am   Post subject: 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




PostPosted: Fri Nov 18, 2011 5:58 pm   Post subject: RE:Listing an Array backwards

and just to help you a bit

Turing:

    integers (i) := integer


What are you telling the computer here?


.
.
.

Okay that's what it's doing.
Tallguy




PostPosted: Fri Nov 18, 2011 8:15 pm   Post subject: RE:Listing an Array backwards

run ur print array backwards

instead of having
code:

for i:1..10


have it in decreasing order
code:

for decreasing j : 10 .. 1
            put j
end for



*srry i forget the code syntax for turing
Aange10




PostPosted: Fri Nov 18, 2011 8:42 pm   Post subject: 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

Turing:

integers (i) := interger


Why not store it starting at the top and working your way down?

Turing:

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.

Turing:

integers (upper(integers) - i + 1) := integer


Now everything starts one higher.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: