# Need help with Fortran tracing

• Comp Sci
• suezxc6
In summary, the code in question creates an array of values, assigns values to each element, and prints out the results.f

#### suezxc6

Homework Statement
Can someone please explain how this is done?
Relevant Equations
.
Code:
      integer n, ar(12)
ar(1) = 1
ar(2) = 2
do n=1, 10
ar(n+2) = ar(n+1) + ar(n)
end do
do n=0,3
print*, ar(12-3*n)
end do
print*, ar(ar(ar(4))-1)
end

Last edited:
What do you mean "how it is done"? Do you mean to ask what it is doing? Since it is a homework problem, I am only allowed to give guiding hints. And you have to show some work.

Last edited:
Then can you please introduce me to materials explaining tracing and how to do it? I really am stuck.

if you are using an IDE, run the code in debug mode. The old fashioned way to trace is to put print statements and either print to a file or the screen.

When i run it into terminal I get the values 233, 55, 13, 3, 21. I mainly want to know how i would get those numbers if i wrote it by hand. The material that i have does not properly explain how i would be able to achieve it.

write it out methodically.

ar(1) = 1
ar(2) = 2

write out the for loop
n = 1 yields ar(3) = ar(2) + ar(1) which is 2 + 1 = 3
n = 2 yields ar(4) = ar(3) + ar(2) which is 3 + 2 = 5
etc...

the write statement is as little more complicated, but doable given what I showed above

Fortran:
      integer n, ar(12)
ar(1) = 1
ar(2) = 2
do n=1, 10
ar(n+2) = ar(n+1) + ar(n)
end do
do n=0,3
print*, ar(12-3*n)
end do
print*, ar(ar(ar(4))-1)
end
Here's how you can hand-trace what the program does for the first half, up through the first do loop. Take a piece of paper and a pencil and write on it a box for n and a series of 12 connected boxes for the array ar.
The first two assignment statements store 1 in ar(1) and ar(2), respectively, so fill in the first two boxes of the array with those values.
In the first do loop, n starts off with the value 1. c
The first end do statement cause n to be incremented to 2. Since 2 is less than or equal to 10, the loop body executes again. What are the values of the indexes n + 2, n + 1, and n this time? Which array element gets set?
Continue with this process until the first do loop finishes, and proceed the same way with the second do loop.

• hmmm27
Thanks i got it!