d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Computer Sciences Homework > Help Pl0x
Add Reply New Topic New Poll
Member
Posts: 4,114
Joined: May 9 2008
Gold: 0.00
Oct 29 2015 08:12pm
Please help with these 3 question that I can't for the life of me figure out. With explanations obviously.


1) Karel robot is facing north.
------------------------------------
while ( !karel.facingEast() )
{
karel.turnLeft();
}
karel.turnLeft();
-----------------------------------
Where will Karel robot be facing after performing the above task?
a) North
b ) South
c) East
d) West
e) Infinite loop

2) Considering there are no walls in the grid and a beeper in every crossing, where will Karel robot be after completing this task, considering he is currently facing north at (10,10).
-----------------------------------
void method1(){
iterate 4 times{
iterate 6 times{
move();
pickBeeper();
}
turnLeft();
}
}
----------------------------------
a) (16,10)
b ) (10,10)
c) (4,10)
d) (10,16)
e) None of the above

3) Considering there are no walls in the grid and a beeper in every crossing, and the robot is facing north at (10,10), how many beepers will he have in his beeper bag after completing each method seperately?
----------------------------------
void method1(){
iterate 4 times{
iterate 6 times{
move();
pickBeeper();
}
turnLeft();
}
}

void method2(){
iterate 6 times{
iterate 4 times
move();
turnLeft();
}
pickBeeper();
}

void method3(){
iterate 24 times{
move();
pickBeeper();
turnLeft();
}
}\
-----------------------------
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Oct 29 2015 08:51pm
This is my interpretation of what happens for #1.

Code

while ( !karel.facingEast() ) {
karel.turnLeft();
}
karel.turnLeft();
}


Karel is facing north.
While loop condition met, Karel turns left.

Karel is now facing West.
The while loop condition is checked again.
Condition still met, Karel turns left.

Karel is now facing South
The while loop condition is checked again.
Condition still met, Karel turns left.

Karel is now facing East.
The while loop condition is checked again.
WHILE LOOP ENDS

Karel turns left.
Karel is facing North.
Member
Posts: 4,114
Joined: May 9 2008
Gold: 0.00
Oct 29 2015 09:35pm
Quote (Noobtard @ Oct 29 2015 10:51pm)
This is my interpretation of what happens for #1.

Code
while ( !karel.facingEast() ) {
karel.turnLeft();
}
karel.turnLeft();
}


Karel is facing north.
While loop condition met, Karel turns left.

Karel is now facing West.
The while loop condition is checked again.
Condition still met, Karel turns left.

Karel is now facing South
The while loop condition is checked again.
Condition still met, Karel turns left.

Karel is now facing East.
The while loop condition is checked again.
WHILE LOOP ENDS

Karel turns left.
Karel is facing North.


Excuse my ignorance but why did you make him turn left 4 times?
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Oct 29 2015 10:20pm
Code
while ( !karel.facingEast() ) {
karel.turnLeft();
}


How this while loop works:
This while loop will run the {code} in the brackets until Karel is facing East. (That's what the first line reads)

So when Karel is NOT facing East run the {code}. Once the code inside the brackets is finished running re-evaluate if he is facing east.(loops back to the start of the while loop - line 1)
It will continue to loop and execute the {code} until Karel faces East.

Why I made him turn 4 times
I made him turn 3 times as a result of the while condition being met.
The last turn is because once the while loop finished there is another karel.turnLeft(); - which happens after the while loop ends.


I hope this helped. Heading to bed.

If this still doesn't make sense google a 'while loop tutorial' - might make more sense.
Member
Posts: 4,114
Joined: May 9 2008
Gold: 0.00
Oct 29 2015 10:44pm
Thanks buddy!
Member
Posts: 29,352
Joined: Mar 27 2008
Gold: 504.69
Oct 30 2015 09:06am
Quote (Noobtard @ Oct 29 2015 10:51pm)
This is my interpretation of what happens for #1.

Code
while ( !karel.facingEast() ) {
karel.turnLeft();
}
karel.turnLeft();
}


Karel is facing north.
While loop condition met, Karel turns left.

Karel is now facing West.
The while loop condition is checked again.
Condition still met, Karel turns left.

Karel is now facing South
The while loop condition is checked again.
Condition still met, Karel turns left.

Karel is now facing East.
The while loop condition is checked again.
WHILE LOOP ENDS

Karel turns left.
Karel is facing North.


I concur.

As for the other two I'm not sure what exactly it means iterate 4 times.

This post was edited by ROM on Oct 30 2015 09:07am
Member
Posts: 5,056
Joined: Jun 22 2010
Gold: 28,009.00
Oct 30 2015 02:42pm
Quote (ROM @ Oct 30 2015 08:06am)
I concur.

As for the other two I'm not sure what exactly it means iterate 4 times.


It looks like the code is pseudo-code, not an actual programming language. As such, "iterate 4 times { .... }" is equal to " for(x=0; x<4; x++) { .... } "

Basically it means, "run this loop X times"

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll