Converting Rectangular to Polar Coordinates

Pages: 12
1) Write a method that takes as input parameters a pair of Cartesian coordinates and which sets as output parameters a pair of polar coordinates corresponding to the same point as the input Cartesian coordinates. Use degrees for output angles.

2) Write a main program that:
- prompts a user to input several pairs of positive Cartesian coordinates, one pair per line and separated by spaces. The user may enter up to 50 pairs, and the user will say he/she is done by inputting the coordinate pair (0, 0).
- Store the pairs in a pair of parallel arrays as you read them in.
shifts all coordinates to the right by 4 units and up by 7 units.
- Converts all shifted coordinate pairs to polar coordinates by calling the method you wrote.
-Prints lines showing both the input coordinates and output coordinates as ordered pairs in the style of this example: (4, 1) ---> (11.3137, 45)

This is what I have to do
I've done some of it and I'm stuck now. I'm not sure how to make it so the user can enter 50 inputs. I tried setting some sort of count, but i dont think I did it correctly
Last edited on
We can't help without seeing your code or knowing what you are stuck on.

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
#include <iostream>
#include <math.h>
using namespace std;

void getrec(double& x, double& y);                             //To get rectangular coordinates
void polar(double x, double y, double& r, double& theta);      //To calculate polar coordinates
void showPolarCoord(double radius, double angle);              //To display polar coordinates

int main()
{
   const int SIZE = 50;
   double x[SIZE];
   double y[SIZE];
   double distance;
   double angle;
   getrec(x[SIZE],y[SIZE]);
   polar(x, y, distance, angle);
}
   
int getrec(double x[], double y[])
{   int count = 0;

    do
    {   cout << "Enter the x coordinate: ";
        cin >> x[count];
        cout << "Enter the y coordinate: ";
        cin >> y[count];
        count++;
    }
    while(count <= 50 && x[count -1] && y[count -1]);
    return count;
}

void polar(double x, double y, double& r, double& theta)
{
   const double toDegrees = 180.0/3.141593;
   r = sqrt((pow(x,2))+(pow(y,2)));
   theta = atan(y/x) * toDegrees;
   return;
}

void showPolarCoord(double radius, double angle)
{
   cout << "The polar coordinates are: " << endl;
   cout << "  Distance from origin: " << radius << endl;
   cout << "  Angle (in degrees) from x-axis: " << angle << endl;

   return;
}
 
Last edited on
First off, you need to get rid of your compile errors.

Line 13-14: Why are you using [] here? Get rid of them.
Line 15-16: What are you trying to express here?
Line 37: count is not defined.
Line 37: A space is not permitted between < and =.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

edit: Above line numbers are off by 10 as OP subsequently added code tags, but started from main(), not first line of code.
Last edited on
Thanks for replying, I really appreciate it.

In line 13-14, I used the [] because I was trying to express x and y as arrays so I can ask for the input multiple times since it says the user may enter up to 50 points. I was trying to use parallel arrays, but I'm not sure how.

In line 15-16, I was doing the shifting part where it says to shift all coordinates right by 4 and up by 7. Thought that's how its done. (I edited the post above, its a little bit more clear now).

I tried initializing count so I can use "< 50", so I can tell the program when to stop taking inputs from the user, but again, wasn't sure how.

This is my first CS class, and there's just a lot going on and a lot to learn, so I'm falling behind. The reason I dont have code tags is because I was taught to use Notepad to write my program and then run it in a compiler.

Thanks again :)
The reason I dont have code tags is because I was taught to use Notepad to write my program and then run it in a compiler.


The editor you use is irrelevant here, AbstractionAnon would like you to display the code on this forum correctly. He gave you a big hint and an article to read - so take that on board and help him to help you :+)

Cheers
The correct way to declare arrays is as follows:
1
2
3
4
  const int SIZE = 50;    //  Declare max array size
...
  double x[SIZE];
  double y[SIZE];


The following two lines make no sense and should be removed.
14
15
double [x+4];    
double [y+7];


These two lines do nothing:
20
21
    int count;  //  These lines don't do anything.
    count = 0;


You have not corrected the syntax errors I pointed out previously.

Here's a corrected version of getrec:
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//  Returns number of coordinates entered
int getrec (double x[], double y[])
{   int count = 0;
    
    do
    {   cout << "Enter the x coordinate: ";
        cin >> x[count];
        cout << "Enter the y coordinate: ";
        cin >> y[count];
        count++;
    }
    while (count < SIZE && x[count-1] && y[count-1]);
    return count;
}


