Math - Inequalities - Challenge

In this lecture we learnt how to manipulate inequalities.

For your challenge, our designer wants to know how many times the player can refuel and still afford an upgrade at the end of the level.

We know that:

  • Pickups are worth 5 coins each
  • There are 30 pickups in a level
  • Refueling costs 10 coins
  • Upgrades cost 100 coins

For a hint, we can express our problem as:
(30 x 5) - (10n) >= 100

So we need to rearrange and solve for n.

Post your answers below and remember to use the spoiler tags.

Summary

float pickup = 30 * 5;
float refuel = 10;
float upgrade = 100;
float n;

n <= (upgrade - pickup) / -refuel;

n <= 5

1 Like

(30 * 5) - 10n >= 100

150 - 10n - 100 >= 0
150 - 100 >= 10n
50/10 >= n
n <= 5

1 Like

With the equation (30*5) - (10n) >= 100

30*5 = 150 so 150-10n >= 100

-150 + 150 - 10n = 100-150 so -10n >= -50

-10n/-10 >= -50/-10 so n <= -50/-10

or

n <= 5

1 Like

So I got no more than 5 refuels and it was pretty easy solving it, however I think the hardest part of this, you actually did for us, namely expressing the whole thing as an inequality. As soon as you explained the problem, I sort of calculated it intuitively but I wasn’t thinking iun terms of inequalities.

I think it would be good to learn how to express these problems formally in math language for when more complex problems that are not so cognitively intuitive come along.

My take away from this lesson is that we expressed the >= sign as “has got to be less or equal to”, in other words we formulate maths statements to require something rather than to discover (in this case at least)

2 Likes

@olidadda, one of the hardest parts of maths is converting your word problem into math terms.
Hopefully me talking through the problem helps you get a better sense of the thought process that goes into getting from one to the other.

Rather that just drop you in at the deep end I do like to give you a helping hand where I can in these early stages of the course, but that help does start to turn into smaller and smaller hints as we move through the course.

And it’s great to hear you got an intuitive feel for the answer before actually solving it.
That’s a good skill to develop and will help you a great deal in the long run.

In your last paragraph though, >= is “greater or equal to” (I think you meant <=).
You’re general thought process seems sound though. Inequalities deal with ranges of possible answers, so they come up a lot in programming - especially for things like if-statements.

2 Likes

Writing it out in full to get into the practice of making sure I have a verbal understanding of what I’m doing:

The player needs to be able to afford an upgrade at the end of the level. Upgrades cost 100 coins. Refueling in the level costs 10 coins. The player can pick up 30 coins in the level, for a total of 150 coins. How many times can the player refuel before they can no longer afford an upgrade?

Let n = number of refuels.

(30 [coins] x 5 [coin value]) - 10n[refuels] >= 100 [upgrade cost]

150 - 10n >= 100

150 >= 100 + 10n

50 >= 10n

5 >= n

n <= 5 : The player can refuel a maximum of 5 times before they can no longer afford an upgrade.

2 Likes

I like the problem example of have fuel for a space ship.

c = m - (b* t)

and having c a range from 0 .. m(100)

I find the evaluation code a bit odd.

if ( 0 ≤ c & c ≤ m(100) ) … has fuel ?

Because c can also be 0, no fuel. I would expect 0 < c
As a developer I would use

if ( 0 < c ) … has fuel ?

I think I got it right… I hope

1 Like

@FedPete,
It really depends on the problem you’re trying to solve.

If you just want to keep the current fuel within the range of what the tank can hold then if(0 ≤ currentFuel && currentFuel ≤ maxFuel) would make sense.
You don’t want to continue burning fuel once there’s none left and you don’t want pickups to increase the value beyond the maximum value.

If you were checking whether the ship actually has enough fuel to fly then if(currentFuel > 0) would be the right way to go. If there’s no fuel then you can’t fly and the maximum fuel limit doesn’t really factor into the equation, since that should be managed by other code.

So both options have their uses, and if you’re writing a fuel management script then you’d probably use both in different places.

2 Likes

Looks good to me @KhaledAhmedYounes. Nice work!

1 Like

(30∗5)−(10n)≥100
150 −10n≥100
−10n≥−50
n≤5

1 Like

n <= 5

1 Like

This is my answer:

(30 * 5) - (10n) >= 100
(150) - (10n) - 100 >= 0
50 >= 10n
50/10 >= n
n <= 5

1 Like