C++ mag( ) function and prototype

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:

#include<iostream>
#include<cmath>

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.
Alyssa ZukasAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
So, what is it you are specifically having trouble with?
0
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
@Alyssa Zukas
Based on your comment, we feel that you need to learn the following:
1. How to write prototype of function.
2. How to handle when user input being invalid
3. define the function where ever required and check static function or available across multiple files,
All above needs to be done before implementation during analysis itself.
Here goes related code doing the same.

#include <limits.h>
#include <stdio.h>
#include <sys/errno.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespace std;
// Function returning magnitude of first parameter(x) and second parameter (y) points.
double mag( const double &, const double & );
// First  parameter referring constant reference for x point
// Second parameter referring constant reference for y point
int main(void)
{
	double x = 0.0;
	double y = 0.0;
	double v=3.0, w=4.0;
	double m = 0.0;
	cout << "Enter X coordinate: ";
	cin >> x;
	while ( cin.fail() )
	{
		cin.clear();
		cin.ignore( INT_MAX,'\n');
		fflush(stdin);
		cout << "Enter only numbers.";
		cout << "Enter X coordinate: ";
		cin >> x;
	}
	cout << "Enter Y coordinate: ";
	cin >> y;
	while ( cin.fail() )
	{
		cin.clear();
		cin.ignore( INT_MAX,'\n');
		fflush(stdin);
		cout << "Enter only numbers.";
		cout << "Enter 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;
}
// Function returning magnitude of first parameter(x) and second parameter (y) points.
double mag( const double &x, const double &y)
// First  parameter referring constant reference for x point
// Second parameter referring constant reference for y point
{
	return sqrt( pow( x, 2) + pow( y, 2));
}

Open in new window

Example output:
$ /usr/bin/g++ -g -Wall magnitude.c -o a.out
$ ./a.out
Enter X coordinate: Murugesan Dinesh
Enter only numbers.Enter X coordinate: 3
Enter Y coordinate: Alyssa Zukas
Enter only numbers.Enter Y coordinate: 4
The magnitude of ( 3 , 4 ) is: 5
The magnitude of ( 3 , 4 ) is: 5
$

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
Best solution provided.
Closing this, since no comment added from Alyssa Zukas.
If modification required, post your comment to Administrator.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.