function convert() not returning calculation

I am supposed to write a function convert() that converts polar coordinates to rectangular coordinates; and then use it in a program that reads the polar coordinates for several points and calls convert(), which calculates and returns the rectangular coordinates for each point. It runs, but it's not calculating anything...The output is always coming out to 0.. 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
#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

double convert(double x,double y);
//{   int  x,y;
//	x = r cos 0;
	//y = r sin 0;
//}

int main()
{   
	double r,o,x,y;
	x=0.0;
	y=0.0;
	r=0.0;
	o=0.0;
	
	
	cout << "Enter polar coordinates. "<< endl;
	cin >> r >> o;
	cout << "The rectangular coordinates for the polar coordinates: " <<r<< " , "<< o<< " are " <<convert(x,y) <<endl;
	return 0;
}

double convert(double x,double y)


{ 
	double r =0.0, o = 0.0;
	
	return x = r * cos (o);
         y = r * sin (o);

}
Last edited on
The output is always coming out to 0..
¿What else could it be?
1
2
	double r =0.0, o = 0.0;
	return x = r * cos (o); //0.0 * 1 = 0 


1
2
double //it will return just one number.
convert(double x,double y);
You aren't using the parameters either.
When I take the 0's off then it says that r and o are being used without being initialized...?
Thank so much ne555! I got it to output a value other than zero, and I better understand how to use the function...This is the revised code...It still is only outputting one value, which I'm hoping is the ONLY bug left..
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
#include "stdafx.h"
#include <iostream>


using namespace std;

double convert(double x,double y);


int main()
{   
	int r,o;
	
	cout << "Enter polar coordinates. "<< endl;
	cin >> r >> o;
	cout << "The rectangular coordinates for the polar coordinates: " <<r<< " , "<< o<< " are " << convert(r,o) <<endl;
	return 0;
}

double convert(double x,double y)


{ 
	double xx  , yy  ;
	
	 xx = x * cos (y);
         yy = x * sin (y);
		 return xx,yy;
}
I don't know why I took the the cmath include statement out...I put it back..
The return statement will only return one value, in this case yy.

One possible solution is to create an array within convert and return that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    double input_x = 1;
    double input_y = 3.14159;

    double *output = convert(input_x,input_y);

    cout << output[0] << " " << output[1] << endl;

    delete[] output;

    return 0;
}

double *convert(double x,double y)
{
    double *xx = new double [2];

    xx[0] = x * cos (y);
    xx[1] = x * sin (y);

    return xx;
}
Last edited on
Or return an struct.
Ok, here is what I have worked out so far...I think the only issue with it now is that it's giving me a negative answer for the second value for some reason...What could be going on to cause that?

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
#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

double convert(double x,double y);

int main()
{   
	double r1,o1;

	
	
	cout << "Enter polar coordinates. "<< endl;
	cin >> r1 >> o1;
	cout << "The rectangular coordinates for the polar coordinates: " <<r1<< " , "<< o1<< " are " <<endl;
	cout << convert (r1,o1)<< endl;


	
	return 0;
}

double convert(double x,double y)


{  
	double xx  , yy;
	
	     xx = x * cos (y);
         yy = x * sin (y);
		 
        
      cout << xx << " and " << yy <<endl;
      
		return 0;
	   

}
Here is an updated version of what I have...It works but the problem is that It's out putting the wrong information and I dont know why. I am using the formula given but, I haven't had any Trig so I don't know how to calculate cos and sin manually for desk checking..
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
#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

void convert(double& r, double& o);

int _tmain(int argc, _TCHAR* argv[])

{

	char again = 'y';
	double x,y,r,o=0.0;

	     while(again == 'y' || again == 'Y')
		 {
  cout << "Enter polar coordinates of a point:" <<endl;
  cin >> r >> o;

 // x= r * cos(o);
 //y= r *sin (o);
    convert (r,o);

  	cout<<endl;
	cout<<endl;

	cout<<"Do you want run this program again? (Y/N)";
	cin>>again;
		 }

     cout<<endl;
	 cout<<endl;
	return 0;
		 
}

void convert(double& r, double& o)
{  
	double x,y;

	x= r * cos(o);
    y= r *sin (o);

    cout << " The rectangular coordinates for point P ("<<r<<","<<o<<")";
    cout << " are (" <<x<<","<<y<<")."<<endl;
}
Use a calculator maybe for desk checking.

The sin and cos functions in cmath probably assume that the angle given is in radians and not in degrees.
Convert your angle to radians before passing to sin and cos.
Topic archived. No new replies allowed.