Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?

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

3 replies to this topic

### #1 Kalint

Kalint

Resident Elite

• Tech Issues Solved: 2
• Joined: 16-January 07

Posted 10 January 2013 - 22:23

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;
}
```

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

### #2 lunamonkey

lunamonkey

Ten years on Neowin.

• Joined: 28-May 03
• Location: Swindon, England

Posted 10 January 2013 - 22:34

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;
}
```

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.

### #3 OP Kalint

Kalint

Resident Elite

• Tech Issues Solved: 2
• Joined: 16-January 07

Posted 10 January 2013 - 22:37

Oh yeah volume duh 3d.

### #4 OP Kalint

Kalint

Resident Elite

• Tech Issues Solved: 2
• Joined: 16-January 07

Posted 10 January 2013 - 22:49

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.