How Does This Fortran Code Generate and Output Its Array Values?

In summary, the code in question creates an array of values, assigns values to each element, and prints out the results.
  • #1
suezxc6
11
0
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:
Physics news on Phys.org
  • #2
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:
  • #3
Then can you please introduce me to materials explaining tracing and how to do it? I really am stuck.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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
 
  • #7
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.
 
  • Like
Likes hmmm27
  • #8
Thanks i got it!
 

1. What is Fortran tracing?

Fortran tracing is a method used to debug and analyze programs written in the Fortran programming language. It involves tracing the execution of a program and monitoring the values of variables and statements at different points in the code.

2. Why is Fortran tracing important?

Fortran tracing allows programmers to identify and fix errors in their code, as well as understand the flow of the program and how different variables and statements interact with each other. This can help improve the efficiency and accuracy of the program.

3. How do I perform Fortran tracing?

To perform Fortran tracing, you can use a debugging tool or a tracing library specifically designed for Fortran. These tools allow you to set breakpoints, monitor variable values, and step through the code to trace the execution of the program.

4. What are some common errors that can be identified through Fortran tracing?

Some common errors that can be identified through Fortran tracing include logical errors, such as incorrect variable values or incorrect use of conditional statements, as well as runtime errors, such as division by zero or out-of-bounds array access.

5. Are there any tips for effectively using Fortran tracing?

Yes, here are a few tips for effectively using Fortran tracing:
- Start with a small section of code to trace, and gradually increase the complexity as you become more familiar with the tracing process.
- Use descriptive variable names and comments in your code to make it easier to understand and trace.
- Take notes and document any changes you make to the code during the tracing process, as this can help you keep track of your progress and identify any potential errors or improvements in the code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
828
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
889
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Back
Top