Sunday, July 12, 2009

C programming language: Need help understanding the output of a program?

The program is:





main()


{ char i,j,k;


for (i='b'; i%26lt;='d'; i++)


for (j='c'; j%26lt;=i; j++)


for (k=j; k%26lt;=i; k++)


printf("%c%c%c\n",i,j,k);


printf("%c%c%c\n",i,j,k);


}








And the output for the program is:





ccc


dcc


dcd


ddd


eee





I’ve tried to figure out how this program results in the above output by using a diagram for the variables i, j, and k, but everything I have tried to has lead to a different output starting with the second line (dcc). The idea here is to (correctly) predict the output of a program such as this one before running it through a complier, so how do you go about predicting a program such as this one? Please explain how to do this step-by-step and line-by-line.

C programming language: Need help understanding the output of a program?
The first thing to be aware of is that you are dealing with the ASCII values of the characters. They are only numbers. They are only converted into characters when printed.


In C, 'A' is the same as 65.


int x = 'A';


int y = 65;


printf("%c%c", x, y); will print AA.


printf("%d %d", x, y); will print 65 65


'a' is 97


'b' is 98


'c' is 99 and so on.





for (i='b'; i%26lt;='d'; i++)


... means to count from the ascii value of 'b' to 'd'.


This is the same as if you did the following:


for(i=98; i%26lt;=100;i++)





for (j='c'; j%26lt;=i; j++)


... means to count from ascii value of 'c' to the ascii value of the counter of the outer loop.





for (k=j; k%26lt;=i; k++)


... means to count from j to i (loop2 counter to loop1 counter)





printf("%c", i); will print the character corresponding to the ascii value of i.





Therefore, the line:


printf("%c%c%c\n",i,j,k); will print the characters corresponding to the values in i,j,k.





Also note the following:


for (k=j; k%26lt;=i; k++)


printf("%c%c%c\n",i,j,k);


printf("%c%c%c\n",i,j,k);


The first printf statement is in the for loop, the second one is not.


All that you need to do is track the values of the loop counters through the loops and figure out what the characters for those are.


That should do it.


-t
Reply:If you want to know how these program is executed then u put the watch for i,j,k and execute the program step by step . u will get to know how program is executing and why ur getting output like these.
Reply:You have "play computer" as a fellow told me when I was learning how to program in college, back in the '60's (yes, you read that right).





Step through the code by hand and list each variable when it's defined, with its value. As each changes, cross out the old value and write in the new. Evaluate each test with the latest values, as the computer will.





In this program, I'd be careful to note which times the printf statement might NOT be executed.





Hope that helps.
Reply:It been awhile but looking at how this is written I think you need to nest the three for statements which you have not. Also change the printf statements to get full use of loops.


The program as it stands is printing junk and without proper parenthesis, you can’t get a meaningful output.

song downloads

No comments:

Post a Comment