zoukankan      html  css  js  c++  java
  • 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 ("
    
    Enter coordinate for x1:");
    scanf ("%f", &x1); 
    
    printf ("
    Enter coordinate for y1:");
    scanf ("%f", &y1); 
    
    
    printf ("
    Enter coordinate for x2:");
    scanf ("%f", &x2); 
    
    
    printf ("
    Enter coordinate for y2:");
    scanf ("%f", &y2); 
    
    
    printf ("The distance between (%f,%f) and (%f,%f) is %.2f
    
    ", x1,y1,x2,y2, calculate_distance(x1,y1,x2,y2)); 
    
    
    return 0;
    
    } 
  • 相关阅读:
    8.5
    8.12
    8.11
    8.14
    8.15
    8.18
    8.16
    8.20
    Android新版NDK环境配置(免Cygwin)
    在Windows7上搭建Cocos2d-x win32开发环境
  • 原文地址:https://www.cnblogs.com/kissfu/p/3363192.html
Copyright © 2011-2022 走看看