# need help with a formula!!

• 03-15-2012
brigas
need help with a formula!!
this is what i need to do:
insert width and length of a rectangular.
how many squares of 20 cms side can u put inside that rectangular.
and whats the area left with no squares.

with the width and lenght of the rectangular i can calculate the area of the rectangle.
Using the area i can calculate how many 20x20 is inside that area. but if its a 30x35 rectangle A=950, and 20x20 is 400. it says it can hold 2 squares ( 400x 2 =800 < 950) , but its not true it can only hold 1 and some area is left over.

How do i have to write to be sure that it reads the square as a 20x20 and not just an area of 400?
• 03-15-2012
javaeyes
It's a tougher algorithm than you are trying to make it. I believe you are going to need to do it on a row by row basis. Basically check whether the rectangle has a height of at least 20, if it does, fill that row with as many 20 x 20's as you can. Then subtract 20 from the overall rectangle height, and test it again. Keep a count of how many 20 x 20s you have used and then subtract that from the original rectangle area. I don't want to do you're hw for you, but I think this is the correct alg. Although you could make it more efficient, by just caching the first number of 20x20s that fit into a row and then figuring out how many rows you can fit into the rectangle. Good luck.
• 03-15-2012
hk_mp5kpdw
Use division on both user entered lengths... the answer is the result of those values multiplied by each other.
• 03-15-2012
javaeyes
That's true and much better approach than mine. % is your friend.
• 03-15-2012
brigas