# Area of a circle error, help

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-14-2010
spazx
Area of a circle
is the line from my program. I changed it to multiplying by itself because using any other operator would generate an error. I tried moving and removing parentheses but it did not stop it from generating an error. The * to the right of PI and / to the left of 4.0 continuously error.
• 09-14-2010
rags_to_riches
What is the error? We're not mind-readers!
• 09-14-2010
Char*Pntr
Quote:

Originally Posted by spazx
is the line from my program. I changed it to multiplying by itself because using any other operator would generate an error. I tried moving and removing parentheses but it did not stop it from generating an error. The * to the right of PI and / to the left of 4.0 continuously error.

The area of a circle is just PI * radius^2

Can you post all of your code?
• 09-14-2010
Your equation is incorrect. You need Area = (Pi * d^2)/4

You'll find it much easier if you first, find the area of the whole circle, then find the area of the hole, then subtract the hole area from the whole area. :p

Right now, you're mashing up the calculation, together. Hard to get just right, that way.

That's a fact, and a pretty dang good pun, at the same time. :p :p
• 09-14-2010
spazx
It keeps saying expression syntax.
The equation is to find the surface area for one side of the gasket. It is the difference between d_gasket and d_hole, both of which need to be squared before subtracted.
• 09-14-2010
spazx
#include <stdio.h>
int main(void)
{
#define PI = 3.14159265;
printf("\n Enter the diameter of the gasket.");
{
printf("\n Error: Must be greater than zero");
}
printf("\n Enter the diameter of the hole.");
scanf("%lg", d_hole);
{
printf("\n Error: Hole too small.");
scanf("%lg", d_hole);
}
{
printf("\n Error: Hole too large");
scanf("%lg", d_hole);
}
printf("\n Area:", &area);
printf("\n Hole diameter:", &d_hole);
printf("\n Normal Termination");
return 0;
}
• 09-14-2010
spazx
It keeps saying expression syntax.
The equation is to find the surface area for one side of the gasket. It is the difference between d_gasket and d_hole, both of which need to be squared before subtracted.
Here is the full code:
#include <stdio.h>
int main(void)
{
#define PI = 3.14159265;
printf("\n Enter the diameter of the gasket.");
{
printf("\n Error: Must be greater than zero");
}
printf("\n Enter the diameter of the hole.");
scanf("%lg", d_hole);
{
printf("\n Error: Hole too small.");
scanf("%lg", d_hole);
}
{
printf("\n Error: Hole too large");
scanf("%lg", d_hole);
}
printf("\n Area:", &area);
printf("\n Hole diameter:", &d_hole);
printf("\n Normal Termination");
return 0;
}
• 09-14-2010
MWAAAHAAA
Quote:

Your equation is incorrect. You need Area = (Pi * d^2)/4

You aren't squaring anything in your equation. Also, you'll find it much easier if you first, find the area of the whole circle, then find the area of the hole, then subtract the hole area from the whole area. :p

That's a fact, and a pretty dang good pun, at the same time. :p :p

Although I find the wordplay quite amusing, I fail to see where OP is not squaring anything?

Isn't d * d the square of d? And isn't A1 - A2 = pi * d1 ** 2 / 4 - pi * d2 ** 2 / 4 = pi * (d1 ** 2 - d2 ** 2) / 4. That is, subtracting to areas results in the equation as implemented by OP?

Spazx, post the exact error you are receiving.
• 09-14-2010
MWAAAHAAA
Quote:

Originally Posted by spazx
It keeps saying expression syntax.
The equation is to find the surface area for one side of the gasket. It is the difference between d_gasket and d_hole, both of which need to be squared before subtracted.
Here is the full code:
Code:

```#include <stdio.h> int main(void) {         double d_gasket, d_hole, area;         printf("Gasket Area Program Started.");         #define PI = 3.14159265;         printf("\n Enter the diameter of the gasket.");         scanf("%lg", d_gasket);         while (d_gasket<= 0)         {                 printf("\n Error: Must be greater than zero");                 scanf("%lg", d_gasket);         }         printf("\n Enter the diameter of the hole.");         scanf("%lg", d_hole);         while (d_gasket*.3<= d_hole)         {                 printf("\n Error: Hole too small.");                 scanf("%lg", d_hole);         }         while (d_gasket*.9>= d_hole)         {                 printf("\n Error: Hole too large");                 scanf("%lg", d_hole);         }         area = ((PI)*(d_gasket^2)-(d_hole^2))/4;         printf("\n Area:", &area);         printf("\n Gasket diameter:", &d_gasket);         printf("\n Hole diameter:", &d_hole);         printf("\n Normal Termination");         return 0; }```

