× Home
Next Lec → ← Previous Lec

Area of the circle using function pointer exercise

Take coordinates of two points from user and by using the Euclid's formula find the distance between two points and find the area of circle with radius as the distance between two coordinates.
Try using function pointer

                        

                                #include <stdio.h>
                                #include <math.h>
                                
                                #define PI 3.14159265
                                
                                float euclidDis(int x1, int y1, int x2, int y2, float (*ptr)(float))
                                {
                                    float result;
                                    result = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
                                    printf("The distance between two given points are : %f\n", result);
                                    printf("The area of the circle with radius %f is %f",result, ptr(result));   
                                }
                                float circleArea(float radius)
                                {    
                                    return (PI * pow(radius, 2));
                                }
                                int main()
                                {
                                    float (*rad)(float);
                                    rad = circleArea;
                                    int x1, y1, x2, y2;
                                    printf("Enter the coordinates of first point : \n");
                                    scanf("%d", &x1);
                                    scanf("%d", &y1);
                                    printf("\nEnter the coordinates of second point : ");
                                    scanf("%d", &x2);
                                    scanf("%d", &y2);
                                    euclidDis(x1, y1, x2, y2, rad);
                                    return 0;
                                }
                        
                    
                        

                                #include <stdio.h>
                                #include <stdlib.h>
                                #include <math.h>
                                
                                float Edistance(int x1, int y1, int x2, int y2)
                                {
                                    int a = (y2-y1)*(y2-y1)+(x2-x1)*(x2-x1);
                                    return sqrt(a);
                                }
                                
                                float areaOfCircle(int x1, int y1, int x2, int y2, float (*distance)(int x1, int y1, int x2, int y2))
                                {
                                    return distance(x1, y1, x2, y2);
                                }
                                
                                int main()
                                {
                                    int x1, y1, x2, y2;
                                    float dst;
                                    // Take x1, y1 and x2, y2 from the user using scanf
                                    printf("Enter the value of x1\n");
                                    scanf("%d", &x1);
                                
                                    printf("Enter the value of x1\n");
                                    scanf("%d", &y1);
                                
                                    printf("Enter the value of x1\n");
                                    scanf("%d", &x2);
                                
                                    printf("Enter the value of x1\n");
                                    scanf("%d", &y2);
                                    dst = areaOfCircle(x1, y1, x2, y2, Edistance);
                                    printf("The distance between these points is %.2f\n", dst);
                                    return 0;
                                }