Thread: Exercise 1 --- Area of a Circle

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    42

    Exercise 1 --- Area of a Circle

    Write a program that calculates the area of a circle from the radius. The radius will be an integer read in from the keyboard. You will need to use the function pi(). According to my understanding, the functions accept a double argument and returns a double. But I was asked to use the integer for radius, so how do I pass it to the function? Another question is there a function square() because when I inserted a square it was undefined. I have been told that my codes are horrible because of the logic. What does it mean and how do I improve my codes?




    Code:
    #include<stdio.h>#include<math.h>
    #include<stdlib.h>
    #define M_PI
    
    
    double CalcRadius(int x);
    int x;
    double y;
    
    
    int main(){
    
    
    	printf("Enter the radius of the circle\n");
    	scanf("%d",&x);
    	double CalcRadius(int x);
    	printf("The area of the circle is %f\n",y);
    
    
    	return 0;
    }
    
    
    double CalcRadius(int x)
    {
    	y= M_PI*x*x;
    		return y;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For starters, you are not calling the function correctly. I suggest you read up on how to properly call functions.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I think you're just guessing and/or trying to google stuff and make it fit into whatever you're trying to do, to be honest. Reading and studying is a better approach.

    Edit: your code is not horrible because of "logic" per se. Your code horrible because you're not designing in with a top-down approach in mind. I'm not saying top-down programming is a golden bullet for all things but, come on... global variables... in something this simple? Start with top-down design and structured programming; you're not going to understand when not to use them (these concepts) until you understand them anyway.

    Edit 2: Personal peeve. Do not use the word "codes". The plural of "code" (in the sense you're expressing) is "code".
    Last edited by Hodor; 01-13-2016 at 09:05 AM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    I think that the radius should be float not just an integer.

    I also think that the function to compute area should be CalcArea instead of CalcRadius, because it does not compute radius, but area.

    Another think is that you have declared variable y but you have not assigned any value to it.

    And colleague Hodor mentioned use of global variables already.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Hodor View Post
    Edit 2: Personal peeve. Do not use the word "codes". The plural of "code" (in the sense you're expressing) is "code".
    I'd argue that "code" should actually always be used in the singular (i.e. there can be no plural), since it is considered a mass noun in computer science.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Matticus View Post
    I'd argue that "code" should actually always be used in the singular (i.e. there can be no plural), since it is considered a mass noun in computer science.
    Yes, of course it should always be singular. That's what I was saying. But if you refer to a collection of source code, it's not source codes it's still just source code.

    There is no debate about this... saying "codes" is like saying "datas".

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Oh, I'm not saying "codes" (as a noun) is acceptable in that sense. You just said "the plural of "code"...", and I pointed out how technically it should not be considered plural in the given context.

  8. #8
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    I came up with a new code. How do you use the function pi()? When do I use type double or type float?

    Code:
    #include<stdio.h>
    
    float square(float x);
    
    
    main()
    {
        float m, area;
        
        printf("Enter the radius\n");
        scanf("%f", &m);
        area = PI * square(m);
        printf("The area of the circle is %f", area);
    }
    
    
    float square(float x)
    {
        float p;
        p = x*x;
        return(p);
    }
    Last edited by nadeera; 01-14-2016 at 07:01 AM.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Looks like PI should be a constant.

    Type double or type float really depends on how much precision you want. The difference is really just length - double offers more significant figures.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There is no standard library function called "pi()". Where did you get this exercise from? All I can find is this: "Basic Programming Exercises", and it doesn't indicate any specific languages. While most of the questions appear to be generic enough for most languages, mentioning a specific function like this can be misleading.

    When do I use type double or type float?
    You should just use doubles, unless you have a specific reason to use floats.

  11. #11
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    All I can find is this: "Basic Programming Exercises", and it doesn't indicate any specific languages.
    Yes, I am trying to do the exercises from that website. So I suppose coding this way, will do?

    Code:
    #include<stdio.h>#define M_PI 3.14159265358979323846
    
    
    float square(float x);
    
    
    main()
    {
    	float m, area;
    
    
    	printf("Enter the radius\n");
    	scanf("%f", &m);
    	area = M_PI * square(m);
    	printf("The area of the circle is %f\n", area);
    }
    
    
    float square(float x)
    {
    	float p;
    
    
    	p = x*x;
    	return(p);
    }
    set1.1.c(12) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    Does this mean I should convert everything into double instead?

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nadeera View Post
    Does this mean I should convert everything into double instead?
    Yes.

    As I said, work with doubles unless you have a specific reason to use floats.

  13. #13
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    Okay, but as soon as I convert everything it gives me a completely ridiculous answer. Is there anything I missed? Can you try running it, please?

    Code:
    #include<stdio.h>#define M_PI 3.14159265358979323846
     
    double square(double x);
     
    main()
    {
        double m, area;
     
        printf("Enter the radius\n");
        scanf("%f", &m);
        area = M_PI * square(m);
        printf("The area of the circle is %f\n", area);
    }
     
     double square(double x)
    {
        double p;
        p = x*x;
        return(p);
    }

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Did you get warnings when you compiled?

    Code:
    /*
    main.c|10|warning: format '%f' expects type 'float *', but argument 2 has type 'double *'
    */
    Code:
    scanf("%f", &m);
    In "scanf()", use "%lf" for doubles (that's the lower-case letter L).
    For "printf()", the "%f" is fine as is.

  15. #15
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    Did you get warnings when you compiled?
    Nope, I did not get any warnings. I changed it as you said, and now it works perfectly. Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C++ Circle Area
    By GrafixFairy in forum C++ Programming
    Replies: 0
    Last Post: 04-27-2014, 07:49 AM
  2. Area of Circle Without Pi
    By Jhernandez860 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2013, 01:24 PM
  3. Area of a circle error, help
    By spazx in forum C Programming
    Replies: 16
    Last Post: 09-14-2010, 06:25 PM
  4. area of a circle
    By wise_ron in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:15 PM
  5. Circle Area
    By Zophixan in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2002, 12:50 PM