Function calculateDistance

Function calculateDistance: The function/method calculateDistance accepts two arguments – point1 and point2 representing two points.

The function/method calculateDistance must return a floating point value representing the distance between the given two points.

Your task is to implement the function calculateDistance so that it passes all the test cases.

The following structure is used to represent Point and is already defined in the default code (Do not write these definition again in your code).

typedef struct point
{
    int X;
    int Y;
} Point;

IMPORTANT: Do not write the main() function as it is already defined.

Example Input/Output 1:
Input:
4 5
7 6

Output:
3.16

Explanation:
The first point is (4, 5).
The second point is (7, 6).
The distance between the given two points is 3.16 (The distance is printed with the precision up to 2 decimal places).

Example Input/Output 2:
Input:
9 -2
0 0

Output:
9.22

#include <stdio.h>
#include <math.h>
typedef struct point
{
    int X;
    int Y;
} Point;
double calculateDistance(Point *point1,Point *point2)
{
int x1=point1->X;
int x2=point2->X;
int y1=point1->Y;
int y2=point2->Y;
return sqrt(pow((x2-x1),2)+pow((y2-y1),2));
}
int main()
{
    Point P1, P2, P3;
    scanf("%d %dn", &P1.X, &P1.Y);
    scanf("%d %d", &P2.X, &P2.Y);
    printf("%.2lf", calculateDistance(&P1, &P2));
    return 0;
}
Previous Article

String - First and Last Pattern

Next Article

Define class Event

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *