0

I tried to calculate distance between 2 points, A, B. When I run the terminal window it give me a false number. Can anyone help me to change some value, or stuct or many tips.

Example: in A : -50 -50 in B : 50 50 distance is 141.42

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

typedef struct{
    double a;
    double b;
    double c;
    double d;
}location;

double dist(location  w,location x, location y,location z)
{
    double l;
    l=sqrt(pow((y.c-w.a),2)+pow((z.d-x.b),2));
    return(l);
}

void main()
{
    location h;
    location i;
    location j;
    location k;
    printf("Enter 1st point(A)\n");
    scanf("%lf %lf",&h.a,&i.b);
    printf("Enter 2nd point(B)\n");
    scanf("%1f %1f",&j.c,&k.d);
    double data;
    data = dist(h,i,j,k);
    printf("%.2lf",data);
}
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 2
    141.42 is the correct Euclidean distance between those points. What do you expect the distance to be? – user1118321 Apr 26 '17 at 05:20
  • 3
    When you use the format `%1f`, at most one digit will be read in. You seem to have mistyped lower-case L and used the digit 1 instead. – paddy Apr 26 '17 at 05:21
  • 4
    The real question is why is there 4 locations in here (or why location has 4 something) –  Apr 26 '17 at 05:21
  • What did you expect to happen? – user253751 Apr 26 '17 at 05:21
  • Welcome to StackOverflow. Please take the [tour] and learn http://stackoverflow.com/help/how-to-ask. If you look for help with debugging code have a look at https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – Yunnosch Apr 26 '17 at 05:44

2 Answers2

2

Do you notice the difference between your scanf format string on the two lines:

scanf("%lf %lf",&h.a,&i.b);
scanf("%1f %1f",&j.c,&k.d);

That's right! The second line uses %1f instead of %lf. That has a completely different meaning, and in your case is wrong. Use %lf.

When you get results you don't understand, it's a great time to use a debugger, or add printf statements to check your variable values against what you expect them to be.

paddy
  • 60,864
  • 6
  • 61
  • 103
0

With the corrections from paddy the code should work, but I still think it's worth mentioning/correcting the smaller mistakes.

First of all void main() is not defined in the standard. Why is it bad to type void main() in C++

If you are using GCC try to compile with the -Wall parameter. Then you get a lot more warnings which will help you write better code in the end.

Also why do you have 4 locations with 4 members? I refactored your code a bit and I think this version is a lot easier to read and understand.

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

typedef struct {
    double x;
    double y;
} Point;

double DistanceBetween(Point p1, Point p2)
{
    Point vector = {
        p2.x - p1.x, p2.y - p1.y
    };

    return hypot(vector.x, vector.y);
}

int main()
{
    Point p1;
    Point p2;

    printf("Enter first point: ");
    scanf("%lf %lf", &p1.x, &p1.y);

    printf("Enter second point: ");
    scanf("%lf %lf", &p2.x, &p2.y);

    double distance = DistanceBetween(p1, p2);
    printf("The distance is: %lf\r\n", distance);

    return 0;
}
Community
  • 1
  • 1
sknt
  • 963
  • 6
  • 16