need help with a formula!!

This is a discussion on need help with a formula!! within the C Programming forums, part of the General Programming Boards category; this is what i need to do: insert width and length of a rectangular. how many squares of 20 cms ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    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?

  2. #2
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    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.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,673
    Use division on both user entered lengths... the answer is the result of those values multiplied by each other.
    I used to be an adventurer like you... then I took an arrow to the knee.

  4. #4
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    That's true and much better approach than mine. % is your friend.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Yeah thx, already fixed it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help on formula?
    By Radox in forum C Programming
    Replies: 3
    Last Post: 03-04-2011, 08:14 PM
  2. Anyone know how to use this formula?
    By gator6688 in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2007, 09:42 PM
  3. Parsing a formula
    By ashkya in forum C Programming
    Replies: 2
    Last Post: 02-27-2006, 07:39 AM
  4. Formula for Pi
    By zowen in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 05:17 AM
  5. formula
    By pete in forum C++ Programming
    Replies: 7
    Last Post: 05-26-2003, 08:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21