1st year student: Tracing a program by hand

Click For Summary
SUMMARY

The discussion focuses on tracing a Fortran77 program fragment to determine the final values of variables n and m. The program initializes n and m to zero and uses nested loops to calculate their values based on the loop counters k and j. The final values of n and m after executing the loops are 6 and 6, respectively. The user is encouraged to complete the tracing by systematically evaluating each iteration of the loops for all values of k and j.

PREREQUISITES
  • Understanding of Fortran77 syntax and structure
  • Familiarity with nested loops and iteration concepts
  • Basic knowledge of variable initialization and arithmetic operations
  • Ability to manually trace program execution and variable changes
NEXT STEPS
  • Practice tracing additional Fortran77 code snippets to reinforce understanding
  • Learn about debugging techniques in Fortran77 to identify variable states
  • Explore the use of flowcharts to visualize program logic and control flow
  • Study the differences between Fortran77 and modern programming languages for better context
USEFUL FOR

First-year programming students, educators teaching Fortran77, and anyone interested in understanding program tracing and variable manipulation in procedural programming languages.

BrownianMan
Messages
133
Reaction score
0
Hey, new to the forum.

I'm currently taking my first programming course, and in one of the assignments, I was asked to trace the following Fortran77 program fragment by hand and determine the final values for n and m:

Code:
integer n, m, k, j
n = 0
m = 0
do k = 1, 3
    do j = k, 1, -1
        n = n + j
    end do
    m = m + k
end do
print*, n
print*, m

I was only able to get this far:

n m k j
0 0 1 1
1 1 2 0
3 3 3 2


How should I approach it from here?
 
Last edited:
Physics news on Phys.org
You are not considering all cases

(
k=1
j=1
)
m=?
n=?

(
k=2
j=2
)
m=?
n=?

(
k=2
j=1
)
m=?
n=?

(
k=3
j=3
)
m=?
n=?

(
k=3
j=2
)
m=?
n=?

(
k=3
j=1
)
m=?
n=?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
2
Views
2K
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K