0

This program finds the distance between two points on a plane capable of floating points. But what I can't figure out (which might be due to my ignorance) is the problem with computing the dist variable. It is set to a Pythagorean equation that returns what should be the distance. All other variables in the system are correct.

What I am mostly having a problem though is an obvious bug with the sqrt() in it's use in this example. For example, if I were to make xdist = 1 with everything else set to 0, the distance should be 1. However, what is printed as dist is exactly 1072693248. I only found one question related to this number. But what was mentioned in the answers was that only the wrong use of %d in printf caused that issue, but I see (to what I know) there isn't a problem with printf and formatting anywhere in the program. What is the solution to get the correct value?

double distance(xdist, ydist)
{
    return sqrtf(pow(xdist, 2.0) + pow(ydist, 2.0));
}

int main()
{
    double x1, y1, x2, y2;

    double xdist, ydist, dist;

    int i;

    for (i = 1; i <= 2; i++)
    {
            printf("%d: Enter an x and y coordinate:\n", i);
            switch (i)
            {
                case 1:
                    scanf("%lf", &x1);
                    scanf("%lf", &y1);
                    continue;
                case 2:
                    scanf("%lf", &x2);
                    scanf("%lf", &y2);
                    continue;
            }
    }

    xdist = fabs(x2 - x1);
    ydist = fabs(y2 - y1);

    dist = distance(xdist, ydist);

    printf("\nxdist = %.2f, ydist = %.2f\n", xdist, ydist);
    printf("The distance between coordinates \n(%.2f, %.2f) to (%.2f, %.2f) \nis %.2f.", x1, y1, x2, y2, dist);

    return 0;
}
Community
  • 1
  • 1
  • 1
    `double distance(xdist, ydist)` ... call me ignorant about C programming, but don't you need to declare the types of `xdist` and `ydist`? – Tim Biegeleisen Dec 15 '16 at 12:32
  • Also, you don't need to take the absolute value of the difference between the x and y ordinates, as squaring loses the sign anyway (the codomain of y=x^2 where x is real rather than complex, is y >= 0). – Evil Dog Pie Dec 15 '16 at 12:37
  • 1) makes more sense to use `sqrt()` than `sqrtf()` in `double distance(xdist, ydist) { return sqrtf(pow(xdist, 2.0) + pow(ydist, 2.0));}` 2) Simplify `double distance(double xdist, double ydist) { return hypot(xdist, ydist); }` – chux - Reinstate Monica Dec 15 '16 at 16:02
  • I used a lot of the suggestions with this question and it's combinations but I still can't find it to work. I think it might have something to do with my compiler. I tried it at school today and it worked flawlessly (the only thing changed was the distance() arguments set to their intended data type). – Tristen Woodruff Dec 15 '16 at 21:48

2 Answers2

4

Type of arguments are not defined in distance function so it will be default int. you have to define their type double

 double distance(double xdist, double ydist)
e.jahandar
  • 1,715
  • 12
  • 30
0

I found my solution. Problems with the scanf() caused some weird printing of floats I think. I had switched "%lf" to "%f" and then to "%lf" again. But within that I don't know what fixed it exactly. The final code is:

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

double distance(double xdist, double ydist)
{
    return hypot(xdist, ydist);
}

int main()
{
    double x1, y1, x2, y2;

    double xdist, ydist, dist;

    int i;

    for (i = 1; i <= 2; i++)
    {
            printf("%d: Enter an x and y coordinate:\n", i);
            switch (i)
            {
                case 1:
                    scanf("%lf", &x1);
                    scanf("%lf", &y1);
                    continue;
                case 2:
                    scanf("%lf", &x2);
                    scanf("%lf", &y2);
                    continue;
            }
    }

    xdist = x2 - x1;
    ydist = y2 - y1;

    dist = distance(xdist, ydist);

    printf("\nxdist = %.2f, ydist = %.2f\n", xdist, ydist);
    printf("The distance between coordinates \n(%.2f, %.2f) to (%.2f, %.2f) \nis %.2f.", x1, y1, x2, y2, dist);

    return 0;
}

But if I were to guess, what fixed this was simplified use of hypot(), removal of fabs() to find the ydist and xdist, and the distance() function taking double parameters.