Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are my calculation results wrong?
    text
    copied!<p>Im building a program where it calculates how many bricks will fit into a box of any dimensions in C#. But also where the brick will fit in any orientation. It works some for some of my tests where the answer returned is what I expect it to be but others, the answer (while technically correct) isn't returning the maximum number that you could fit in to the box.</p> <pre><code>private static int BricksPerDimension(int dimWidth, int dimLength, int dimHeight, int brickSideWidth, int brickSideLength, int brickSideHeight) { int newBrickSideHeight; int newBrickSideWidth; if (dimHeight &gt;= brickSideLength) { newBrickSideHeight = brickSideLength; return (int)Math.Floor((double)dimHeight / newBrickSideHeight); } else if (dimWidth &gt;= brickSideLength) { newBrickSideWidth = brickSideLength; return (int)Math.Floor((double)dimWidth / newBrickSideWidth); } else if (dimLength &gt;= brickSideLength) { return (int)Math.Floor((double)dimLength / brickSideLength); } else if (dimLength &lt;= brickSideLength &amp;&amp; dimHeight &lt;= brickSideLength &amp;&amp; dimWidth &lt;= brickSideLength) { return 0; } else { return 0; } </code></pre> <p>This is my code the test results are as follows: box with dimensions of 10x20x50 with brick of 1x2x5 = 1000 bricks, which is what I expected. And when I've tested the box but with the dimensions reordered for example 20x10x50 im returned 1000 bricks again which is great.</p> <p>But for example this test box = 20 x 70 x 30 brick = 1 x 2 x 5 returns 2744, which unless my own calculations are seriously wrong should return as 4200 bricks.</p> <p>I've used breakpoints to see if any of the values of the variables are going wrong somewhere, but nothing seems to be wrong with them. Ive been scratching my head over this for days and am not any closer to figuring it out.</p> <p>Thanks in advance. P.S The way I constructed the code in regards to bricks fitting in any orientation was that if the longest side of the brick was equal to or less than the longest side of the box, then at the very least 1 brick must be able to fit. and so I did the if statement to check if this was true and then change the variables around. If this is the wrong way to approach this please let me know.</p> <p>Edit Multiplication:</p> <pre><code>int numX = BricksPerDimension(boxW, boxL, boxH, brickSideWidth, brickSideLength, brickSideHeight); int numY = BricksPerDimension(boxW, boxL, boxH, brickSideWidth, brickSideLength, brickSideHeight); int numZ = BricksPerDimension(boxW, boxL, boxH, brickSideWidth, brickSideLength, brickSideHeight); return numX * numY * numZ; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload