Multiple functions assignment

Hi, I have a homework assignment where I have to ask the user to give 2 points of a graph (a,b) and then I have to find the distance between those two points and the origin. I need to make a function which calculates the distance formula and include that function in the main. So I went to the functions thread and tried to do it and I have many errors which I don't understand so if someone can explain to me what my mistakes are, it'll be great.

-----------------------------------------------------------------------------

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int distance (float a, float b);

int main ()
{
float x;
float y;

cout << "enter two numbers (x,y)"<< endl;
cin >> x;
cin >> y;


float z;
z = distance (x,y);
cout << "The result is " << z;
system ("pause");
return 0;
}

int distance (float a, float b)
{
float r;
r = sqrt(pow(a,2))+sqrt(pow(b,2));
return (r);
}
Your distance formula is wrong. It should only contain one call to sqrt.

You should also change the return type of the distance function otherwise the fractional part will be lost.
Two main errors. The return type of function distance() needs to be floating point. By the way, I'd recommend the use of type double throughout the program, rather than type float. Currently you return an integer, which will lose the fractional part of the result.

Secondly, your code for √(a² + b²) is not correct.
Last edited on
Topic archived. No new replies allowed.