0

What is wrong with this

#include <iostream> #include <math.h> using namespace std; double x1; double y1; double x2; double y2; double k; int distance(x1,y1,x2,y2); int main() { cin>>x1; cin>>y1; cin>>x2; cin>>y2; distance(x1,y1,x2,y2); cout<<" the distance between points "<<"("<<x1<<","<<y1<<")"<<" and "<<"("<<x2<<","<<y2<<")"<< " is "<<k<<endl; return 0; } int distance(x1,y1,x2,y2){ k=pow(pow(x2 - x1,2) + pow(y2 - y1,2),1.0/2); return k; }

31st May 2018, 1:36 PM
Vikram
Vikram - avatar
2 ответов
+ 4
#include <iostream> #include <math.h> using namespace std; int distance(double x1,double y1,double x2,double y2); double k; // global variable was not declared int main() { double x1; double y1; double x2; double y2; cin>>x1; cin>>y1; cin>>x2; cin>>y2; distance(x1,y1,x2,y2); //data types were specified which caused error cout<<" the distance between points "<<"("<<x1<<","<<y1<<")"<<" and "<<"("<<x2<<","<<y2<<")"<< " is "<<k<<endl; return 0; } int distance(double x1,double y1,double x2,double y2) { k=sqrt(pow(x2-x1,2) +(pow(y2-y1,2))); // extra brackets were added return k; }
31st May 2018, 2:19 PM
Nova
Nova - avatar
+ 2
When you pass variables, you do not have specify the type before them. So just call the function like this : distance(x1,y1,x2,y2);
31st May 2018, 2:02 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar