Solved

# Give this a shot...

Posted on 2000-03-06
252 Views
Back in my senior year of high school my shop teacher was offering extra credit for anyone that could come up with some code (Commodore 128 at the time) to answer his challenge.  Well, I graduated over 10 years ago, now play with Visual Basic 6, and his challenge still bothers me.  If anyone can help me with this I will give you all the points I have (sorry, I just joined so it's only 75).

You are building a shed and know exactly what cut lenghts of wood you need.  So now you need to go buy the wood, assuming the 2x4's (in this case it's all 2x4's) come in 8ft lenghts, how many 2x4's will you need to build the shed.

Even more extra credit was available if the cut width was added into the algorithm...

This has bothered me since 1988 and will for a while..  Any help would be great...

0
Question by:jitz
• 7
• 5
• 5
• +4

LVL 4

Expert Comment

ID: 2590431
If you fill your profile you get another 125 points
;->
0

LVL 28

Expert Comment

ID: 2590435
hmm...I dont even know what its asking  =/
0

Author Comment

ID: 2590455
Sorry AzraSound, I re-read my question and realized it is a little difficult.

Basically if you needed the following lenghts:

(2) 4ft lengths
(4) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
(1) 2ft lenght

Then you would have to purchase four 8ft 2x4's to cover your lengths, and and you would have a 6ft piece left over.

I hope this helps to explain.
0

LVL 4

Expert Comment

ID: 2590459
I think you can try this for a solution..

Area of the shed=(Time of the day * length of shadow )-(Distance between the sun and the earth)/Time at which the first ray from the sun reached the shed.

Once we get the area we can easily solve the other problem you mentioned.

;->
0

Author Comment

ID: 2590465
Uhm, that sounds like a good solution as long as there is lemonade (read: Beer) involved...  hehehe
0

LVL 4

Expert Comment

ID: 2590626
//////////////
Sorry AzraSound, I re-read my question and realized it is a little difficult.

Basically if you needed the following lenghts:

(2) 4ft lengths
(4) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
(1) 2ft lenght

Then you would have to purchase four 8ft 2x4's to cover your lengths, and and you would have a 6ft piece left over.

I hope this helps to explain.
////////////

hey with this hint you dont need to calculate the area....

i will give you the solution but you have to increase the points to 200....

;->
0

LVL 4

Expert Comment

ID: 2590634
//////////////
Sorry AzraSound, I re-read my question and realized it is a little difficult.

Basically if you needed the following lenghts:

(2) 4ft lengths
(4) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
(1) 2ft lenght

Then you would have to purchase four 8ft 2x4's to cover your lengths, and and you would have a 6ft piece left over.

I hope this helps to explain.
////////////

hey with this hint you dont need to calculate the area....

i will give you the solution but you have to increase the points to 200....

;->
0

LVL 18

Expert Comment

ID: 2590780
You need to consider every possible ordering of the cut wood and how it could be cut from the 8 foot lengths in sequence.

A question - is there actually a recognized solution to this problem, i.e is there a 'magic' combination to find which will use less pieces of wood than any other?
0

LVL 14

Accepted Solution

mcrider earned 75 total points
ID: 2591001
Here you go...

1) Create a new project.

2) Add a MODULE to the program.

3) Set the Startup Object to "Sub Main".  You do this by clicking "Project->Project1 Properties..." and select it from the General Tab.

4) Add the code below to the module and run the program...

THE CODE:

Type WoodType
Size As Long
Count As Long
End Type
Public LumberCount As Long
Public WoodPieces() As WoodType
Public Working() As WoodType

Sub Main()
Dim iVal As Long
Dim lBuf As String

'CLEAR THE ARRAYS
On Error Resume Next
ReDim WoodPieces(-1) As WoodType
ReDim Working(-1) As WoodType
Err = 0

'LOAD THE ARRAYS WITH THE WOOD SIZES AND AMOUNT
AddWood 4, 2 '(2) 4ft lengths
AddWood 2, 4 '(4) 2ft lenghts
AddWood 5, 1 '(1) 5ft lenght
AddWood 3, 1 '(1) 3ft lenght
AddWood 2, 1 '(1) 2ft lenght

CalculateWood
lBuf = "You will need " + CStr(LumberCount) + " 8-foot 2x4"
For iVal = 0 To UBound(Working)
With Working(iVal)
If Not .Count = 0 Then
lBuf = lBuf + vbCrLf + "There will be " + CStr(.Count) + " " + CStr(.Size) + "-foot piece leftover"
End If
End With
Next iVal
MsgBox lBuf
End Sub
Sub AddWood(Size As Long, Count As Long)
Dim iVal As Long
Dim jVal As Long
Dim lSwitch As Boolean
On Error Resume Next
iVal = UBound(WoodPieces)
If Not Err = 0 Then
ReDim WoodPieces(0) As WoodType
With WoodPieces(0): .Size = Size: .Count = Count: End With
ReDim Working(0) As WoodType
With Working(0): .Size = Size: .Count = 0: End With
Else
lSwitch = False
For jVal = 0 To iVal
If WoodPieces(jVal).Size = Size Then
WoodPieces(jVal).Count = WoodPieces(jVal).Count + Count
lSwitch = True
Exit For
End If
Next jVal
If lSwitch = False Then
iVal = iVal + 1
ReDim Preserve WoodPieces(iVal) As WoodType
With WoodPieces(iVal): .Size = Size: .Count = Count: End With
ReDim Preserve Working(iVal) As WoodType
With Working(iVal): .Size = Size: .Count = 0: End With
End If
End If
End Sub