Code tags have nothing to do with notepad or your compiler. They are a formatting convention on this site. As I previously stated, they help to read and respond to your code by allowing posters to identify specific lines in your code. If a user continues to not use code tags after being asked to do so, I will stop responding. If you can't make a minimal effort to make your code readable, then you should not expect people to help you. Read the link I provided if you don't understand how to use code tags.

edit: Corrected condition on line 33.
Last edited on
I removed my reply earlier to avoid cluttering this post, hope you don't mind :)

Anyway, I made the changes to the code above and I tried compiling it, and an error read

In function 'int main()':
17:31: error: cannot convert 'double*' to 'double' for argument '1' to 'void polar(double, double, double&, double&)'

I'm thinking of including an 'if' statement in the function 'getrec' to check if the coordinate pair entered are [0,0], would that be a good idea?

Thank you!



Can I get some help please? I still can't figure it out
What is the latest version of your code - I guess it has changed since the last time you posted it, as there have been a few posts by others since then. Post the current version and someone might be able to help.
Last edited on
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
#include <iostream>
#include <math.h>
using namespace std;

void getrec(double& x, double& y);                             //To get rectangular coordinates
void polar(double x, double y, double& r, double& theta);      //To calculate polar coordinates
void showPolarCoord(double radius, double angle);              //To display polar coordinates

int main()
{
   const int SIZE = 50;
   double x[SIZE];
   double y[SIZE];
   double distance;
   double angle;
   getrec(x[SIZE],y[SIZE]);
   polar(x, y, distance, angle);
}
   
int getrec(double x[], double y[])
{   int count = 0;

    do
    {   cout << "Enter the x coordinate: ";
        cin >> x[count];
        cout << "Enter the y coordinate: ";
        cin >> y[count];
        count++;
    }
    while(count <= 50 && x[count -1] && y[count -1]);
    return count;
}

void polar(double x, double y, double& r, double& theta)
{
   const double toDegrees = 180.0/3.141593;
   r = sqrt((pow(x,2))+(pow(y,2)));
   theta = atan(y/x) * toDegrees;
   return;
}

void showPolarCoord(double radius, double angle)
{
   cout << "The polar coordinates are: " << endl;
   cout << "  Distance from origin: " << radius << endl;
   cout << "  Angle (in degrees) from x-axis: " << angle << endl;

   return;
}
 

I'm having trouble figuring out whether I need an 'if statement' to check if the user has ended the input by entering the coordinates (0,0)
The compiler error at line 17:
 
    polar(x, y, distance, angle);

is because x and y are arrays, but the function expects a single value.
For example,
 
    polar(x[0], y[0], distance, angle);

would call the function with the first element of x and y - though you would probably want to do something using a loop to access each array element in turn.

At line 16, the function call should be
 
    int count = getrec(x, y);

Here the value returned by the function is very important, it tells how many values were entered by the user.

Also, the prototype declaration at line 5 doesn't agree with the function definition at line 20. Change line 5 to match the actual function
 
int getrec(double x[], double y[]);


I'm having trouble figuring out whether I need an 'if statement' to check if the user has ended the input by entering the coordinates (0,0)

No, you already have a do-while loop which does that checking at line 30.

I notice you have discarded the recommended use of SIZE and used the magic number 50 instead. If you move the constant declaration
const int SIZE = 50; at line 11 outside of main, place it somewhere before the start of main() say at line 4 or line 8, you will then be able to use SIZE properly. The advantage of this is that if you need to change the number 50, it need only be done in one place. So-called magic numbers such as 50 are a bad idea, they can cause problems in both understanding and maintaining code, better to use the named constant.




*UPDATED CODE*
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
#include <iostream>
#include <math.h>
using namespace std;

void getrec(double x[], double y[]);                             //To get rectangular coordinates
void polar(double x, double y, double& r, double& theta);      //To calculate polar coordinates
void showPolarCoord(double radius, double angle);              //To display polar coordinates
const int SIZE = 50;

int main()
{
   double x[SIZE];
   double y[SIZE];
   double distance;
   double angle;
   int count = getrec(x,y);
   polar(x, y, distance, angle);
}
   
