CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2010
    Posts
    9

    Code Help Function

    Problem: how many small squares can you put into a rectangle
    Formula: i'll get the area of the rectangle and divide it by the area of the square.
    CODE:

    #include <stdio.h>

    int areaSquare(int nSide){

    int nAreaS;
    nAreaS=(nSide*nSide);
    return (nAreaS);
    }

    int areaRectangle (int nLength, int nWidth){

    int nAreaR;
    nAreaR=(nLength*nWidth);
    return (nAreaR);
    }

    main(){
    float numberSquares;
    numberSquares=areaRectangle()/areaSquare();

    printf("The Number of Squares:%.2f", numberSquares);
    getch();
    }


    i used getch(); so i can see my visual output,

    PROBLEM: too few arguments on areaRectangle and areaSquare on main.
    if i did a mistake please tell me
    and some tips too. THANKS!

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Code Help Function

    Try
    Code:
    numberSquares=areaRectangle(5,7)/areaSquare(2);
    but do check your result using pen & paper. You probably would want to re-write your code after that.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jun 2010
    Posts
    9

    Re: Code Help Function

    How can i show the remainder if ever?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Code Help Function

    It depends on what you mean by the remainder but one way could be to tell if all rectangle space could be used and if not, how much space that was unusable.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Jun 2010
    Posts
    9

    Re: Code Help Function

    space that is unusable

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Code Help Function

    You're going about this incorrectly. Just dividing areas isn't going to give you the right result. As S_M_A said, try it with pencil and paper first, and figure out the correct algorithm before you try to code. Are you allowed to rotate rectangles?

  7. #7
    Join Date
    Jun 2010
    Posts
    9

    Re: Code Help Function

    help! i know that a two 2x2 square can only fit in a 4x3 rectangle. :P i need some algorithm with a remainder or excess or something

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Code Help Function

    Quote Originally Posted by twogreenarrows View Post
    help! i know that a two 2x2 square can only fit in a 4x3 rectangle. :P i need some algorithm with a remainder or excess or something
    That's kind of vague, but once you have the number of rectangles, then you can subtract the total area of the rectangles from the area of the square.

  9. #9
    Join Date
    Jun 2010
    Posts
    9

    Re: Code Help Function

    ohh yeah subtraction. Please help me for the algorithm,

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Code Help Function

    Quote Originally Posted by twogreenarrows View Post
    ohh yeah subtraction. Please help me for the algorithm,
    Can you rotate the rectangles?

  11. #11
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Code Help Function

    Stop thinking in area and start thinking in lengths. Example:

    If your rectangle is 25*35, and your squares are 3*3:
    Your rectangle widths can hold 25/3 = 7 and 35/3 = 11 square widths.

    This means it can hold a total of 7*11=77 squares.

    Rounding down is simple, it is the default integer behavior: Nothing fancy.

    I'll let you write the algorithm yourself. If I provide any more help, I'd be doing it for you.

    PS. I'm assuming you can't rotate the rectangles, or place them in some crazy fonfiguration of course.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  12. #12
    Join Date
    Jun 2010
    Posts
    9

    Re: Code Help Function

    ok i get it now i made the algorithm but will this require me to use function?

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Code Help Function

    Nothing ever requires you to use functions (except main(), of course). But it's usually a good idea....

    You want a function any time you have very similar code appearing multiple times in your program, or any time you can describe a task in terms of a few inputs and outputs, independent of what the rest of the logic is doing with those numbers.
    Last edited by Lindley; June 20th, 2010 at 10:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured