This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic pseudocode trace through question  RSS feed

 
Ching Ming
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trace through the pseudocode identified below. What will be printed? 

SET counter = 0 SET distribution = 500 DOWHILE distribution > 200 AND counter < 5    SET giveaway = distribution * 0.5    PRINT giveaway    SET distribution = distribution - giveaway    SET counter = counter + 1 ENDDO

I think the answers is 250 in giveaway. Giveaway is the only thing printed and its just distribution *.5. Is that correct?
 
Carey Brown
Bartender
Posts: 2989
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a 10 line Java program and see what you get. This would also be good practice.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ching Ming wrote:I think the answers is 250 in giveaway. Giveaway is the only thing printed and its just distribution *.5. Is that correct?
Are you sure the final answer of giveaway is 250?

Did you see when you calculate giveaway = distribution * 0.5, It means 500 * 0.5 so giveaway becomes 250 then you deducted giveaway from distribution, distribution = distribution - giveaway so  distribution becomes 250 don't you think loop will get executed once again as distribution > 200 i.e. 250 > 200  and counter i.e. 1 < 5?

 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dry run using a pencil and ruff page will help you solve this ...
 
Ching Ming
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first time I run the loop through I get 250 the second time I get 125 which is not greater than 200. I am still confused.
 
Junilu Lacar
Sheriff
Posts: 11132
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ching Ming wrote:The first time I run the loop through I get 250 the second time I get 125 which is not greater than 200. I am still confused.


It might help if you tried walking through the operations manually. Remember, the sequence of execution will go from top to bottom unless there is a loop structure that makes you go back and repeat previous operations.  Also, the DOWHILE checks if distribution is greater than 200. The PRINT displays the value of giveaway, then a SET changes the value of distribution.  So ask yourself, was the value of distribution changed before or after the check for > 200 made?

I hope you're not under the impression that the check happens all throughout the execution of statements that are inside the DOWHILE loop. It does not.  When execution gets to the DOWHILE on line 4 the condition is checked. If it is true, then lines 6-9 are executed. At the ENDDO, the execution goes back to line 4 where the condition is once again checked. This is the only point of execution when the DOWHILE condition is checked again.  If the condition is still true, the body of the loop will be executed again. Upon reaching the ENDDO, execution will again go back up to line 4, where the condition is checked yet again. The loop body will continue to be executed until the loop condition evaluates to false. Then execution will skip the loop body and continue after the ENDDO.
 
Ching Ming
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so does the count after going through the first loop start at 1 or 0
 
Carey Brown
Bartender
Posts: 2989
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It starts at 0 per line 1, and gets incremented when line 9 is reached.
 
Ching Ming
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the counter through 4 times and this is what I got but I don't think I am solving the problem correctly.
 
Ching Ming
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the code through 4 times this  is what I got. I dont think i did it correctly.
500 * .5 = 250
Counter is 1
250 * .5 = 125
Counter is 2
125 * .5 = 62.5
Counter is 3
62.5 *.5 = 31.25
Counter Is 4
 
Junilu Lacar
Sheriff
Posts: 11132
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say you "ran the code 4 times" do you mean you wrote some Java code and ran it?  Can you show us your code?
 
Junilu Lacar
Sheriff
Posts: 11132
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the critical piece to understand how this algorithm works is the AND part.  The loop condition will be true only if both expressions on either side of the AND are true.  In Java code, you use the && operator to implement that logic. So, if one of the conditions that are joined with AND is false, then the entire boolean expression is false and the loop will terminate immediately.
 
Ching Ming
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my point of view I think this loop can only be run one time. Because times 500 by its half will only be greater than 200 once because 250 *.5 = 125.
 
Junilu Lacar
Sheriff
Posts: 11132
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ching Ming wrote:From my point of view I think this loop can only be run one time. Because times 500 by its half will only be greater than 200 once because 250 *.5 = 125.

Then you still don't understand how it works.  Walk through the algorithm as though you are the computer. Perform each operation in the order that they will be performed by the computer. Don't think about anything but the instruction and follow it blindly. That's what computers do. If you think too much and inject your own interpretation, you will be wrong.
 
Carey Brown
Bartender
Posts: 2989
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the values for distribution and counter each time line 4 (the loop test) is visited? Use pencil and paper.
 
Junilu Lacar
Sheriff
Posts: 11132
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is how it starts (it's best if you write each step and result down as you trace through it):

Line 1 - counter is 0
Line 2 - distribution is 500
Line 4 - is distribution > 200 AND counter < 5 ? Yes, execute loop from line 6
Line 6 - giveaway is 500 * 0.5 ==> 250
Line 7 - print out giveaway, which is 250
Line 8 - distribution becomes 500 - 250 ==> 250
Line 9 - counter is now 1
Line 11 - go back to line 4
Line 4 - is distribution > 200 AND counter < 5 ? (answer?), (what next?)
... and so on
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ching Ming wrote:From my point of view I think this loop can only be run one time.

You got tired probably. Take some rest and come back to it.

Initial question is: "What will be printed?"

So, during the very first iteration, 500 is greater than 200 and counter is less than 5, right? Right. So, based on the algorithm you got, boolean condition being satisfied and we dive inside the block of do while procedure.

Inside of it we find some calculation line, which gives a result...
  • So this result gets printed


  • So, do some other calculations and start the procedure again. What we end up with after the first iteration? With 250, you just said that yourself. So, again, 250 is greater than 200 and counter is less than 5, right? Right. Based on the algorithm you got (deja vu), boolean condition being satisfied and we dive inside the block of do while procedure.

    Inside of it we find some calculation line, which gives a result...
  • So this result gets printed

  • So, do some other calculation and start the procedure again. What we end up with after the second interation? With 125, you just said that yourself. So, again, 125 is greater than 200? What? No!

    Remember what Junilu said? In order for the condition to be true when the AND logical operator is involved, both of the operands need to be true.

    Are you going to dive 3rd time to the do while procedure?

    A bit just to note for you, have you noticed that in this particular scenario second line is a bit redundant if in the first line we'd change giveaway with distribution.
    However, don't slip on such cases when you trying to simplify calculations. Simplification would work in this case as we're talking about halves. If there would be a one third (1/3), that would shape your calculations wrongly.
     
    Ching Ming
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok I think I got the most the loop can run is twice because after the second time the distribution is 125. So after first loop distribution is 250 and counter is at 1 and after second loop distribution is 125 and counter is at 2. I don't think you can go farther than that. 
     
    Junilu Lacar
    Sheriff
    Posts: 11132
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ching Ming wrote:Ok I think I got the most the loop can run is twice because after the second time the distribution is 125. So after first loop distribution is 250 and counter is at 1 and after second loop distribution is 125 and counter is at 2. I don't think you can go farther than that. 

    For what it's worth, that correct. You really should go through the exercise as I illustrated so that you're not as tentative as your response seems to suggest.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!