Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Steve's Programming Challenge: Weights and Measures
#22
And, since it's not right to issue a challenge without also filling it, I'd like to share this solution, which highlights the problem perfectly:

Code: (Select All)
Color 15
_Define A-Z As _FLOAT
Dim TargetWeight(1 To 4), TargetWeight$(1 To 4), I, CompareWeight, Fraction
TargetWeight(1) = 3: TargetWeight$(1) = "3"
TargetWeight(2) = 3 / 8: TargetWeight$(2) = "3 / 8"
TargetWeight(3) = 11 / 32: TargetWeight$(3) = "11 / 32"
TargetWeight(4) = 1 / 10: TargetWeight$(4) = "1 / 10"



For I = 1 To 4
CompareWeight = 4: Fraction = 1
Print TargetWeight$(I); " =";
Do
If CompareWeight < 1 Then Fraction = Fraction * 2
If CompareWeight <= TargetWeight(I) Then
TargetWeight(I) = TargetWeight(I) - CompareWeight
If Fraction <= 1 Then
Print " + "; CompareWeight;
Else
Print " + ( 1 /"; Fraction; ")";
End If
End If
CompareWeight = CompareWeight / 2
Loop Until TargetWeight(I) = 0 Or CompareWeight < 0.0000000000001##
If TargetWeight(I) > 0 Then
Color 4
Print ; " + "; TargetWeight(I); "OFF"
End If
Color 15
Print: Print
Next


Note that I've deliberately set this up to go to a much lower level of precision than just the 1/256 weights which I asked for with the first post. This is to show that even if we keep going deeper and deeper down the rabbit hole, some rabbits just aren't at home to catch to begin with!!
Reply


Messages In This Thread
RE: Steve's Programming Challenge: Weights and Measures - by SMcNeill - 08-16-2024, 07:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Steve is on vacation SMcNeill 9 966 11-06-2025, 06:55 PM
Last Post: TempodiBasic
  personal coding challenge results James D Jarvis 4 626 06-20-2025, 03:20 PM
Last Post: James D Jarvis
  QB64PE programming challenge? auto-convert image to photoreal etch-a-sketch drawing madscijr 9 1,856 02-14-2025, 05:49 PM
Last Post: madscijr
  For what it’s worth: Programming Clarity PhilOfPerth 11 2,251 07-27-2024, 03:15 PM
Last Post: bplus
  Vintage programming BigPete 4 892 05-15-2024, 09:57 AM
Last Post: BigPete

Forum Jump:


Users browsing this thread: 1 Guest(s)