I've been working on this now for a couple of weeks and am still having dificulties. I'm trying to come up with a function to compute and return the area of the quarter circle by dividing it into a given number of rectangles. I need to have it accept the number of rectangles used, and display the estimate of pi.

The width of each rectangle can be achieved by dividing the radius by the # of rectangles... so I got

int main()
{
      int n;
      double radius = 2;
      double width = radius / n;
 
  cout << "Enter the number of rectangles> ";
  cin >> n;

then height would be h = square root of( r * r – x * x) but I'm not sure how to implement that into my program. then the area would be the sum of n (h*w).

does anyone know how i would be able to set up the height of the rectangles?

I've been working on this now for a couple of weeks and am still having dificulties. I'm trying to come up with a function to compute and return the area of the quarter circle by dividing it into a given number of rectangles. I need to have it accept the number of rectangles used, and display the estimate of pi.

The width of each rectangle can be achieved by dividing the radius by the # of rectangles... so I got

int main()
{
      int n;
      double radius = 2;
      double width = radius / n;
 
  cout << "Enter the number of rectangles> ";
  cin >> n;

then height would be h = square root of( r * r – x * x) but I'm not sure how to implement that into my program. then the area would be the sum of n (h*w).

does anyone know how i would be able to set up the height of the rectangles?

Try picking up a beginners book, prolly how to enter variables. This is about as simple as it gets.

Also, your main function needs a closing brace:

int main()
{
      int n;
      double radius = 2;
      double width = radius / n;
 
  cout << "Enter the number of rectangles> ";
  cin >> n;
}

Thanks peeps, but i still do not understand how to get the height. eg, with radius 2 and n 10, the width of each rectangle would be 0.2. therefore x would be changing at the midpoint of each rectangle. so x=0.1, 0.3, 0.5, etc up to the 10th rectangle.....I have no idea how to get my program to do that:(

First write out a timeline of how to do it on paper. Then take that timeline and translate it into code. (hint: this double width = radius / n; is in the wrong place).

Don't see the problem. As you say, the width of each rectangle will be r/n, and presumably you will be going round a loop to calculate the area of each rectangle. If i is the index in a for loop, then at each iteration x = r * i / n, and the height will be sqrt( r * r - r * r * i * i / (n * n) ), or slightly more compactly, r * sqrt( 1 - i * i / (n * n) ).

This article has been dead for over six months. Start a new discussion instead.