Using code tags preserves the indentation. Makes it easier for anyone to read your code.

So what is the error you are getting?

EDIT: see red code. C does not have a built-in exponentiation operator. The operator you are using, ^, applies a bitwise XOR to its operands. This is not allowed for floating point types (i.e. float and double). Your original post, d * d, would be a correct solution.

EDIT #2: see yellow code. man printf. You are forgetting to insert conversion specifiers into your format string. Hint: use %f for doubles. Moreover, the arguments are not supposed to be pointers. They can be passed by value (as opposed to scanf, which has to store the user input somewhere, which is done by passing a pointer to the storage area).

EDIT #3: see blue code. man scanf. Refer to EDIT #2. You need pointers here, otherwise user input cannot be stored properly and you will probably end up segfaulting.

EDIT #4: see purple code. #include <math.h> in your source file and use the defined M_PI. Not sure if M_PI is provided as a matter of standard or not.

EDIT #last: pay attention to the error and warning messages your compiler spits out at you.
• 09-14-2010
spazx
Expression Syntax, Floating point error and Statement missing ";"
The line is currently:
• 09-14-2010
Char*Pntr
Code:

```         Quote:                                                                                                 Originally Posted by spazx                                                                                 It keeps saying expression syntax. The equation is to find the surface area for one side of the gasket. It is the difference between d_gasket and d_hole, both of which need to be squared before subtracted. Here is the full code: #include <stdio.h> int main(void) {         double d_gasket, d_hole, area;         printf("Gasket Area Program Started.");         #define PI = 3.14159265;         printf("\n Enter the diameter of the gasket.");         scanf("%lg", d_gasket);         while (d_gasket<= 0)         {                 printf("\n Error: Must be greater than zero");                 scanf("%lg", d_gasket);         }         printf("\n Enter the diameter of the hole.");         scanf("%lg", d_hole);         while (d_gasket*.3<= d_hole)         {                 printf("\n Error: Hole too small.");                 scanf("%lg", d_hole);         }         while (d_gasket*.9>= d_hole)         {                 printf("\n Error: Hole too large");                 scanf("%lg", d_hole);         }         area = ((PI)*(d_gasket^2)-(d_hole^2))/4;         printf("\n Area:", &area);         printf("\n Gasket diameter:", &d_gasket);         printf("\n Hole diameter:", &d_hole);         printf("\n Normal Termination");         return 0; }                                                 ```

Also PI declaration is incorrect... get rid of 1) '=' and 2) the semicolon ';'
• 09-14-2010
spazx
The PI declaration was what was wrong with it, my syntax after the change of the declaration was fine. Thank you.
• 09-14-2010
Char*Pntr
Quote:

Originally Posted by spazx
The PI declaration was what was wrong with it, my syntax after the change of the declaration was fine. Thank you.

I thought that may :-)

In the future use code tags
Code:

`  code inside here...`
It makes reading much easier. A little bit of indentation would help too.
• 09-14-2010
MWAAAHAAA
Quote:

Originally Posted by spazx
The PI declaration was what was wrong with it, my syntax after the change of the declaration was fine. Thank you.

Indeed, the syntax is fine (aside from attempting to XOR with a double, if this is considered a syntax error). However, this does not mean that the code is correct or does what is intended.
• 09-14-2010
bhdavis1978
I've just taken a cursory glance through this, but there is a pretty fundamental error in your questions
Code:

`area = ((PI)*(d_gasket^2)-(d_hole^2))/4;`
In C, the ^ operator does not mean "raise to the power of", it means XOR, hence MWAAAHAAA's comment. You need to use the pow function, which is a bit silly here- do this instead

Code:

`area= ((PI)*(d_gasket*d_gasket)-(d_hole*d_hole))/4;`
.

Don't assume that just because any given operator is typically used for a given function in most instances, that it always has the same meaning. If you want to perform the following calculation

x^y

Then you do it this way

Code:

`pow(x,y);`
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last