We help IT Professionals succeed at work.

C++ mag( ) function and prototype

Alyssa Zukas
Alyssa Zukas asked
Last Modified: 2017-04-14
Assignment I am having trouble with:

Complete 'this program', by adding a function called mag( ) to calculate the magnitude of a vector.  You'll need to add a prototype before main( ), and the implementation (header & body) of the function after main( );

reminder: mag = sqrt (x^2 + y^2)

Calling it with something like:

            vec_length = mag(x, y);

program to be completed:


using namespace std;

//      for part a, put your function prototype for mag here!


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;
    return 0;

//.................put your mag function here...................
// be sure to add comments describing the inputs and outputs so a reader would
// know how to use it.
Watch Question

evilrixSenior Software Engineer (Avast)

So, what is it you are specifically having trouble with?
Application Development and Automation Management
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
MURUGESAN NApplication Development and Automation Management

Best solution provided.
Closing this, since no comment added from Alyssa Zukas.
If modification required, post your comment to Administrator.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions