1st year student: Tracing a program by hand

In summary, the conversation discusses a programming assignment involving tracing a Fortran77 program fragment and determining the final values for n and m. The person asking for help has only been able to trace part of the program and is seeking guidance on how to approach the remaining cases.
  • #1
BrownianMan
134
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
  • #2
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=?
 
  • #3


Hello, welcome to the forum! It's great to hear that you are taking your first programming course and are already tackling assignments like this. Tracing a program by hand is a valuable skill that can help you understand the flow of a program and identify any potential errors.

In this case, you have correctly traced the first two iterations of the outer loop (k = 1 and k = 2). To continue, you will need to go back to the beginning of the inner loop (j = k) and trace it for the third iteration of the outer loop (k = 3).

For k = 3, the inner loop will start with j = 3 and count down to 1 (since it is a negative increment). So, for the third iteration, the values of n and j will be 6 and 3 respectively. Then, when the outer loop completes, the final values of n and m will be 9 and 6.

I hope this helps guide you in tracing the program further. Keep practicing and don't hesitate to ask for help if you get stuck. Good luck with your programming journey!
 

Related to 1st year student: Tracing a program by hand

1. What is the purpose of tracing a program by hand?

Tracing a program by hand is a helpful exercise for first year students to gain a better understanding of how a program works. It involves following the logic and flow of a program step by step, allowing students to identify errors and understand the program's overall structure.

2. How do I start tracing a program by hand?

The first step is to carefully read and understand the program's instructions and any given inputs. Then, you can begin to follow the program's logic by manually writing down each step and tracking the values of variables as they change.

3. What is the benefit of tracing a program by hand instead of using a debugger?

Tracing a program by hand requires students to actively engage with the code and think critically about each step, leading to a deeper understanding of the program. Debuggers can be helpful tools, but they can also make it easier to overlook important details.

4. How can I check if my hand-tracing is correct?

Once you have completed tracing the program, you can compare your hand-traced steps to the actual output of the program. If they match, then your tracing is likely correct. You can also double check with a classmate or instructor for confirmation.

5. Is tracing a program by hand only for beginners?

No, tracing a program by hand can be a useful practice for programmers of all levels. It can help with debugging complex programs and improve understanding of new programming languages or concepts. Experienced programmers may also use this technique to teach others or to refresh their own skills.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
840
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top