C ++ Sqrt函数

时间:2013-01-23 01:34:26

标签: c++ function sqrt

当我编译我的程序时,它似乎没有执行我的公式我不知道我做错了什么帮助将不胜感激

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;
 }

6 个答案:

答案 0 :(得分:2)

将距离,Xvalue和Yvalue改为双打

答案 1 :(得分:1)

我很确定这可能是您问题的一部分:

Xvalue = (x1 - x2);
Yvalue = (y1 - y1);

它应该是:

Xvalue = (x1 - x2);
Yvalue = (y1 - y2);

答案 2 :(得分:1)

双变量的差异可以是double,而Yvalue总是计算为zero

实际上,你的公式本身是错误的。

Distance Formula: Given the two points (x1, y1) and (x2, y2), 

这些点之间的距离由公式给出:

d = sqrt((x2-x1)^2 + (y2-y1)^2)

请注意,您正在减去而不是添加差异的平方。

double x1,y1,x2,y2,distance, Xvalue, Yvalue;
Xvalue = (x1 - x2);
Yvalue = (y1 - y2);
distance = sqrt(Xvalue * Xvalue + Yvalue * Yvalue);

答案 3 :(得分:1)

距离= sqrt(pow((x2-x1),2)+ pow((y2-y1),2));

答案 4 :(得分:0)

首先输入cout输入值,以便确定输入中是否存在问题

cout<<x1<<endl;
cout<<x2<<endl;
cout<<y1<<endl;
cout<<y2<<endl;

然后你想尝试... cout!

cout<<"this is the"<< distance << cout ... // cout again, is not very good!

cout<< "this is the"<< distance <<endl;
cout << endl << endl;

并且无论如何..除非你因为特定原因需要那些“int”,否则最好有双打。 (你仍然可以稍后用“floor(value)”将它们向下舍入)

答案 5 :(得分:-1)

包括

包括

int main(void)

{ 浮点数x1,x2,y1,y2,距离;

printf(“ x2的值:”); scanf(“%f”,&x2);

printf(“ x1的值:”); scanf(“%f”,&x1);

printf(“ y2的值:”); scanf(“%f”,&y2);

printf(“ y1的值:”); scanf(“%f”,&y1);

距离= sqrt(pow((x2-x1),2)+ pow((y2-y1),2)); printf(“(X2,X1)和(Y2,Y1)之间的距离为%f”,距离);

返回0;

}