You are using a version without Ads of this website. Please, consider donating:

### Cannot figure out how to use one function's output in another.

In my code the cout phrase is supposed to give me an angle in degrees. no matter which 2 points i enter in, it always outputs the angle between them to be 57. This is because the acos value of 0 in degrees is 57. The program compiles without error.

here is the code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136`` ``````/* File: vector_angle.cpp Created by: Me Creation Date: 03/17/2013 Last Modification Date: 03/17/2013 THIS PROGRAM IS TAKEN FROM CSE202 SPRING 09 BY PROF WENGER Synopsis: This program reads in the coordinates of two 2D vectors and outputs the angle between the vectors in degrees. */ #include #include #include using namespace std; // function prototypes // ENTER FUNCTION PROTOTYPE FOR normalize() HERE. void normalize(double & x, double & y); // ENTER FUNCTION PROTOTYPE FOR dot_product() HERE. int dot_product(double x1, double y1, double x2, double y2); // ENTER FUNCTION PROTOTYPE FOR compute_vector_angle() HERE. int compute_vector_angle(double x1, double y1, double x2, double y2); // ENTER FUNCTION PROTOTYPE FOR radians2degrees() HERE. int radians2degrees(double radians); // ENTER FUNCTION PROTOTYPE FOR output_angle() HERE. void output_angle(double x1, double y1, double x2, double y2, double angle); // *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION. // *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION. // *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION. int main() { double u1, v1; // coordinates of vector 1 double u2, v2; // coordinates of vector 2 double radians; // angle in radians double degrees; // angle in degrees // Read points cout << "Enter first vector (2 floats): "; cin >> u1 >> v1; cout << "Enter second vector (2 floats): "; cin >> u2 >> v2; // compute angle in radians between (u1, v1) and (u2, v2) radians = compute_vector_angle(u1, v1, u2, v2); // convert radians to degrees degrees = radians2degrees(radians); // output angle output_angle(u1, v1, u2, v2, degrees); return(0); } // DEFINE FUNCTION normalize() HERE. void normalize(double & x, double & y) { if(((x*x)+(y*y)) != 0) { x = x/pow(((x*x)+(y*y)),(1/2)); y = y/pow(((x*x)+(y*y)),(1/2)); } else if(((x*x)+(y*y)) == 0) { x = 0; y = 0; } } // DEFINE FUNCTION dot_product() HERE. int dot_product(double x1, double y1, double x2, double y2) { double dot(0); // variable for output of dot product dot = (x1*x2)+(y1*y2); return(dot); } // DEFINE FUNCTION compute_vector_angle() HERE. int compute_vector_angle(double x1, double y1, double x2, double y2) { double radians(0); // variable for output of angle in radians double product(0); // variable for output of dot_product normalize(x1,y1); normalize(x2,y2); product = dot_product(x1,y1,x2,y2); radians = acos(product); return(radians); } // DEFINE FUNCTION radians2degrees() HERE. int radians2degrees(double radians) { double degrees(0); // variable for output angle in degrees degrees = 180*(radians/M_PI); return(degrees); } // DEFINE FUNCTION output_angle() HERE. void output_angle(double x1, double y1, double x2, double y2, double degrees) { cout << "Angle between vectors (" << x1 << "," << y1 << ") and (" << x2 << "," << y2 << ") = " << degrees << " degrees."; }``````

I have done some trouble shooting to figure out where the error is in the program and made a bunch of cout statements to follow its reasoning. In the following part of the program (when inputs are (1,0) and (1.5,1.5) the angle should be 45 degrees), cout says the value of "product" is 0 where it is supposed to be .707107. So the problem lies within the 'product = dot_product(x1,y1,x2,y2);' line. The dot_product function is giving "product" a value of 0.

 ``123456789101112131415`` ``````int compute_vector_angle(double x1, double y1, double x2, double y2) { double radians(0); // variable for output of angle in radians double product(0); // variable for output of dot_product normalize(x1,y1); normalize(x2,y2); product = dot_product(x1,y1,x2,y2); radians = acos(product); return(radians); }``````

Last edited on
OK, some problems here.

First use doubles not ints - nothing in this program will work properly with ints.

Lines 78 & 79: 1/2 is integer division and equals 0. Use 0.5 instead. I always put double values with a decimal point and numbers after the decimal point - as in 3.0 not 3 or 3. , or 0.5 not .5 .

Values like 180.0/M_PI are constants, if they are not already defined in the maths header, you should define them at the beginning of the program:

const double 180_PI = /*whatever it is */ ;

Hope all goes well.

Last edited on
All your non void prototype functions return an int type. Is this what you intended?
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: