• 0

C# Best way to determine the # of times a single product could fit within a


Question

Math time!

So I'm working on a project where you have those plastic adjustable compartment boxes, and you want to fill one of those compartments with a certain product. I want to figure out how many can fit instead said box.

At the most basic level, I could do.


public static int VolCalc(int mLength, int mWidth, int mHeight, out int mVol)
{
// Solve for product and compartment volume.
mVol = mLength * mWidth * mHeight;
return mVol;
}
public static int TotalCalc(int mPVol, int mBVol, out int mTot)
{
// Solve for # of times a product can fill the box.
var pMultiply = 0;
for (mTot = 0; pMultiply <= mBVol;)
{
mTot++;
pMultiply = mPVol * mTot;
}
// Since I'm using integers, remove the last one to account for <=.
mTot -= 1;
return mTot;
}
[/CODE]

Would you consider this the best way to approach this problem?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Math time!

So I'm working on a project where you have those plastic adjustable compartment boxes, and you want to fill one of those compartments with a certain product. I want to figure out how many can fit instead said box.

At the most basic level, I could do.


public static int AreaCalc(int mLength, int mWidth, int mHeight, out int mArea)
{
// Solve for product and compartment area.
mArea = mLength + mWidth + mHeight;
return mArea;
}
public static int TotalCalc(int mPArea, int mBArea, out int mTot)
{
// Solve for # of times a product can fill the box.
var pMultiply = 0;
for (mTot = 0; pMultiply <= mBArea;)
{
mTot++;
pMultiply = mPArea * mTot;
}
// Since I'm using integers, remove the last one to account for <=.
mTot -= 1;
return mTot;
}
[/CODE]

Would you consider this the best way to approach this problem?

The first function would be volume for a start, and it would be the product (multiply), not an addition.

Then just divide the total volume of the room, by the smaller boxes volume.

Room is say 4 * 4 * 4 = 64 squares in volume.

The box is 2 * 2 * 2 = 8 squares in volume

64/8 = 8 boxes in total.

The real issue is dealing with partial boxes, so you might have to use some MOD in there. The calc would try to stretch or squeeze boxes, so you can only deal in integer boxes in each direction.

Do your own homework....

Link to comment
Share on other sites

  • 0

The first function would be volume for a start, and it would be the product (multiply), not an addition.

Then just divide the total volume of the room, by the smaller boxes volume.

Room is say 4 * 4 * 4 = 64 squares in volume.

The box is 2 * 2 * 2 = 8 squares in volume

64/8 = 8 boxes in total.

The real issue is dealing with partial boxes, so you might have to use some MOD in there. The calc would try to stretch or squeeze boxes, so you can only deal in integer boxes in each direction.

Do your own homework....

I'll look into the changes.

Do your own homework....

No.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.