This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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).

The challenge was this:

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...

Thanks in advance...

The challenge was this:

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...

Thanks in advance...

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.

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.

;->

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....

;->

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....

;->

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?

(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

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.

Thank You!

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

Thanks for the points!

Cheers!®©

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... :)

Cheers!®©

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.

Cheers!®©

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.

;)

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... :)

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...

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...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

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...

Cheers!®©

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