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.
//This program helps the user find the distance between two points.
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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.
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!):