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



Users browsing this thread: 33 Guest(s)