Help with this formula

Hello,

I am a newbie in the programming world and I am currently taking intro to C++. I took a quiz on Wednesday, and I didn't get this program to work. the program asked to find the distance between two points. I am trying to find the right formula but still with no avail.

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  //This program helps the user find the distance between two points.

#include <iostream>

#include <cmath>


using namespace std;



int main()
{

int x1, x2, y1, y2;



	double point_1 = (x1 - x2);


	double point_2 = (y1 - y2);

	double distance = pow(x2-x1*x2-x1 + y2-y1*y2-y1,0.5);
	
	

	


	cout << " Please enter your first point (x1,y1).\n Seperate using the space bar.\n" ;

	cin >> x1 >> y1 ;



	
	cout << " Please enter your second point point (x2,y2).\n Seperate using the space bar\n" ;
	
	cin >> x2 >> y2 ;



	
	cout << " The distance between the points is " << distance << endl ;
	






	return 0;




} 
double point_1 = (x1 - x2);


double point_2 = (y1 - y2);

double distance = pow(x2-x1*x2-x1 + y2-y1*y2-y1,0.5);

You dont expect that to work right?...assignment without explicit initialization is never a good idea. You must bring this piece of code after the user has assigned x1 x2 y1 and y2 values.

Aceix.
Last edited on
Your formula to work out the distance is incorrect. To get the difference between two points on a graph, you want to get the size of the smallest vector that can connect them. The formula you want is (brackets are important!):

sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

And what Aceix said has to be sorted out too.
Thank you
I'll give it a shot.
It worked thanks

:)
Topic archived. No new replies allowed.