Logic time erros: Help fixing

Hello!! My code is to find the length, midpoint, and slope of a line segment using 2 points. It compiles and everything, but i just can't seem to find the logical errors. I have run it over and just can't seem to find it. I may just be overlooking, but could i get a second opinion on it? Thanks!


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
 /**
   Compute the distance between two points (x1,y1) and (x2,y2) 
   using the standard formula:
            ________________________
           /         2            2
      /\  / (x2 - x1)  + (y2 - y1)
        \/

   @param x1 the x-coordinate of the first point
   @param y1 the y-coordinate of the first point
   @param x2 the x-coordinate of the second point
   @param y2 the y-coordinate of the second point
   @return the distance between the two points
 */
double distance(double x1, double y1, double x2, double y2)
{
   return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

/**
   Determine the midpoint between two points (x1,y1) and (x2,y2).
   The midpoint has coordinates:

       (x2 + x1)           (y2 + y1)
      -----------   and   -----------
           2                   2
   
   @param x1 the x-coordinate of the first point
   @param y1 the y-coordinate of the first point
   @param x2 the x-coordinate of the second point
   @param y2 the y-coordinate of the second point
   @param xmid the x-coordinate of the midpoint (reference)
   @param ymid the y-coordinate of the midpoint (reference)
 */
void compute_midpoint(double x1, double y1, double x2, double y2,
                      double& xmid, double ymid)
{
   xmid = (x2 + x1) / 2;
   ymid = (y2 + y1) / 2;
}

/*
   Calculate the slope (Δy/Δx) of a line between two points
   (x1,y1) and (x2,y2).

   @param x1 the x-coordinate of the first point
   @param y1 the y-coordinate of the first point
   @param x2 the x-coordinate of the second point
   @param y2 the y-coordinate of the second point
   @param vertical true iff slope is vertical (reference)
   @param result the computed slope (reference)
 */
void compute_slope(double x1, double y1, double x2, double y2,
                   bool vertical, double result)
{
   const double EPSILON = 1E-14;    // "close enough" for floating point
   vertical = (abs(x1 - x2) < EPSILON);
   if (not vertical);
   {
      result = (y2 - y1) / (x2 - x1);
   }
}

int main()
{
   double p_x, p_y;     // coordinates of the segment's first endpoint
   cout << "Enter the x & y coordinates for the first point: ";
   cin >> p_x >> p_y;

   double q_x, q_y;     // coordinates of the segment's second endpoint
   cout << "Enter the x & y coordinates for the second point: ";
   cin >> q_x >> q_y;

   cout << "For the line segment from (" << p_x << "," << p_y << ")"
        << " to (" << q_x << "," << q_y << "):" << endl;
   cout << "\tIts length is " << distance(p_x, q_y, p_x, q_y) << endl;

   double m_x, m_y;     // coordinates of the segment's midpoint
   compute_midpoint(p_x, p_y, q_x, q_y, m_x, m_y);
   cout << "\tIts midpoint is at (" << m_x << "," << m_y << ")" << endl;

   double slope;        // the line segment's slope (unless it's vertical)
   bool is_vertical;    // whether or not the segment is vertical
   compute_slope(p_x, p_y, q_x, q_y, is_vertical, slope);
   cout << "\tIts slope is ";
   if (is_vertical) { cout << "vertical"; }
   else { cout << slope; }
   cout << endl;

   return 0;
}
Last edited on
Edit your post and repaste your code between [code] [/code] tags to preserve spacing.

Compile your code with full warnings enabled and pay attention to the warnings. There are a lot of them!
And you are passing incorrect parameters to distance().

$ g++ -std=c++14 -Wall -W -pedantic slope.cpp
slope.cpp: In function ‘void compute_midpoint(double, double, double, double, double&, double)’:
slope.cpp:44:44: warning: parameter ‘ymid’ set but not used [-Wunused-but-set-parameter]
                       double& xmid, double ymid)
                                            ^
slope.cpp: In function ‘void compute_slope(double, double, double, double, bool, double)’:
slope.cpp:66:22: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]
     if (not vertical);
                      ^
slope.cpp:62:42: warning: parameter ‘result’ set but not used [-Wunused-but-set-parameter]
                    bool vertical, double result)
                                          ^
slope.cpp: In function ‘int main()’:
slope.cpp:87:51: warning: ‘m_y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     compute_midpoint(p_x, p_y, q_x, q_y, m_x, m_y);
                                                   ^
slope.cpp:92:18: warning: ‘is_vertical’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     compute_slope(p_x, p_y, q_x, q_y, is_vertical, slope);
                  ^
slope.cpp:92:58: warning: ‘slope’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     compute_slope(p_x, p_y, q_x, q_y, is_vertical, slope);
Topic archived. No new replies allowed.