Area of a circle error, help

This is a discussion on Area of a circle error, help within the C Programming forums, part of the General Programming Boards category; area = PI*(d_gasket*d_gasket-d_hole*d_hole)/4.0 is the line from my program. I changed it to multiplying by itself because using any other ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    18

    Area of a circle

    area = PI*(d_gasket*d_gasket-d_hole*d_hole)/4.0
    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.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    What is the error? We're not mind-readers!

  3. #3
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by spazx View Post
    area = PI*(d_gasket*d_gasket-d_hole*d_hole)/4.0
    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?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    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.
    Last edited by Adak; 09-14-2010 at 10:39 AM.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    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.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    #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;
    }

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    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;
    }

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by Adak View Post
    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.

    That's a fact, and a pretty dang good pun, at the same time.
    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.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by spazx View Post
    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.
    Last edited by MWAAAHAAA; 09-14-2010 at 10:49 AM.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    Expression Syntax, Floating point error and Statement missing ";"
    The line is currently:
    area = (PI*(d_gasket*d_gasket-d_hole*d_hole))/4.0;

  11. #11
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Code:
    Quote Originally Posted by spazx View Post
    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 ';'

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    The PI declaration was what was wrong with it, my syntax after the change of the declaration was fine. Thank you.

  13. #13
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by spazx View Post
    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.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by spazx View Post
    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.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    40
    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);

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. area of a circle
    By wise_ron in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:15 PM
  2. Circle Area
    By Zophixan in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2002, 11:50 AM
  3. Whats wrong with my find Circle Area program?[compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 22
    Last Post: 06-16-2002, 02:49 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21