My guess is that
const double AREC (int length)(int width);
is supposed to be a function which returns the area of a rectangle.
Let's think mathematically for a second. A function is (ideally) nothing more than a mapping between inputs and exactly one output.
In this case, you want to program a function which accepts the dimensions of a rectangle and returns it's area.
There are four things you need to do:
Decide on names which make the purpose of your function clear
Decide what kind of inputs you want to accept
Decide what kind of output you want to produce
Write the function body.
You want a function which computes the area of a rectangle.
rectangle_area()
You need two inputs -- the width and the height.
rectangle_area(width, height)
Probably both dimensions are real numbers, and even if not, perhaps you should allow for that possibility. Therefore we'll be using
double
precision floating-point, which can represent a decent approximation of most reasonable inputs.
rectangle_area(double width, double height)
If we will allow users to compute the area of a rectangle whose dimensions are real, we'll need to output a real-number too:
double rectangle_area(double width, double height)
The part of the program that computes the area from the width and height goes between braces:
1 2 3
|
double rectangle_area(double width, double height) {
}
|
And we know that the area of a rectangle is the product of its dimensions:
1 2 3
|
double rectangle_area(double width, double height) {
width * height;
}
|
Finally, we need to give the result of width*height back to the caller -- the person who's asking for it:
1 2 3
|
double rectangle_area(double width, double height) {
return width * height;
}
|
Then you could compute the area of a rectangle with dimensions 2.5 and 3.5 by writing a complete program which looks like this:
1 2 3 4 5 6 7 8
|
# include <iostream>
double rectangle_area(double width, double height) {
return width * height;
}
int main() {
std::cout << rectangle_area(2.5, 3.5);
}
|
Can you follow a similar process for computing the area of a circle with some radius? Show us your attempt.
Also, please move this post to the Beginner's board.