Write a program that calculates the distance between two points (Please help me edit)

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>
int main()

{
	float x1; 
	float y1; 
	float x2; 
	float y2; 
	float Distance;
	x1 = 7;
	y1 = 12;
	x2 = 3;
	y2 = 9;
	Distance = sqrt(pow((x1-x2),2)+pow((y1-y2),2));
	printf("The distance between points (7,12) and (3,9) is f%", Distance);
	
	return 0;
	
}


I have to have this output
The distance between points (7,12) and (3,9) is 5.000000

This is what I got
The distance between points (7,12) and (3,9) is f


*I just realized this program made no sense. Not for the X and Y anyway. The formula works. I just can't get the right output.

Here is my exact problem:
Write, compile, and execute a C program that calculates the distance between two points whose coordinates are (7,12) and (3,9). Use the fact that the distance between two points having coordinates (x1,y1) and (x2,y2) is distance = sqrt([x1-x2]^2 + [y1-y2]^2).
Last edited on
Format specifiers are %(d|f|s|etc..), not (d|f|s|etc..)%

Also, your formula is a little mixed up: d2 = (x2 - x1)2 + (y2 - y1)2

15
16
Distance = sqrt(pow((x2-x1),2)+pow((y2-y1),2));
printf("The distance between points (7,12) and (3,9) is %f", Distance);


Hope that helps.
Thanks Danny, simple mistake on the %f. I had a brain fart. I would have sat here all night being upset about why my program doesnt work.
@Kevin

The pow function is expensive for squaring numbers because it uses a series expansion. So create 2 new variables DeltaX and DeltaY say, then just multiply them:

Distance = sqrt(DeltaX * DeltaX + DeltaY * DeltaY ) ;

Also, prefer to use double rather than float. Float has 7 or 8 significant figures of precision and this is easily exceeded, while double has 15 or 16 so it is suitable for most things.

Cheers
@IdeaMan, I'll keep that in mind! I'm currently working on my last program. I'm sure it'll mess up and I'll have to come back and rely on y'all once more!
Topic archived. No new replies allowed.