0

I want to calculate the distance between two points but I need to do it by using a main function. I am having some difficulties in trying to make sure that my program returns the correct value. I have attached my code to this so I would appreciate it if anyone can help in correcting the portion that I may have made a mistake. (Note: I am fairly new to C so I may need some additional help in understanding some things.)

#include <stdio.h>
double distance(double x1, double y1, double x2, double y2)
{
    double square_difference_x = (x2 - x1) * (x2 - x1);
    double square_difference_y = (y2 - y1) * (y2 - y1);
    double sum = square_difference_x + square_difference_y;
    double z = 0.00001;
    for (double i = 0; i < sum; i = i + z)
    {
        if (i * i == sum)
        {
            return i;
        }
    }
}

int main(void)
{
    double a = 1.0, b = 2.0, c = 4.0, d = 6.0;
    double dis;
    dis = distance(a, b, c, d);
    printf("The distance of the points (%lf, %lf) and (%lf, %lf) is %lf\n",  a,b,c,d,dis);

    return 0;
}

I feel like my problem is happening with the return option in the main function although I am not so sure how to fix this problem if there is one. `

math_user
  • 105
  • 1
  • 1
  • 6
  • 4
    `distance` may very well not return a value, because the `i * i == sum` test is likely to miss. Floating-point numbers on computers are [generally not accurate](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency/3730040#3730040), and even if they were, that would probably still frequently not hit. You may want to just use `sqrt(sum)` from ``, with the added benefit of being faster than a linear search to the square root. If you can't, at least, you'll probably want to do `i * i >= sum`, which will likely hit at some point. – zneak Dec 02 '16 at 18:29
  • 2
    Also note that for very large numbers, `i + 0.00001` will actually return a value identical to `i`, which would stall your program in an endless loop. – zneak Dec 02 '16 at 18:31
  • 1
    The `printf` format for `double` is `%f`. – Weather Vane Dec 02 '16 at 18:32
  • @WeatherVane `"%lf"` is an acceptable `printf()` specifier for `double` since C99. – chux - Reinstate Monica Dec 02 '16 at 20:06
  • @chux is everything promoted to `long double`? – Weather Vane Dec 02 '16 at 20:11
  • @zneak Detail: The link [generally not accurate](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency/3730040#3730040) refers to inaccuracies of binary FP of base 10 numbers. That does not apply here as OP problems exist with a binary, decimal or any base FP math. – chux - Reinstate Monica Dec 02 '16 at 20:12
  • @WeatherVane Unclear why you mention `long double`? Note that a `printf` specifier for `long double` is `"%Lf"`, not `"%lf"`. – chux - Reinstate Monica Dec 02 '16 at 20:13
  • @chux oh OK thank you. – Weather Vane Dec 02 '16 at 20:14

3 Answers3

3

This way better and efficient solution.

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

double distance(double x1, double y1, double x2, double y2)
{
    double square_difference_x = (x2 - x1) * (x2 - x1);
    double square_difference_y = (y2 - y1) * (y2 - y1);
    double sum = square_difference_x + square_difference_y;
    double value = sqrt(sum);
    return value;
}
MSH
  • 297
  • 1
  • 3
  • 12
1

This is just expanding on Weather Vane's comment about using hypotenuse.

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


typedef struct
{
    double x, y;
} Point;

typedef struct
{
    Point a,b;
} Line;


double length( Line line )
{
    return( hypot(line.b.x - line.a.x, line.b.y - line.a.y) );
} 


int main(int argc, char *argv[])
{
    Line line = { {4,0},{0,3} };

    printf("Line length = %lf\n", length(line));
    return( 0 );
}

This shows the well known 3,4,5 triangle. Compile with gcc xxx.c -lm

Chimera
  • 5,884
  • 7
  • 49
  • 81
0
#include <iostream>
#include <math.h>
using namespace std;

int main() {
    int t,i,a,b,c,d,x,y;
    double r;
    cin>>t;
    for(i=0;i<t;i++)
    {
        cin>>a>>b>>c>>d;
        x=abs(a-c);
        y=abs(b-d);

        r=sqrt((x*x)+(y*y));
        if(r-floor(r)>0.500000)
        cout<<ceil(r)<<endl;
        else
        cout<<floor(r)<<endl;
    }
    return 0;
}