-
June 18th, 2010, 10:50 PM
#1
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!
-
June 19th, 2010, 06:03 AM
#2
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.
-
June 19th, 2010, 09:21 AM
#3
Re: Code Help Function
How can i show the remainder if ever?
-
June 19th, 2010, 10:13 AM
#4
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.
-
June 19th, 2010, 10:46 AM
#5
Re: Code Help Function
space that is unusable
-
June 19th, 2010, 11:15 AM
#6
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?
-
June 19th, 2010, 11:19 AM
#7
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
-
June 19th, 2010, 11:34 AM
#8
Re: Code Help Function
Originally Posted by twogreenarrows
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.
-
June 19th, 2010, 11:41 AM
#9
Re: Code Help Function
ohh yeah subtraction. Please help me for the algorithm,
-
June 19th, 2010, 12:31 PM
#10
Re: Code Help Function
Originally Posted by twogreenarrows
ohh yeah subtraction. Please help me for the algorithm,
Can you rotate the rectangles?
-
June 19th, 2010, 12:57 PM
#11
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.
-
June 20th, 2010, 01:19 AM
#12
Re: Code Help Function
ok i get it now i made the algorithm but will this require me to use function?
-
June 20th, 2010, 10:40 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|