Thread: calculate area of circle using pointers

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    7

    calculate area of circle using pointers

    Hi all,

    I found a nice cache of C problems to attempt to solve on a website via google search. It basically lists them one by one. You can look at the code solution by clicking on the hyper link of the problem itself. But I am trying to figure them out on my own.

    So I found one that said "calculate the area of a circle using pointers" and this is what I came up with. Was hoping you can tell me if this looks right. It gives a proper answer, but maybe there's better ways of doing it.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define PI 3.14
    
    float exponent(float *r);
    void f_area(float *sq_radius);
    
    int main(void) {
     // Area of circle using pointer
     
     float radius;
     
     printf("Please enter the radius: ");
     scanf("%f", &radius);
     exponent(&radius);
     f_area(&radius);
     
     return 0;
    }
    
    float exponent(float *r)
    {
     *r = pow(*r, 2);
     return *r;
    }
    
    void f_area(float *sq_radius)
    {
     float area;
     area = PI * (*sq_radius);
     printf("The area of the circle is %2.2f", area);
     
     return;
    }
    edit: already realized I could get the same answer by changing exponent to a void and just return; instead of return r;
    Last edited by DustBin; 05-20-2016 at 10:26 AM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You should provide the link to the questions.

    The question "calculate the area of a circle using pointers" is strange and not easy to interpret.

    Your value for PI is very imprecise!

    It's better to use double for the extra precision. Only use float if there's a reason to do so (space, speed).

    It's best to calculate the square of a number by simply multiplying it by itself. No reason to use the general (presumably slower) pow function.

    Your misnamed "exponent" function doesn't need to return the answer since it's modifying the value of the caller.

    Requiring the square of the radius in your f_area function is odd. Usually such a function would take the radius and be named something like calc_circle_area. It also wouldn't print the answer itself but instead simply return the answer so the caller can do whatever it wants.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    7
    Your value for PI is very imprecise!
    It is and I don't really care at the moment. I'm just experimenting with pointers and functions. I guess if I were working for a company and mathematical precision was important, yes, I would use more digits after the decimal and switch to double. For now, every beginner's C book I've looked at uses float with such small digits and that's what I've been doing. Just saying.


    It's best to calculate the square of a number by simply multiplying it by itself. No reason to use the general (presumably slower) pow function.
    Point taken.

    Your misnamed "exponent" function doesn't need to return the answer since it's modifying the value of the caller.
    I assume you mean its misnamed because of what the function actually does isn't directly in reference to exponents? Because I'm pretty sure I spelled "exponent" correctly. "The exponent of a number says how many times to use the number in a multiplication. In 8 2 the "2" exponent says to use 8 twice in a multiplication." The exponent here is two. So I'm not sure if you're just nitpicking?



    Requiring the square of the radius in your f_area function is odd. Usually such a function would take the radius and be named something like calc_circle_area. It also wouldn't print the answer itself but instead simply return the answer so the caller can do whatever it wants.
    Not sure what you mean by this. Could you provide an example?

    Basic C Programs : C Programs A-Z - Basic C Programs | C Programming Examples Here's the link.. bunch of beginner problems. I guess some of it is left up to interpretation to the user how to solve them. I used pointers in conjunction with functions to attempt to solve the problem. What I'm really asking is if there's another approach using pointers that might be inferred that I completely missed? A simpler way than what I concocted..
    Last edited by DustBin; 05-20-2016 at 12:00 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not sure what you mean by this. Could you provide an example?
    Well, basically, it is easier to reuse a function that does one thing only, like calculate the area of a circle. The code you gave will always call printf() with that information, when it could be used in alternate ways.

    And alternate uses can be necessary or even fun:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    
    double CircleArea(double radius)
    {
       return 3.141593 * radius * radius;
    }
    
    
    int main()
    {
       char message[1000] = "";
       double slices = 2, total_slices = 9, size = 10;
       
       sprintf(message, "You ate %2.1f square inches of pizza!\n", CircleArea(size / 2) * slices / total_slices);
       MessageBox(NULL, message, "Pizza Alert", MB_OK);
    
       return 0;
    }
    The amount of utility you can get out of some code effects how much work you need to do as a programmer, in general.
    Last edited by whiteflags; 05-20-2016 at 02:20 PM.

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    7

    Thumbs up

    Thanks for the advice. I tried updating it and came up with this.

    Code:
    #include <stdio.h>
    #define PI 3.141592
    
    void CircleArea(double *area, int radius);
     
    int main(void) {
      
        double radius;
        double pCircArea;
         
        printf("Please enter the radius: ");
        scanf("%lf", &radius);
        CircleArea(&pCircArea, radius);
        printf("%lf", pCircArea);
         
        return 0;
    }
     
     
    void CircleArea(double *area, int radius) {
        *area = PI * radius * radius; 
    }
    I'm a noob so I will keep asking noob questions. The more advice the merrier. But if I don't understand something I will question it to make sure I'm not lost. Hope that doesn't bother anyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-09-2015, 08:03 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

Tags for this Thread