Sub AddLeftoverWood(Size As Long, Count As Long)
Dim iVal As Long
Dim jVal As Long
Dim lSwitch As Boolean
On Error Resume Next
iVal = UBound(Working)
lSwitch = False
For jVal = 0 To iVal
If Working(jVal).Size = Size Then
Working(jVal).Count = Working(jVal).Count + Count
lSwitch = True
Exit For
End If
Next jVal
If lSwitch = False Then
ReDim Preserve Working(UBound(Working) + 1) As WoodType
With Working(UBound(Working)): .Size = Size: .Count = Count: End With
End If
End Sub

Sub woodAvailable(lWood As WoodType)
Dim iVal As Long
For iVal = 0 To UBound(Working)
With Working(iVal)
If .Size = lWood.Size Then
If .Count > lWood.Count Then
.Count = .Count - lWood.Count
lWood.Count = 0
Else
lWood.Count = lWood.Count - .Count
.Count = 0
End If
Exit For
End If
End With
Next iVal
End Sub

Function CalculateWood()
Dim iVal As Long
Dim jVal As Long
Dim kVal As Long
For iVal = 0 To UBound(WoodPieces)
woodAvailable WoodPieces(iVal)
LumberCount = LumberCount + ((WoodPieces(iVal).Size * WoodPieces(iVal).Count) / 8)
kVal = IIf(8 - ((WoodPieces(iVal).Size * WoodPieces(iVal).Count) Mod 8) = 8 _
, 0, 8 - ((WoodPieces(iVal).Size * WoodPieces(iVal).Count) Mod 8))
If Not kVal = 0 Then AddLeftoverWood kVal, 1
Next iVal
jVal = 0
For iVal = 0 To UBound(Working)
If Working(iVal).Count <> 0 Then
jVal = jVal + (Working(iVal).Count * Working(iVal).Size)
End If
Next iVal
Do
If jVal <= 0 Then Exit Do
jVal = jVal - 8
LumberCount = LumberCount + 1
Loop
End Function
0

LVL 4

Expert Comment

ID: 2591023
why cannot this be

(2) 4ft lengths
(4) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
(1) 2ft lenght

like this

(2) 4ft lengths
(5) 2ft lenghts
(1) 5ft lenght
(1) 3ft lenght
0

LVL 4

Expert Comment

ID: 2591047
deighton: Yes, there is.
I saw an example like this in one of my courses back in school. It has to do with optimization routines. I'll give it a look.
0

Author Comment

ID: 2592836
Wow, you actually did it!  I put the routine through a few tests and found that with higher numbers it sometimes had some extra remaining wood (2 - 4ft pieces).  But all in all I am VERY impressed!!

Thank You!

Now, I wonder if that teacher still works at the school....

0

LVL 14

Expert Comment

ID: 2592901
Well, it was just off the top of my head... But I'm glad you liked it.

Thanks for the points!

0

LVL 32

Expert Comment

ID: 2593620
Try it with fractions...

0

Author Comment

ID: 2594604
Erick7,

I thought of that too and modified Mcriders code to allow fractions.  It's not working as well as I'd hoped, but it does work and that's fine with me...  :)
0

LVL 14

Expert Comment

ID: 2594926
Sorry, but I didn't take fractions into consideration when I wrote that... You didn't mention it ;-)

0

LVL 32

Expert Comment

ID: 2596411
It's common sense.
The last time I built a shed, I believe I needed some non-integer sizes of lumber.  And my saw took about a 3/32" out of each cut.

Measure twice, cut once.
0

LVL 14

Expert Comment

ID: 2598560
Well, I dont build sheds, I build programs.. ;-)  Standard rule... If it's not in the program spec, Don't expect it in the program...

0

LVL 32

Expert Comment

ID: 2598594
Fair enough.
At least provide the user with a warning:

MsgBox "I'm sorry, You can't use fractional cuts to build your shed"

Otherwise, the builder may not get the right number of 2x4's.

;)
0

Author Comment

ID: 2598677
I don't plan to use Mcriders code for anything, I just wanted to see how this sort of thing is done.  Therefore fractional lengths really weren't that big of a deal (for me), and I didn't ask for support for them.

I'm sure if you offer Mcrider some points he'd be more than happy to modify the code for you...  :)  Since he is the #2 Expert on this site..  ;)

hehe, I feel as though I'm in the presence of greatness...  :)
0

Author Comment

ID: 2598713
Erick37,

I must apoligize.  I see you are #7 on the experts list.  I didn't know that before now, so when I mentioned mcrider being #2 I wasn't trying to be a smart-ass or anything.  I just didn't know you were up there also.

Just wanted to say that...
0

LVL 14

Expert Comment

ID: 2599116
jitz,

No genuflecting is necessary... I just like helping. ;-)

Cheers!
0

Author Comment

ID: 2599199
hehehehehe....

That was not worship (or genuflection), it was a simple "high five" if you will, or maybe just a "thumbs up", or how about the underrated "pat on the back"...

Now, if you were to re-do the sample you posted to handle non-integer numbers and, well, make it "bullet-proof" then, YES, it would be (minor) genuflection...  hehehe  :)

Later...
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were câ€¦
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games. Â  The game allows for a choice of who goes first and keeps track of the number of wins forâ€¦
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can launâ€¦
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process fromâ€¦

#### 744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!