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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
  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 <iostream> 
#include <iomanip> 
#include <cmath> 
 
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.