int getrec(double x[], double y[])
{   int count = 0;

    do
    {   cout << "Enter the x coordinate: ";
        cin >> x[count];
        cout << "Enter the y coordinate: ";
        cin >> y[count];
        count++;
    }
    while(count <= 50 && x[count -1] && y[count -1]);
    return count;
}

void polar(double x, doube y, double& r, double& theta)
{
   const double toDegrees = 180.0/3.141593;
   r = sqrt((pow(x,2))+(pow(y,2)));
   theta = atan(y/x) * toDegrees;
   return;
}

void showPolarCoord(double radius, double angle)
{
   cout << "The polar coordinates are: " << endl;
   cout << "  Distance from origin: " << radius << endl;
   cout << "  Angle (in degrees) from x-axis: " << angle << endl;

   return;
}
 



Thank you for taking the time to explain everything, I really appreciate it.

At line 30
Since count is set to 50, how does that check whether the user entered (0,0)?
---
For the shifting part, I tried:
1
2
x[i] = x[i] + 4;
y[j] = y[j] + 7;


To shift all coordinates to the right 4, and up 7. But from what I understand, I think shifting on the original parallel arrays would lose the original coordinates given initially by user, so it will be overwritten by the shifted values. And we need the original values, so I'm stuck there aswell.

I thought of making a copy of the original arrays and adding the shifts there, any thoughts?

Thank you!
Last edited on
Ok a few thought. First the updated code doesn't compile.
Line 5
 
void getrec(double x[], double y[]); 

should agree with line 20
 
int getrec(double x[], double y[])


Line 17 gives an error
 
polar(x, y, distance, angle);

because x and y are arrays but the function polar is expecting a single value of type double.

For example
 
polar(x[0], y[0], distance, angle);

would call the function with the first element of the arrays x and y.

... I'm getting a rather unnerving sense of deja vu here ...

Line 34
 
void polar(double x, doube y, double& r, double& theta)

the type double has mysteriously changed into doube.

Ok, having gone through that I've an uneasy feeling. If the code doesn't compile, then you cannot run it. If it isn't actually running, then trying to either check that it works, or make any enhancements won't be easy.

Now for some of the other questions,
At line 30
Since count is set to 50, how does that check whether the user entered (0,0)?

A minor point, which I'll mention and then skip over - SIZE if a better alternative than 50 here.

Now, how does it check check whether the user entered (0,0)?
This needs breaking down into smaller steps.
Line 30 looks like this:
 
    while(count <= 50 && x[count -1] && y[count -1]);


The first thing to notice is the use of && to combine expressions. There are three individual conditions:
 
count <= 50

 
x[count -1]

 
y[count -1]


Since the && (logical AND) operator is used, that means the loop will continue to execute only when all three of these are true.

By now you are probably puzzled. count <= 50 looks like a condition. But what about the other two? Well in both C and C++ any value which evaluates as zero is considered as meaning logical false. Conversely, any value or expression which is not zero means logical true.

Now we've taken this apart, we can see a few problems.
This is the problem requirement:
The user may enter up to 50 pairs, and the user will say he/she is done by inputting the coordinate pair (0, 0).

Not it says up to 50. But testing count <= 50 allows the loop to execute one more time when count has reached 50 (count is equal to 50) thus the user may enter 51 values. That is dangerous. Where will that 51st pair of values be stored? Not inside the arrays. They are already completely full. Some adjacent area of memory will be corrupted. The condition should be count < 50, with the 'less than' operator, not 'less than or equal to'.

The other conditions are not quite right. (0,0) is supposed to mark the end of input. Currently if something like (0,7) or (5, 0) is entered, it will terminate the loop.

This reply is getting quite long, so I'll end there. I know there are other questions, but I can only recommend getting the program up and running, start testing it with actual input before considering any next move to make.
Last edited on
*UPDATED CODE*

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
#include <iostream>
#include <math.h>
using namespace std;

int getrec(double x[], double y[]);                             
void polar(double x, double y, double& r, double& theta);      
void showPolarCoord(double radius, double angle);              


