tags:

views:

284

answers:

6

The Problem:

A box can hold 53 items. If a person has 56 items, it will require 2 boxes to hold them. Box 1 will hold 53 items and box 2 will hold 3.

How do i repeat the above where 53 is a constant, unchanging value and 56 is a variable for each box:

Math.Ceiling(Convert.ToDecimal(intFeet / 53))

what i have so far that is:

int TotalItems = 56; 
int Boxes = Math.Ceiling(Convert.ToDecimal(intFeet / 53));  

for (int i = 0; i < Boxes; i++)
{  
    int itemsincurrentbox=??  
}
+4  A: 

simple, overly imperative example:

int itemsPerBox = 53;
int totalItems = 56;
int remainder = 0;
int boxes = Math.DivRem(totalItems, itemsPerBox, out remainder);
for(int i = 0; i <= boxes; i++){
    int itemsincurrentbox = i == boxes ? remainder : itemsPerBox;
}
Luke Schafer
I yearn for Perl and it's list syntax where I can do (div, rem) = divRem(totalItems, itemsPerBox); Why can't I get a modern imperative language to support this syntax?
Chris Kaminski
Beats me... it's hard enough to get functions as first class objects... though I think tuple support would be easier to implement.
Luke Schafer
@darthcoder - you mean one like F#? Or Python?
Greg Beech
F# isn't a 'modern imperative language' - it's functional (though impure)I wouldn't argue Python as being 'modern' (that's not to say I don't like it, because I do...), but even so, it's functional enough that I feel dirty calling it imperative :)
Luke Schafer
Not sure why people downvoted me, except to maybe raise their answers, as my example is more complete (uses the OPs code) and uses an already provided library function (DivRem) instead of performing the same operation manually.
Luke Schafer
+1  A: 

If I understand the question properly, all boxes except the last box will hold 53 items, whereas the last box will hold intFeet % 53 (intFeet mod 53, or the remainder after the division of intFeet and 53).

The loop is unnecessary, however, to answer your question;

int totalItems = 56;
int boxes = Math.Ceiling(Convert.ToDecimal(totalItems / 53)) + 1;
for(int i=0; i< boxes;i++)
{
   int numberInBoxes = i != boxes -1 ? 53 : totalItems % 53;
}
johnc
unnecessary computation during each loop - not that it's going to make much of a difference to computation time (or that it matters), but it decreases clarity and readability - there is no need to perform the mod in the loop if the remainder is retained (see my example).But yeah, most people seem to arrive at pretty much the same result :)
Luke Schafer
I'm not repeating the modulus. read up on ternary operators
johnc
I could have sworn that the mod was on the LHS when I read it, my bad. I do think it's less clean to read, but you are totally correct. 'Read up on ternary operators' was a bit of a wicked stab, as my example clearly uses a ternary, but I forgive you.
Luke Schafer
The mod was always on the rhs, however I do admit I did accidently have i == Boxes instead of i != Boxes, so I can understand the confusion. Thanks for the forgiveness ;p
johnc
+1  A: 

try using a modulus?

x % y
Jason
He clearly isn't at the level yet where it is clear how to use it properly without a bit more help. Also, the modulus symbol in C# is %
Luke Schafer
A: 

All but the last box will have 53 items. As for computing the number of full boxes and the number of items in the last box, look up integer division and modulus.

BCS
If the OP wants more than that, they are asking me to do there work for them.
BCS
+1  A: 

Use the modulus operator to determine the remainder. Quick example:

int totalBoxes = Math.Ceiling(Convert.ToDecimal(intFeet / 53));

List<int> boxes = new List<int>();
for (int i=0; i< totalBoxes; i++)
{
  if (i == totalBoxes-1) 
      boxes.Add(intFeet % 53)
  else 
      boxes.Add(53);
}
womp
+11  A: 

Where the integers capacity and numItems are your box capacity (53 in the example) and the total number of items you have, use the following two calculations:

int numBoxes = numItems / capacity;
int remainder = numItems % capacity;

This will give you the number of boxes that are filled (numBoxes), and the number of items in an additional box (remainder) if any are needed, since this value could be 0.

Edit: As Luke pointed out in the comments, you can get the same result with the .NET class library function Math.DivRem.

int remainder;
int numBoxes = Math.DivRem( numItems, capacity, out remainder );

This function returns the quotient and puts the remainder in an output parameter.

Bill the Lizard
also: int remainder;int numBoxes = Math.DivRem(numItems, capacity, out remainder);
Luke Schafer
@Luke: Fancy! I really need to do more .NET programming to become familiar with the common library functions.
Bill the Lizard
It's not like you really need it though :) Good explanation by the way.
Luke Schafer