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: 7,324
Joined: Dec 22 2002
Gold: 1,261.00
Oct 30 2015 10:29am
1) Karel should keep turning left until he is facing east. After that, he will turn left one more time. If you are facing east and you turn left, you will be facing north (a)

2) I don't know what a beeper is and what pickBeeper() does. If we assume that it doesn't affect where and how Karel moves, then we have the following:

part a: we will move forward one square (presumably) at a time, 6 times
part b: we will turn left
part c: we will repeat part a and b 4 times

So you end up walking in a big square, with each side being 6 grid squares long. You'll end up in the exact same spot where you started and facing the same direction.

3) method1 is just a repeat of part 2), but that's not really important. The important part is looking at where exactly the pickBeeper() call is in every method. For method1() it's in the inner-most loop, which means it will be called 6*4 = 24 times. In other words, we have a task that includes picking a beeper, we repeat that task 6 times as part of a bigger task, and we repeat that bigger task 4 times.
If that still doesn't make sense, think of it this way: let's say a bus fare costs $1. You have to take the bus to work and back every day (so 2 bus rides a day). You go to work 5 days a week. How much money will you spend on buses every week? As a nested loop like the ones you have, it would be represented like this:

Code
calculateWeeklyBusCost(){
iterate 5 times{
iterate 2 times{
payForBus();
}
}
}


so for method2, you only pick a beeper in the outer loop, so you'll end up with 6 beepers. You are in essence only picking up beepers at the corners of the big square that you are walking.
for method3, you are walking a tiny square of 1x1. So even though you are moving and picking beepers a lot of times (24), you'll only have 4 beepers at the end because you'll pick them all up and then you'll just keep going over the same empty grid.

This post was edited by russian on Oct 30 2015 10:31am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll