### Problem with Program, Calculating area of circle

Hello there,

I am having a problem with my program. My teacher said I was very close to get the correct answer but for some reason, I can not seem to figure out what is wrong with it. The program calculates the area of a circle. n is the number of rectangles, that get smaller and smaller throughout the program until the difference between the old calculated value and the new value is less than .1%. X is the length of the base of each rectangle. If the difference is bigger than .1%, the loop should repeat itself with a higher number for N. He told me that I should use another while loop instead of the for loop that I am using right now.

I really hope that someone can help me, and thank you for your help already.

/* This program calculates the area under a semi circle, by prompting the user to enter a vlaue for the radius.*/

#include<iostream>
#include<cmath>

using namespace std;

int main ()
{

double radius, difference, area, oldarea, fullarea, n(2), i, square;

cout << "Please enter the radius of the circle you wish to calcuate the area for.\n\n";

while (difference <= .1)
{
for (i=1; i==n; i++) {

area += square;

}

difference = abs(((area - oldarea)/oldarea))*100;

if (difference > .1) {
oldarea=area;
}

n++;

}

fullarea = 2*area;

cout << "\nThe estimated area of the circle is: " << fullarea << endl << endl;

system("pause");
return(0);

}
Last edited on
Tell me, the very first time you reach the while loop, what is the value of difference? You're testing against it, so what are you expecting to happen the very first time?
Also, if you are calculating the area of a circle where is PI? AH nm, using smaller and smaller rectangles I see now.
Last edited on
The first time I reach it, it should use the value from "old area" which is an estimate that is made before the while loop and when it gets in the loop it should use this value and use the new value to calculate the difference...
 The first time I reach it, it should use the value from "old area" which is an estimate that is made before the while loop

No. That has nothing to do with the variable `difference`. Point to the line of code you think is setting the value of difference before the while loop.
Last edited on
Topic archived. No new replies allowed.