CS106B Stanford University (kinda like Riemann Sum)

Hi I was doing this exercise while back and now I want to complete it.Here is the original posts about this issues i posted while back.
ccie007 (1) Jul 13, 2011 at 6:53pm
Hi All,
I am doing CS106B Standford university course and practising C++ exercises from the book/reader.I am stuck on the exercise bcoz i don't understand what i have to do here.Could anybody give hints please.I believe i can do this if i get some hints.The quesion is as follows
You can also approximate π by approximating the area bounded by a circular arc. Consider the following quarter circle:
which has a radius r equal to two inches. From the formula for the area of a circle, you can easily determine that the area of the quarter circle should be π square inches. You can also approximate the area computationally by adding up the areas of a series of rectangles, where each rectangle has a fixed width and the height is chosen so that the circle passes through the midpoint of the top of the rectangle. For example, if you divide the area into 10 rectangles from left to right, you get the following diagram:
The sum of the areas of the rectangles approximates the area of the quarter circle. The more rectangles there are, the closer the approximation.
For each rectangle, the width w is a constant derived by dividing the radius by the number of rectangles. The height h, on the other hand, varies depending on the position of the rectangle. If the midpoint of the rectangle in the horizontal direction is given by x, the height of the rectangle can be computed using the distance formula

h = √r–x

The area of each rectangle is then simply h x w.
Write a program to compute the area of the quarter circle by dividing it into 100 rectangles.
writetonsharma (1180) Jul 13, 2011 at 7:59pm
**the diagram and formula cannot be seen.

if the radius is 40, and we have four rectangles. The width will be 10 each. height can be calculated by the formula given above. So the height will increase for each rectangle if we go from left to right.
just add the area you get four time or the number of rectangle times. Thats it.

I don't if this is the question or I understood something else.
muratagenc (65) Jul 14, 2011 at 8:28am
if you think about this image: http://img20.imageshack.us/i/97770678.png/

the number of rectangles is 100,
radius; 2 inch
according to the definition above;
w = 2 inch / 100 for each rectangular. (on the picture they're not equal though)

height for each rectangular;
for example, for the 49th rect;

it's corner coordinates;
a = 0
b = 49 * ( 2 / 100).

the height is the distance of the corner of the 49th rect to the intersection at the arc;
intersection point is from the circle equation:
(x * x ) - ( y * y ) = 2 * 2

x = b;
sqrt((b * b) - (2 * 2)) = y

the height of this 49th rectangle is y (it is equal to h)

which means,
1. first find w, it's a fixed number.
2. go into a loop, find all rectangles' height
3. within the loop, calculate w * h for each rect, and add it to a totalArea variable.

which is the answer. I think.
Last edited on Jul 14, 2011 at 8:31am
mcrist (33) Jul 14, 2011 at 12:48pm
Aren't you just trying to compute the Riemann sum with 100 divisions and middle sum? If you research this, you will probably understand the problem a little better - at least how I think you are proposing it.



I have finished the exercise but not sure whether i did it right or not.Instead of 2 inch I chose 200 centimeters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * File: narcissism.cpp
 * --------------------
 * This file defines a program that prints out
 * the first 25 narcissistic numbers.
 */

#include"genlib.h"
#include"simpio.h"
#include<iostream>
#include <stdio.h>
#include <math.h>



int main(){
//instead of 2 inch Radius as the book says I chose 200.0 centimeters for radius
	double r=200.0;
//number of rect is 100
	double numbOfRect=100.0;
//wide of a rectangle
	double w=r/100;
  //  double w=2/numbOfRect;
	
//initialising b variable
	double b=0.0;
//initialising total are of a quarter circle
	double totalAreaQuartCircle=0;
	
//loop starts
	for(int i=0;i<numbOfRect;i++){
	
	//this calculation I learnt from the poster muratagenc (65)
		b=i*(r/100);
	
		
	//height of a rectangle calucaltion
		double h=sqrt((r*r)-(b*b));
				   
				 
	
	//area of a rectangle calculation
	
		double areaOfRect=h*w;
		
	
		
	//total area of a quarter circle calculation	
		
		totalAreaQuartCircle+=areaOfRect;
		
		
	}
	
	//diplaying the answer
	
	cout<<"The total of area of a quarter circle is "<<totalAreaQuartCircle<<" cm ";
}


Have i done it right.Could anybody help me plz.Thank you advance
Can you please condense this to, let's say, 5% of that text? I doubt that whatever your problem is that it requires this much information to solve.
Sorry about that mate.Here is the question from the book I am trying to tackle.
You can also approximate π by approximating the area bounded by a circular arc. Consider the following quarter circle:
which has a radius r equal to two inches. From the formula for the area of a circle, you can easily determine that the area of the quarter circle should be π square inches. You can also approximate the area computationally by adding up the areas of a series of rectangles, where each rectangle has a fixed width and the height is chosen so that the circle passes through the midpoint of the top of the rectangle. For example, if you divide the area into 10 rectangles from left to right, you get the following diagram:
The sum of the areas of the rectangles approximates the area of the quarter circle. The more rectangles there are, the closer the approximation.
For each rectangle, the width w is a constant derived by dividing the radius by the number of rectangles. The height h, on the other hand, varies depending on the position of the rectangle. If the midpoint of the rectangle in the horizontal direction is given by x, the height of the rectangle can be computed using the distance formula

h = √r–x

The area of each rectangle is then simply h x w.
Write a program to compute the area of the quarter circle by dividing it into 100 rectangles.


My code is based on the info provided by the poster muratagenc(65) who says
muratagenc (65) Jul 14, 2011 at 8:28am
if you think about this image: http://img20.imageshack.us/i/97770678.png/

the number of rectangles is 100,
radius; 2 inch
according to the definition above;
w = 2 inch / 100 for each rectangular. (on the picture they're not equal though)

height for each rectangular;
for example, for the 49th rect;

it's corner coordinates;
a = 0
b = 49 * ( 2 / 100).

the height is the distance of the corner of the 49th rect to the intersection at the arc;
intersection point is from the circle equation:
(x * x ) - ( y * y ) = 2 * 2

x = b;
sqrt((b * b) - (2 * 2)) = y

the height of this 49th rectangle is y (it is equal to h)

which means,
1. first find w, it's a fixed number.
2. go into a loop, find all rectangles' height
3. within the loop, calculate w * h for each rect, and add it to a totalArea variable.

which is the answer. I think.

is it still long?
Can anybody help me please?
Topic archived. No new replies allowed.