Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Steve's Programming Challenge: Weights and Measures
#1
So here's a challenge which I'd like to propose for all of our coders here to undertake -- particularly @TerryRitchie @bplus @Pete @Dav , but which I'd truly LOVE to see everyone tackle.

The program concept is very simple -- Write a program that simulates you being a merchant in ancient times.  You have a whole bunch of lead weights, which you can use to measure out how much a customer's gold nugget weighs.

Your available weights are all in the power of two -- you have:

1 four-pound weight.
1 two-pound weight.
1 one-pound weight.
1 half-pound weight.
1 quarter-pound weight.
1 1/8-pound weight.
And so on to give you one of each weight from 4 pounds to 1/256 pounds.

Now the task is to figure out what weights are required to properly and perfectly weigh the following:

A 3 pound gold nugget
A 3/8 pound gold nugget
A 11/32 pound gold nugget
A 1/10 pound gold nugget

Now the first solution is obviously 1 2-pound weight + 1 1-pound weight. 
The question is -- can you find the proper combination for the other three nuggets I've mentioned??



Complete this task, and I doubt you guys will ever have any question ever again in the future about how floating point numbers work.  As you're some of our most prolific posters (and teachers) here, I would hope a simple task like this would be enough to help you guys understand the underlying issue with floating point numbers, so when people ask in the future, you'll be able to explain it to them better.  Wink
Reply


Messages In This Thread
Steve's Programming Challenge: Weights and Measures - by SMcNeill - 08-15-2024, 11:15 PM



Users browsing this thread: 1 Guest(s)