Distance Between Points

I need some help. I have to create a function that will calculate the distance between points (x1,y1) and (x2, y2). All numbers are of type double. I keep getting incorrect output. I am usinge visual studio and C language. Here is my code.

#include <stdio.h>
#include <math.h> 


double calculate_distance (double x1,double y1,double x2 ,double y2)

{

double distance;

double distance_x = x1-x2;

double distance_y = y1- y2; 

distance = sqrt( (distance_x * distance_x) + (distance_y * distance_y));

return distance;

}

int main ()
{
double x1;

double x2;

double y1;

double y2; 


printf ("Let me help you find the distance between two points (x1,y1) and (x2, y2)."); 

printf ("\n\nEnter coordinate for x1:");
scanf ("%f", &x1); 

printf ("\nEnter coordinate for y1:");
scanf ("%f", &y1); 


printf ("\nEnter coordinate for x2:");
scanf ("%f", &x2); 


printf ("\nEnter coordinate for y2:");
scanf ("%f", &y2); 


printf ("The distance between (%f,%f) and (%f,%f) is %.2f\n\n", x1,y1,x2,y2, calculate_distance(x1,y1,x2,y2)); 


return 0;

} 

 

转载于:https://www.cnblogs.com/kissfu/p/3363192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值