Functions help please!!!

Hey guys my ficking code wont work! Im suppose to return the polar form for the points x and y. The rectangular-to-polar function should take two arguments as input (x, y) and return two values (r, ө) where:
r = mag (x,y)
ө = atan2(y,x)
note: rectangular coordinate (1,1), is equivalent to polar coordinate (1.414, PI/4);

Here is my code but it wont return any values for r and theta? What am i doing wrong????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<cmath>

using namespace std;

double mag(double xParameter, double yParameter);


double rect2polar(double xParameter, double yParameter, double rParameter, double thetaParameter);

//............................................................

int main(void)
{
    double x = 0;
    double y = 0;
    double v=3.0, w=4.0;
    double m;

    cout << "Please enter the X coordinate: ";
    cin >> x;
    cout << "Please enter the Y coordinate: ";
    cin >> y;

    m = mag(x,y);       // you need to write this function!
    cout << "The magnitude of ( "<< x << " , "<< y <<" ) is: " << m << endl;
    
    m = mag(v,w);       // now get the magnitude of vector (v,w)
    cout << "The magnitude of ( "<< v << " , "<< w <<" ) is: " << m << endl;
            
   
    double r=0, theta=0;
    
    rect2polar(x,y,r,theta);
 
    cout << "the polar form of ( "<< x << " , "<< y <<" ) is: ( " << r << " , " << theta << " )"<<endl;
    
	rect2polar(v,w,r,theta);
 
    cout << "the polar form of ( "<< v << " , "<< w <<" ) is: ( " << r << " , " << theta << " )"<<endl;
 

    return 0;
}


double mag(double xParameter, double yParameter)
{
	double addition = 0.0, squareRt = 0.0;
	addition = (xParameter*xParameter) + (yParameter*yParameter);
	squareRt = sqrt(addition);
	return squareRt;
}


//......................................................


double rect2polar(double xParameter, double yParameter, double rParameter, double thetaParameter)
{
	double addition = 0.0, squareRt = 0.0;
	addition = (xParameter*xParameter) + (yParameter*yParameter);
	squareRt = sqrt(addition);
	rParameter = squareRt;
	thetaParameter = atan2(yParameter, xParameter);
	return rParameter, thetaParameter;
}


It also says if a function has more than one output, it is customary to write a void function (a function whose return type is void) and have it pass its results back through two or more reference parameters but I have tried and failed many times!
Last edited on
1) it is ino a void function and in takes all arguments by value, therefore you cannot make changes visible outside.

You might change function declaration to
void rect2polar(double x, double y, double& r, double& theta)
and get rid of return. That way you do not have to change anything else in your code.

Better way to do that will be either declare your own structure describing a point and return it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct CartPoint
{
    int x;
    int y;
};

struct PolarPoint
{
    int r;
    int t;
};

//...
PolarPoint rect2Polar(CartPoint inp)
{
    PolarPoint res;
    res.r = std::sqrt( (inp.x*inp.x) + (inp.y*inp.y) );
    res.t = std::atan2(inp.x, inp.y);
    return res;
}

//Usage:
    CartPoint p;
    cout << "Please enter the X coordinate: ";
    cin >> p.x;
    cout << "Please enter the Y coordinate: ";
    cin >> p.y;

    PoalrPoint n;
    n = rect2polar(p);
//do stuff with n 
Or you could use standard data type std::pair:
1
2
3
4
#include <utility>
using point = std::pair<double, double>
point rect2polar(point);
//... 
Last edited on
closed account (o3hC5Di1)
Edit: Sorry, I was typing at the same time as MiiNiPaa, I addressed your problem, but MiiNiPaa's suggestion of using a struct is a very good one which you should consider.


Hi there,

It also says if a function has more than one output, it is customary to write a void function (a function whose return type is void) and have it pass its results back through two or more reference parameters


That is indeed an option, another option would be to return a std::pair or std::tuple.

Now, as for your function - you are not passing anything in as references at all, which means the parameters are copied into the functions local scope and are thus deleted when the function returns. Passing by reference means you are actually passing the address of the values in memory into the funcition, allowing you to access and alter the exact same variables which you passed in.

In order to do so:

1
2
3
4
5
6
7
8
9
10
//Notice the "void" and "&" after the typename
void rect2polar(double xParameter, double yParameter, double& rParameter, double& thetaParameter) 
{
	double addition = 0.0, squareRt = 0.0;
	addition = (xParameter*xParameter) + (yParameter*yParameter);
	squareRt = sqrt(addition);
	rParameter = squareRt;
	thetaParameter = atan2(yParameter, xParameter);
	return rParameter, thetaParameter; //void function, doesn't return a value
}


For more information:

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

Hope that helps.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.