Reputation: 11
when i compile my program it doesnt seem to execute my formula i cant figure what i am doing wrong help would be appreciated
int main ()
{
int distance, Xvalue, Yvalue;
double x1,y1,x2,y2;
cout << "\n Please enter X1 value: ";
cin >> x1;
cout << " Please enter X2 value: ";
cin >> x2;
cout << "\n Please enter Y1 value: ";
cin >> y1;
cout << " Please enter Y2 value: ";
cin >> y2;
Xvalue = (x1 - x2);
Yvalue = (y1 - y2);
distance = sqrt(Xvalue * Xvalue + Yvalue * Yvalue);
cout << "This is the distance between the two points" <<distance<<
cout << endl << endl;
system ("pause");
return 0;
}
Upvotes: 1
Views: 1567
Reputation: 9
int main(void)
{ float x1, x2, y1, y2, Distance;
printf("value of x2: "); scanf("%f",&x2);
printf("value of x1: "); scanf("%f",&x1);
printf("value of y2: "); scanf("%f",&y2);
printf("value of y1: "); scanf("%f",&y1);
Distance = sqrt(pow((x2-x1),2)+pow((y2-y1),2)); printf("The distance between (X2,X1) and (Y2,Y1) is %f", Distance);
return 0;
}
Upvotes: -1
Reputation: 8317
first cout input values so you can be sure if the problem is not in the input
cout<<x1<<endl;
cout<<x2<<endl;
cout<<y1<<endl;
cout<<y2<<endl;
then you are trying to cout... cout!
cout<<"this is the"<< distance << cout ... // cout again, is not very good!
try
cout<< "this is the"<< distance <<endl;
cout << endl << endl;
and anyway.. unless you need those "int" for specific reason is better to have doubles. (you can still round down them later with "floor(value)")
Upvotes: 0
Reputation: 6121
Difference of double variables can be a double
and your Yvalue
always computes to zero
.
Actually, your formula itself is wrong.
Distance Formula: Given the two points (x1, y1) and (x2, y2),
the distance between these points is given by the formula:
d = sqrt((x2-x1)^2 + (y2-y1)^2)
note that u are subtracting instead of adding the squares of differences.
double x1,y1,x2,y2,distance, Xvalue, Yvalue;
Xvalue = (x1 - x2);
Yvalue = (y1 - y2);
distance = sqrt(Xvalue * Xvalue + Yvalue * Yvalue);
Upvotes: 1
Reputation:
I am pretty sure this might be part of your problem:
Xvalue = (x1 - x2);
Yvalue = (y1 - y1);
it probably should be:
Xvalue = (x1 - x2);
Yvalue = (y1 - y2);
Upvotes: 1