
Reading points from user and calculating the distance between them in the Euclidean space in C
Algorithm
Input: Input 4 points from user
output: Displays the distance between point 1&2 and between point 3&4 and the sum of the distances.
- Start
- Read x1,x2,x3,x4,y1,y2,y3,y4 from user, which are the co-ordinate of points 1,2,3 and 4
- d1 = pow(pow((d.x2-d.x1),2) + pow((d.y2-d.y1),2),0.5)
- d2 = pow(pow((d.x3-d.x4),2) + pow((d.y3-d.y4),2),0.5)
- d.s = d1 + d2 (d is a variable for a structure)
- Print "The distance between 1st and 2nd is ", d1
- Print "The distance between 3rd and 4th is", d2
- Print "The sum of distance between 1st,2nd,3rd and 4th point is ", d.s
- Stop
C Program
#include <stdio.h>
#include <math.h>
struct distance{
float x1,x2,y1,y2,x3,x4,y3,y4,s;
}d;
int main()
{
float d1,d2;
//Inputing data for first distance
printf("Enter the x-coordinate of 1st point: ");
scanf("%f", &d.x1);
printf("Enter the y-coordinate of 1st point: ");
scanf("%f", &d.y1);
printf("Enter the x-coordinate of 2nd point: ");
scanf("%f", &d.x2);
printf("Enter the y-coordinate of 2nd point: ");
scanf("%f", &d.y2);
//Inputing data for second distance
printf("Enter the x-coordinate of 3rd point: ");
scanf("%f", &d.x3);
printf("Enter the y-coordinate of 3rd point: ");
scanf("%f", &d.y3);
printf("Enter the x-coordinate of 4th point: ");
scanf("%f", &d.x4);
printf("Enter the y-coordinate of 4th point: ");
scanf("%f", &d.y4);
//Calculating the distances
d1 = pow(pow((d.x2-d.x1),2)+pow((d.y2-d.y1),2),0.5);
d2 = pow(pow((d.x3-d.x4),2)+pow((d.y3-d.y4),2),0.5);
d.s= d1+d2;
//Displaying values to user
printf("The distance between 1st and 2nd is %f\n", d1);
printf("The distance between 3rd and 4th is %f\n", d2);
printf("The sum of distance between 1st,2nd,3rd and 4th point is %f",d.s);
return 0;
}
Output
Enter the x-coordinate of 1st point: 2
Enter the y-coordinate of 1st point: 3
Enter the x-coordinate of 2nd point: 3
Enter the y-coordinate of 2nd point: 4
Enter the x-coordinate of 3rd point: 4
Enter the y-coordinate of 3rd point: 5
Enter the x-coordinate of 4th point: 5
Enter the y-coordinate of 4th point: 6
The distance between 1st and 2nd is 1.414214
The distance between 3rd and 4th is 1.414214
The sum of distance between 1st,2nd,3rd and 4th point is 2.828427
Comments