const int SIZE = 50;
const double toDegrees = 180.0/3.1415926;
int main()
{
   double x[SIZE];                                            
   double y[SIZE];                                            
   double distance[SIZE];                                          
   double angle[SIZE];
   double x_same[SIZE];
   double y_same[SIZE];                                             
   int count = getrec(x,y);                                   
   polar(x[SIZE], y[SIZE], distance, angle);
   x_same[SIZE] = x_same[SIZE] + 4;
   y_same[SIZE] = y_same[SIZE] + 7;
   for(int i=0; i < count; i++)
   {
      polar (x[SIZE], y[SIZE], distance, angle);
   }
}
   int getrec(double x[], double y[])                           
{   int count = 0; 
    double temp = x[SIZE];
    double temp1 = y[SIZE];                                          

    do
    {   cout << "Enter the x coordinate: ";                   
        cin >> x[count];
        cout << "Enter the y coordinate: ";                  
        cin >> y[count];
        count++;
    }
    while(count < SIZE && (temp =! 0) && (temp1 =! 0));
    return count;
}


void polar(double x, double y, double& r, double& theta)      
{                  
   r = sqrt((pow(x,2))+(pow(y,2)));                           
   theta = atan(y/x) * toDegrees;                             
   return;
}

void showPolarCoord(double radius, double angle)           
{
   cout << "The polar coordinates are: " << endl;
   cout << "  Distance from origin: " << radius << endl;
   cout << "  Angle (in degrees) from x-axis: " << angle << endl;

   return;
}
 



This is what I have now. I tried compiling it and it said:
In function 'int main()':
21:43: error: invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double*'
6:6: note: in passing argument 3 of 'void polar(double, double, double&, double&)'
26:47: error: invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double*'
6:6: note: in passing argument 3 of 'void polar(double, double, double&, double&)'



I've been trying for so long and I asked my TA, and he couldn't figure out what the problem is... I have no idea what to do
Last edited on
I asked my TA, and he couldn't figure out what the problem is

That's unfortunate.

Still, let's press on.

I've just gone right back to the top of this thread to make sure I understand the requirements, that is, what you are supposed to do.

I think you definitely need to store the x and y values in arrays - it says so,
- Store the pairs in a pair of parallel arrays as you read them in.


As for the polar coordinates, it doesn't exactly say that you need arrays (it doesn't say that you don't). All it says for sure is that you need to print them out.

Anyway, the compiler error.
21:43: error: invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double*'

That's because you are trying to pass an array called distance and an array called angle to a function which expects just two ordinary variables of type double.
But another issue is why you would want to call the function polar() at that point in the program, it seems premature to me.

So let's briefly go back to the question.
- Store the pairs in a pair of parallel arrays as you read them in.
- shifts all coordinates to the right by 4 units and up by 7 units.
- Converts all shifted coordinate pairs to polar coordinates by calling the method you wrote.
-Prints lines showing both the input coordinates and output coordinates as ordered pairs in the style of this example: (4, 1) ---> (11.3137, 45)


You've done the first part - get the input and store it. So the next part is to shift all coordinates to the right by 4 units and up by 7 units. Now you will need a loop for that.
Something like this:
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 x[SIZE];
    double y[SIZE];
    double distance[SIZE];
    double angle[SIZE];
    double x_same[SIZE];
    double y_same[SIZE];   

    int count = getrec(x,y);  

    for (int i=0; i < count; i++)
    {
        x_same[i] = x[i] + 4;
        y_same[i] = y[i] + 7;
    } 

    for (int i=0; i < count; i++)
    {
        polar(x_same[i], y_same[i], distance[i], angle[i]);
    } 
    
}



Personally I'm not sure you need all those other arrays, but that isn't too important right now. What I want to do is to get you to a stage where the code will compile.

What remains to be done is this part:
-Prints lines showing both the input coordinates and output coordinates as ordered pairs in the style of this example: (4, 1) ---> (11.3137, 45)

... and of course lots of testing and debugging if it doesn't work properly.
Last edited on
*UPDATED CODE*

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
#include <iostream>
#include <math.h>
using namespace std;

int getrec(double x[], double y[]);                             
void polar(double x, double y, double& r, double& theta);      
void showPolarCoord(double radius, double angle);              


