2-dimensional vector and a 3-dimensional vector program

May 25, 2011 at 4:00pm
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 May 25, 2011 at 4:00pm
May 25, 2011 at 4:37pm
You mean return sqrt(x*x+y*y); ?
May 25, 2011 at 6:30pm
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!
May 25, 2011 at 7:20pm
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)?
May 26, 2011 at 3:37pm
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?
May 26, 2011 at 3:41pm
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 May 26, 2011 at 3:42pm
Topic archived. No new replies allowed.