Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is this 8?

 
Vivienne Ryan
Greenhorn
Posts: 12
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there came across this piece of code in a test:


why is the result 8?
My source is from a test I got in class. Our instructor just printed out the questions on a sheet, no idea where they come from sorry.
 
Henry Wong
author
Marshal
Pie
Posts: 20998
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is rather elementary if you know how to read the code. I am suspecting that your instructor wants you to learn how to read code. So, I'm goi9g to give you tips on how to read code rather than spoon feed the answer.

I suggest that you take a piece of paper, and go over the code line by line and print out the state of each variable as you go over each line. When you are inside the loops, go through each iteration of the loop one by one. IOW, pretend you are the computer and execute the statement on paper.

I would start off like this

Line 3: c=0
Line 4: start of A loop; i = 0, c=0
Line 5: start of B loop; i = 0, j=0, c = 0
Line 6: start of C loop; i = 0, j = 0, k = 0, c = 0
Line 7: c++; i = 0, j = 0, k = 0, c = 1
Line 8: is (K > j) .. no, continue; i = 0, j = 0, k = 0, c = 1
Line 9: end of C loop, jump to line 6; i = 0, j = 0, k = 0, c = 1
Line 6: next iteration of C loop, k++ means k=1. is k<2... yes so loop is not complete; i = 0, j = 0, k = 1, c = 1

and so on

You will have to go through each step meticulously.
 
Vivienne Ryan
Greenhorn
Posts: 12
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I break, do I then break out of C: or do I break out to the very first for?
 
Vivienne Ryan
Greenhorn
Posts: 12
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:The answer is rather elementary if you know how to read the code. I am suspecting that your instructor wants you to learn how to read code. So, I'm goi9g to give you tips on how to read code rather than spoon feed the answer.



Thank you very much! You have just helped me to fully understand what the code is supposed to do!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope it is not too late to welcome you as a new ranch member

You will find practice exams have that sort of code in, which would get you the sack if you produced it in real life. They tend to put features in to confuse you, e.g. incorrect indentation. At least you have correct indentation, but that code has at least three red herrings in.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivienne Ryan wrote:when I break, do I then break out of C: or do I break out to the very first for?
Out of the innermost loop only.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivienne Ryan wrote:
Jayesh A Lalwani wrote:The answer is rather elementary if you know how to read the code. I am suspecting that your instructor wants you to learn how to read code. So, I'm goi9g to give you tips on how to read code rather than spoon feed the answer.



Thank you very much! You have just helped me to fully understand what the code is supposed to do!


You are welcome. Actually, what I showed you is the secret sauce to understanding every program in existence. You can figure out what a program does by just reading it line by line. It's difficult initially, but when you get the hang of it, you can read it much faster. TO draw an analogy, when kids first learn to read, thyey read word by wrod, then string the words together to decode the meaning in the sentence. As they practice more, they don't need to "read" the whole word to understand the meaning. Then, you start learing speed reading:- You don't need to read the whole sentence to understand it (until you reach a difficult or poorly worded sentence). Reading code is just like that. You start of going line by line, but eventually, as long as the code follows convention, and is well written, you can read it much faster.

Vivienne Ryan wrote:when I break, do I then break out of C: or do I break out to the very first for?


You break out of the loop you are in. So, in this case, it's the innermost loop.

Look at the condition in the break statement and compare it with your loop variables. You will see what Campbell is saying about a "red herring"
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote: . . . You will see what Campbell is saying about a "red herring"
I didn't say, “red herring”.

I said, “red herrings”.
Challenge to Vivienne Ryan: count the red herrings.
 
Vivienne Ryan
Greenhorn
Posts: 12
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Jayesh A Lalwani wrote: . . . You will see what Campbell is saying about a "red herring"
I didn't say, “red herring”.

I said, “red herrings”.
Challenge to Vivienne Ryan: count the red herrings.


Will do!
Right now though I am celebrating passing the Programmer I exam
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic