C++

Hi I need to do the following in C++ but I have no idea what I am doing.

Here is my problem question.

Write a program that takes calculate the magnitude and angle of a 2 dimensional vector. The components of the vector should be inputted on the command line. To practice subroutines and pointers, the calculation should be done in a single subroutines and the two results need to passed backed to the main using pointers


CAN SOMEONE HELP ME PLEASE...
Last edited on
Do you mean sth. like 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
#include <iostream>
#include <stdlib.h>

using namespace std;

struct Vector
{
  int magnitude;
  float angle;
};


void GetMagnitude(int *vec);
void GetAngle(float *angle);

int main()
{
  Vector vec;

  GetMagnitude(&vec.magnitude);
  GetAngle(&vec.angle);

  cout << "\n\nThe vector you entered\n\n" << "Magnitude: " << vec.magnitude << "\tAngle: " << vec.angle << "\n\n";

  system("pause");
  return 0;
}

void GetMagnitude(int *magnitude)
{
  cout << "Magnitude: ";
  cin >> *magnitude;
}

void GetAngle(float *angle)
{
  cout << "Angle: ";
  cin >> *angle;
}
I cannot remember the calculations off hand, but how far have you gotten? Do you at least know how to get command line arguments? That seems like the hardest part of this exercise to me.
1
2
void GetMagnitude(int *vec);
void GetAngle(float *angle);

That won't work because the assignment specifically says
the calculation should be done in a single subroutines

So I think it needs to be something like
1
2
3
// Given an input vector vec, compute the magnitude and angle and put them in
// *magnitude and *angle.
void calculateMagAndAngle(const Vector &vec, double *magnitude, double *angle);


I hope that this is a very beginner exercise because it's teaching some bad habits:
- since the vector is required, it should be a method of class Vector, not a stand-alone function
- magnitude and angle should be passed back using references instead of pointers. In general, only pass a pointer if the pointer can be null.
- What if you only want one value (magnitude or angle)? Maybe this is a reason to pass them by pointer and allow null to mean (don't set this one), but it would be easier to just have two separate methods.
Thank you so much for your help. Dhayden, yes the calc is in a single subroutines. So, I should replace
void GetMagnitude(int *vec);
void GetAngle(float *angle);

to one line :
void calculateMagAndAngle(const Vector &vec, double *magnitude, double *angle);


Hopefully, It will work.

Thank you so much
Topic archived. No new replies allowed.