Jump to content

1st year student: Tracing a program by hand

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
BrownianMan

BrownianMan

    Newbie

  • Members
  • Pip
  • 2 posts
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:

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?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would slow down. For each row, only update one variable. You may want to also indicate the line that updated the variable. You cannot have two variables updated at once.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
BrownianMan

BrownianMan

    Newbie

  • Members
  • Pip
  • 2 posts
What happens when k becomes higher than 3 though? Shouldn't it exit the loop?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
n=n+j gets processed 6 times. You've skipped some executions.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog