2-dimensional vector and a 3-dimensional vector program

i am trying to write a program that could output the magnitudes of a 2-dimensional vector and a 3-dimensional vector, however im stuck at this point

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using std::cout;
using std::endl;
#include <cmath>
double magnitude(double x, double y);
double magnitude(double x, double y, double z);
int main() {
double mag2D, mag3D;
mag2D = magnitude(3,4);
mag3D = magnitude(3,4,5);
cout<<"magnitude of 2D vector = " << mag2D << endl;
cout<<"magnitude of 3D vector = " << mag3D << endl;
return 0;
}


can anyone help me?
Last edited on
You mean return sqrt(x*x+y*y); ?
yes pretty much but how do I include that into the program?
also I dont understand y the current on always gives me errors
thx!
Just inside {}
1
2
3
double magnitude(double x, double y){
   return sqrt(x*x+y*y);
}


main() is a function, and you know how to write main(), so that means how to write any function, right?.

What errors are you getting (except "undefined reference to magnitude" or something like that)?
hi yeh i fixed it thx!! (silly rookie error, missed a semi colon )
however just one last question what does the sqrt function does?
could i possibly write a function without using it?
sqrt(x) is short for Square Root of x. You can write a function any way you like, it's just this specific function that you will want to use the sqrt() function for.
Last edited on
Topic archived. No new replies allowed.