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

  • Context: Comp Sci 
  • Thread starter Thread starter suezxc6
  • Start date Start date
  • Tags Tags
    Fortran tracing
Click For Summary

Discussion Overview

The discussion revolves around understanding a Fortran code snippet that generates and outputs values from an array. Participants explore how to trace the code execution and derive the output values manually, focusing on the logic behind the loops and array assignments.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant seeks clarification on what the code is doing, indicating it is a homework problem and expressing a need for guidance.
  • Another participant suggests using an IDE in debug mode or adding print statements to trace the code execution.
  • A participant shares their output values from running the code and expresses a desire to understand how to derive those values manually.
  • One response outlines a methodical approach to hand-tracing the code, detailing how to fill in the array values step by step through the loop iterations.
  • Another participant confirms their understanding after receiving guidance on how to trace the program.

Areas of Agreement / Disagreement

Participants generally agree on the need for tracing the code to understand its output, but there is no consensus on a single method or approach to achieve this understanding. Multiple perspectives on tracing techniques and explanations are presented.

Contextual Notes

Some participants mention limitations in their understanding of tracing and the materials available to them, indicating a potential gap in foundational knowledge regarding the code execution process.

Who May Find This Useful

Students learning Fortran programming, individuals seeking to improve their debugging skills, and those interested in understanding array manipulations in programming may find this discussion beneficial.

suezxc6
Messages
11
Reaction score
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
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.
 
  • Like
Likes   Reactions: hmmm27
Thanks i got it!
 

Similar threads

Replies
7
Views
3K
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 7 ·
Replies
7
Views
16K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 37 ·
2
Replies
37
Views
5K