0

So, I was trying to code a program which asks the user to input the points of a graph to calculate the euclidean distance and uses it as a radius to give the area of a circle.
Here's my code:

/*You have to take four points(x1,y1,x2,y2) from the user and use it radius to find area of a circle. To find the distance between these points, you will use the Euclidean distance formula.*/
#include <stdio.h>
#include <math.h>
#define PI 3.14

float euclideanDistance(float x1, float x2, float y1, float y2)
{
    float ed = 0;
    ed = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    return ed;
}
int areaOfCircle(float x1, float y1, float x2, float y2, float(ed)(float x1, float y1, float x2, float y2))
{
    return PI * (*ed)(x1, y1, x2, y2) * (*ed)(x1, y1, x2, y2);
//not sure if it's correct or not. No error squiggles.

}
int main()
{
    float x1, y1, x2, y2;
    float (*fptr)(float, float, float, float);
    fptr = euclideanDistance;
    printf("Enter the four points x1,y1,x2,y2 to calculate the Euclidean Distance.\n");
    printf("x1:");
    scanf("%f", &x1);
    printf("y1:");
    scanf("%f", &y1);
    printf("x2:");
    scanf("%f", &x2);
    printf("y2:");
    scanf("%f", &y2);
    ;
    printf("The euclidean distance is %f", fptr(x1, x2, y1, y2));

    printf("The area of the circle which has the above mentioned Euclidean Distance as it's radius is: %f", areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2))); //error in this printf.

    return 0;
}

There are two problems over here.
I am not getting how to use the function euclideanDistance as a radius in areaOfCircle and second is how to implement it in my main function.
For the second problem In the VS Code it's showing me the error that.

{"message": "argument of type "float" is incompatible with parameter of type "float (*)(float x1, float y1, float x2, float y2)""}


Please explain what I'm doing wrong and guide me.
el Drago
  • 35
  • 5
  • 1
    `float(ed)(float x1, float y1, float x2, float y2)` That is not a function pointer. Compare it to how you have defined the function pointer in `main`. Also, it is common to define a typedef for function pointers to make it easier to use in multiple places: `typedef float (*ed_func)(float x1, float y1, float x2, float y2);` and then use as `ed_func fptr = euclideanDistance;` – kaylum Jun 02 '21 at 04:14
  • @kaylum That does work as a function pointer, despite the missing `*`: see https://godbolt.org/z/sz6sjqK36 and [Function pointer parameter without asterisk](https://stackoverflow.com/q/57143586/7509065) – Joseph Sible-Reinstate Monica Jun 02 '21 at 04:23
  • @JosephSible-ReinstateMonica Interesting. I didn't know that. – kaylum Jun 02 '21 at 04:27

1 Answers1

0

The problem is that you're basically double-calling euclideanDistance. In main, you do areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2)), and then in areaOfCircle, you do (*ed)(x1, y1, x2, y2). To pass a function pointer, you just pass it like any other kind of pointer, rather than calling it with arguments. In other words, change areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2)) to areaOfCircle(x1, x2, y1, y1, fptr).

  • thanks everyone who helped me with this program. In addition to this problems you guys helped me with I also had to change the function areaOfCircle from int type to float. Special thanks to @JosephSible-ReinstateMonica to show me the answer. It really helped. – el Drago Jun 02 '21 at 10:10