const int SIZE = 50;
const double toDegrees = 180.0/3.1415926;
int main()
{
   double x[SIZE];                                            
   double y[SIZE];                                            
   double distance[SIZE];                                          
   double angle[SIZE];
   double x_same[SIZE];
   double y_same[SIZE];                                             
   
   int count = getrec(x,y);                                   
   
   for (int i=0; i < count; i++)
   {
      x_same[i] = x[i] + 4;
      y_same[i] = y[i] + 7;
   }
   for(int i=0; i < count; i++)
   {
      polar (x_same[i], y_same[i], distance[i], angle[i]);
   }
}
   int getrec(double x[], double y[])                           
{   int count = 0; 
    double temp = x[SIZE];
    double temp1 = y[SIZE];                                          

    do
    {   cout << "Enter the x coordinate: ";                   
        cin >> x[count];
        cout << "Enter the y coordinate: ";                  
        cin >> y[count];
        count++;
    }
    while(count < SIZE && (temp =! 0) && (temp1 =! 0));
    return count;
}


void polar(double x, double y, double& r, double& theta)      
{                  
   r = sqrt((pow(x,2))+(pow(y,2)));    
   theta = atan(y/x) * toDegrees;                             
   return;
}

void showPolarCoord(double radius, double angle)           
{
   cout << "The polar coordinates are: " << endl;
   cout << "  Distance from origin: " << radius << endl;
   cout << "  Angle (in degrees) from x-axis: " << angle << endl;

   return;
}


I tried compiling this program and it worked, however, when I entered (0,0), it kept asking for more inputs rather than terminating, howcome?

however, when I entered (0,0), it kept asking for more inputs rather than terminating,

Yeah, that happened to me too when I tried it.

The code in that part of the program (lines 35, 36 and 45) is a bit messed up. I mean really messed up.

Let's go back to basics of arrays.
Say you have an array with four elements.
int array[4];
The elements of that array are as follows:
array[0], array[1], array[2] and array[3].

Ok so far?
Now which element is array[4]? The answer is. None of them. It is outside the array. If you try to access an out of bounds element, you will get incorrect results or possibly cause the entire program to crash.

The same applies here:
35
36
    double temp = x[SIZE];
    double temp1 = y[SIZE]; 

temp and temp1 are assigned the value of some unknown element outside the array.

Now let's move on to look at line 45.
while(count < SIZE && (temp =! 0) && (temp1 =! 0));

The first part, while(count < SIZE is correct. That's a good start. Now what else is needed? If the last two values of both x and y are zero, the loop should end.

Now the last two values of x and y are x[count -1] and y[count -1].

If both of those are zero the loop should end.
Conversely, if either of them is not zero, the loop should continue.

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
#include <iostream>
#include <math.h>
using namespace std;

int getrec(double x[], double y[]);                             
void polar(double x, double y, double& r, double& theta);      
void showPolarCoord(double radius, double angle);              


const int SIZE = 100;
const double toDegrees = 180.0/3.1415926;
int main()
{
   double x[SIZE];                                            
   double y[SIZE];                                            
   double distance[SIZE];                                          
   double angle[SIZE];
   double x_same[SIZE];
   double y_same[SIZE];                                             
   
   int count = getrec(x,y);                                   
   
   for (int i=0; i < count; i++)
   {
      x_same[i] = x[i] + 6;
      y_same[i] = y[i] + 2;
   }
   for(int i=0; i < count; i++)
   {
      polar (x_same[i], y_same[i], distance[i], angle[i]);
   }
}
   int getrec(double x[], double y[])                           
{   int count = 0; 
                                             
    do
    {   cout << "Enter the x coordinate: ";                   
        cin >> x[count];
        cout << "Enter the y coordinate: ";                  
        cin >> y[count];
        count++;
    }
    while(count < SIZE && (x[count -1] != 0) && (y[count -1] =! 0));
    return count;
}


void polar(double x, double y, double& r, double& theta)      
{                  
   r = sqrt((pow(x,2))+(pow(y,2)));    
   theta = atan(y/x) * toDegrees;                             
   return;
}

void showPolarCoord(double radius, double angle)           
{
   cout << "The polar coordinates are: " << endl;
   cout << "  Distance from origin: " << radius << endl;
   cout << "  Angle (in degrees) from x-axis: " << angle << endl;

   return;
} 


Alright thank you so much, that worked.

I'm thinking of removing this part:

1
2
3
4
5
6
7
8
void showPolarCoord(double radius, double angle)           
{
   cout << "The polar coordinates are: " << endl;
   cout << "  Distance from origin: " << radius << endl;
   cout << "  Angle (in degrees) from x-axis: " << angle << endl;

   return;
}


Because we don't just need the polar coordinates, we need the rectangular to polar(Both coordinates).

Do I just put cout/cin statements to get each coordinate?
Pages: 12