Hey I’m at Remainder leasson, but I could not understand Turn example, I understand remainder is what is left over but I dont understand the turn example so well and how to use remainders at programming, getting odd and even is usefull and knowing what is left over.

But I dont get what more I can do like there’s 10 swords and 6 knights, but each knight can hold 2 swords, so 1 knight is left, I dont see how to use remainder here. 10%6=4

Finding the remainder is great for things like turn timers in a game.

Imagine players are given 30 seconds to complete their turn before things move on to the next player.

One way to do this would be to store a variable for the turn timer, count down the 30 seconds, and then reset that variable when it moves onto the next player.

By using a remainder, we can instead query the game clock to see how long the game has been running and then find the time%30 to see how far the player is into their turn.

For instance, let’s say that the game had been running for 342 seconds and the first players turn started when the clock was 0.

Using the remainder we get 342/30 = 11 r12.

This means the current player is 12 seconds into their turn.

We can take this further and also say that it’s currently player two’s turn, since 11%2 = 5 r1 (they take their turn when the remainder is 1)

And we can even keep going to say that they are on their 6th turn of the game (the whole number represents the zero-indexed turn counter).

In the example of your knights and swords, 10%6 = 1 r4, so every knight gets 1 sword and their are 4 swords left unused.

However, if every knight can hold 2 swords, then you actually want 10%(6 * 2) = 0 r10.

In this case, the divisor is greater than the dividend, meaning that 0 knights get two swords but we still handed out 10 swords to the knights.

From there you could work out that 5 knights got 2 swords, because 10/2 = 5.

I hope that helps.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.