
-----------------------------------
Jagoff
Fri Oct 03, 2008 9:10 am

Reversing An Array
-----------------------------------
I have a program I have to write for a class, and I have to make an array run normally, then make it run backwards.

Example:
1 2 3 4 5 6...100
-Then-
100 99 98 97 96...1

The main thing giving me problems is the array is taken from a text file saved on the drive and I have to make the elements of the text file go forwards then reverse. Any ideas?

Here's my 
var fin, Index, client : int  % file stream number
var Lname, Fname : array 1 .. 100 of string (20)
open : fin, "namelist.txt", get                 % open file for get
client := 1
loop
    Index := 1
    get : fin, Lname (Index), Fname (Index)  % get values from file
    put client, " ", Fname (Index), " ", Lname (Index)
    Index += 1
    client += 1
    exit when hasch
end loop
delay (1000)
close : fin



Thanks!

-----------------------------------
CodeMonkey2000
Fri Oct 03, 2008 9:15 am

RE:Reversing An Array
-----------------------------------
Why not just get the information first, then output in reverse order using a for loop. And you should use a for loop to get input.

-----------------------------------
Zeroth
Fri Oct 03, 2008 9:17 am

Re: Reversing An Array
-----------------------------------
What he means is you should load all the stuff into an array first, then output it forwards and backwards.

-----------------------------------
Jagoff
Fri Oct 03, 2008 9:25 am

Re: Reversing An Array
-----------------------------------
The information I'm getting from the text file has multiple lines, and I need to read each one separately and apply a number to each one.

This is what my teacher wants:

1 ExampleI
2 ExampleII
3 ExampleIII
4 ExampleIV
5 ExampleV

-then-

5 ExampleV
4 ExampleIV
3 ExampleIII
2 ExampleII
1 ExampleI


But every examplex is a line from the file. Which there is two arrays for a First name and Last name. If it's hard to understand what I mean I'll explain it more in depth, I'm bad at explaining. :]

-----------------------------------
Zeroth
Fri Oct 03, 2008 9:29 am

Re: Reversing An Array
-----------------------------------
Look at what I wrote. Thats how you do it. The numbers can be outputted dynamically.

-----------------------------------
Insectoid
Fri Oct 03, 2008 1:41 pm

RE:Reversing An Array
-----------------------------------
Load them into arrays. Use a for loop to display forwards. Use a reverse for loop to display backwards. 

reverse for loop:


for decreasing x: 10..1
    %do stuff
end for


Here's a hint: the 'x' int the for loop can be substituted into the index/subscript of the array.

-----------------------------------
Nova
Tue Oct 07, 2008 12:01 pm

Re: Reversing An Array
-----------------------------------
If you really want to reverse the array, you'll need to do some juggling.
You'll need a temporary location to store a value and have a for loop in the following manner
Not exact, I haven't checked if the code is perfect, but its this kind of idea

var temp : int
var yourArray : array 1..* of int /pretend its already got all its values loaded/

for decreasing i : upper(yourArray)..upper(yourArray div 2)
    temp := yourArray(i)
    yourArray(upper(yourArray)-i)+1 := yourArray(i)
    yourArray := temp
end for


-----------------------------------
S_Grimm
Tue Oct 07, 2008 2:51 pm

RE:Reversing An Array
-----------------------------------
a reverse array can be made with 
for decreasing.
