# Annoying bug I can't find or fix!

• 11-20-2004
homeyg
Annoying bug I can't find or fix!
I've got a question on a program I've been trying to write (I'm pretty much done except for one annoying bug). The bug (in the program below) occurs right above where the bolded comment is and for the rest of the program, however, doesn't occur before this (I proved this with the numerous 'cout' statements). I don't know why it occurs or how it occurs, but it does. The program I'm writing tells two variations of the equation of the line that passes through the two points entered and the slope of that line. All goes well when initializing the pointArrays, the right values are stored (at least I think); x1 and y1 for pointArray11 and x2 and y2 for pointArray21. I've proved this by adding cout statements after each initialization index. Now this is where I'm stumped: in the cout statement that displays all of the values together, pointArray11[0] changes its value to the value stored in pointArray21[1] and all others still hold their correct values. I have no idea how this happens! pointArray11[0] retains the value stored in pointArray21[1] for the rest of the program. Can anyone see why this is happening? I sure can't. Thanks to anyone who spends their time to solve my pathetic program's problem.

Code:

```#include <iostream> using namespace std; double yIntercept(double rise, double run, double xcoor, double ycoor); int main() {     int cont;     double pointArray11[1];     double pointArray21[1];         cout << "This program finds the slope and two variations of the equation of the line that passes through these two points."<<endl;     while(cont!=0)     {          cout << "Please enter the coordinates of the first point." << endl;         for(int coor = 0; coor<2; coor++)         {             double* coordinateSpace1 = new double;             cout << "Coordinate?";             cin  >> *coordinateSpace1;             pointArray11[coor] = *coordinateSpace1;             delete coordinateSpace1;         }         cout << pointArray11[0]<<pointArray11[1]<<endl;                         cout << "Please enter the coordinates of the second point." << endl;         for(int rooc = 0; rooc<2; rooc++)         {             double* coordinateSpace2 = new double;             cout << "Coordinate?";             cin  >> *coordinateSpace2;             pointArray21[rooc] = *coordinateSpace2;             delete coordinateSpace2;         }         cout << pointArray21[0]<<pointArray21[1]<<endl;                    cout << pointArray11[0]<<pointArray11[1]             << pointArray21[0]<<pointArray21[1]<<endl;         //pointArray11[0] changes to same value as pointArray21[1] during the cout statement immediately above and for the rest of the program                    double slope1 = pointArray21[1] - pointArray11[1];         double slope2 = pointArray21[0] - pointArray11[0];         cout << "The slope of the line is: " << slope1 <<"/" << slope2 << endl;         cout << "The equation for the line is: y-" << pointArray11[1] << "=" << slope1 << "/"<<slope2 << "(x-"<< pointArray11[0] <<")"<<endl;         cout << "The slope-intercept form of this equation is: y=" <<slope1<<"/"<<slope2<<"x+"<<yIntercept(slope1, slope2, pointArray11[0], pointArray11[1])<<endl;         cout << "Continue? Press zero to quit and any number to continue." <<endl;         cin  >> cont;     }              return 0; }   double yIntercept(double rise, double run, double xcoor,  double ycoor)  {     double b = rise/run*(0-xcoor)+ycoor;     return b; }```
• 11-20-2004
The Brain
Code:

```#include <iostream> using namespace std; double yIntercept(double rise, double run, double xcoor, double ycoor); int main() {     int cont;     double pointArray11[2];     double pointArray21[2];         cout << "This program finds the slope and two variations of the equation of the line that passes through these two points."<<endl;     while(cont!=0)     {          cout << "Please enter the coordinates of the first point." << endl;         for(int coor = 0; coor<2; coor++)         {             cout << "Coordinate?";             cin  >> pointArray11[coor];         }         cout << pointArray11[0]<<pointArray11[1]<<endl;                         cout << "Please enter the coordinates of the second point." << endl;         for(int rooc = 0; rooc<2; rooc++)         {             cout << "Coordinate?";             cin  >> pointArray21[rooc];                    }         cout << pointArray21[0]<<pointArray21[1]<<endl;                    cout << pointArray11[0]<<pointArray11[1]             << pointArray21[0]<<pointArray21[1]<<endl;         //pointArray11[0] changes to same value as pointArray21[1] during the cout statement immediately above and for the rest of the program                    double slope1 = (pointArray21[1] - pointArray11[1]);         double slope2 = (pointArray21[0] - pointArray11[0]);         cout << "The slope of the line is: " << slope1 <<"/" << slope2 << endl;         cout << "The equation for the line is: y-" << pointArray11[1] << "=" << slope1 << "/"<<slope2 << "(x-"<< pointArray11[0] <<")"<<endl;         cout << "The slope-intercept form of this equation is: y=" <<slope1<<"/"<<slope2<<"x+"<<yIntercept(slope1, slope2, pointArray11[0], pointArray11[1])<<endl;         cout << "Continue? Press zero to quit and any number to continue." <<endl;         cin  >> cont;     }              return 0; }   double yIntercept(double rise, double run, double xcoor,  double ycoor)  {         if(run)         {                 double b = ((rise/run)*(0-xcoor)+ycoor);                                return b;         }         else         {                 cout << "\n\n\t\aDivision By Zero Error! ";                 return 1;         } }```

There are just two issues with your program... you declared your arrays to have only one element.... so your 'for' loops are actually accessing the array out of bounds on the last iteration (this is known as an, "off-by-one" error) so in your array delcarations, I added an additional element. array[2] has elements [0] and [1]

Also, in your intercept function you have a division with a variable in the denominator.. so you should somehow account for a 'division by zero'.
• 11-20-2004
homeyg
Ah, I see now. The array declarations were the main problem. Thanks alot for pointing that out to me! Never did get a divide by zero, though.
• 11-20-2004
The Brain
you should handle a 'division by zero' exception anytime you have a variable in the denominator.. as the domain for any denominator is all real numbers except for 0.

Use some test data:

(0,1) and (0,2) will not have a "run" (the slope for this vertical line is "undefined")

you should test your algorithms at the boundries.. looking for zeros is always a good test.