Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Every time I see a program I really want to know how it works. For example
the...
C++
for(x=0; x<=2; x++)

tracing of the for loop
C++
x=0 0<=2 TRUE
x=1 1<=2 TRUE
x=2 2<=2 TRUE
x=3 3<=2 FALSE

There are some programs that I find it difficult to trace and how can I trace it even if it is hard to trace what I mean is how to determine the flow of the program?

Here's another program this time it is hard for me to trace each code in the program.
How can I understand this program with the use of pen and paper?
How can I determine the result of the program without using the compiler only pen and paper?
C++
#include<iostream.h>
main()
{
 int a[15]={5,10,15,20,25};
 int x,temp,size;
 size 5;

 for(x=0; x<=size/2; x++)
  {
   temp=a[x];
   a[x]=a[((size-1)-x)];
   a[((size-1)-x)]=temp;
  }
 for(x=0; x<=4; x++)
  {
   cout<<" ELEMENT "<<x<<a[x];
  }
}
Posted
Updated 3-Jul-14 3:26am
v2
Comments
joshrduncan2012 3-Jul-14 9:27am    
What's your question? This question seems really obscure. Have you looked at the program via the debugger and step through what it's doing?
[no name] 3-Jul-14 9:29am    
You are only going to do the pen and paper bit for very small bits of code. For this specific example, you would do it just like everything else. Write down what the values are line by line and note the changes.

You have to become yourself a debugger (instead of a compiler).That is: note all the variable and update their values after executing every instruction.
At first it is a clumsy activity, but after while you are going to follow mentally such a procedure (and able to skip unnecessary update steps).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jul-14 11:49am    
My 5.
I approached the question from a very different side, please see my answer.
—SA
Espen Harlinn 3-Jul-14 18:47pm    
5 :-)
I have a different advice: don't use so much effort to understand programs written by other people. This is programming. Try not wasting your time on that. Understanding comes when you write code; and the ability to read code well comes much later, at least in most of the beginners. Indeed, why would you need to understand how someone else's code works? To learn programming? This is not the best way. It's much better to learn by reading just the manuals and doing simple exercises, especially programming exercises from the manuals and textbooks, but not only them, eventually you need to learn to develop whole projects.

When and only when you become really confident in writing, you start understanding something other people write, and even that comes not at once. For important exercises, try to understand your own code written an year ago and more. If you have trouble getting into it (most developers have this trouble, especially with lesser experience). But if you have this trouble, you are not ready yet. It will come.

And of course, you can dig into code using the debuggers. Note that the need to dig into someone else's code does not come often, and it should not.

As you can see, I made an assumption that you are a beginner; it seems so from your question. Sorry if you think I'm mistaken. Still, hope my advice can be useful.

—SA
 
Share this answer
 
Comments
Espen Harlinn 3-Jul-14 18:47pm    
5 :-)
Sergey Alexandrovich Kryukov 3-Jul-14 20:47pm    
Thank you, Espen.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900