1st year student: Tracing a program by hand

AI Thread Summary
To trace the Fortran77 program fragment, start by analyzing the nested loops and how they affect the variables n and m. The outer loop iterates k from 1 to 3, while the inner loop decrements j from k to 1, adding j to n during each iteration. After completing the inner loop for each k, the value of k is added to m. By carefully calculating the contributions to n and m for each iteration, the final values can be determined as n = 6 and m = 6. Understanding the flow of the loops is crucial for accurately tracing the program.
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
7
Views
2K
Replies
10
Views
3K
Replies
10
Views
2K
Replies
21
Views
3K
Replies
9
Views
4K
Replies
37
Views
4K
Back
Top