Where is my program failing?

I'm making a program that gives a solution to a problem in graph theory
and it has worked except for one of the lower functions.
Here is the program below:

The purpose of the program is, given a series of points on a graph that the user
inputs, the program wants to generate a random path betweem them and calculate
the distance of the path. The path is generated by choosing three points at
random and then adding points to the series by selecting the point that is closest to the last chosen point.

I know this is alot of code and I don't expect anyone to read through it all
but if anyone does I will be very thankful. :)

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int amm;
double * x;
double * y;
int * point;

//This is just the distance formula.
double distance (double _x1, double _x2, double _y1, double _y2)
{
    double xx,yy;;
    xx = pow( _x2 - _x1 , 2);
    yy = pow( _y2 - _y1 , 2);
    return (sqrt (xx+yy));
}

void generate_path();
long factorial();
double find_distance(int, int);
int closest(int);
int not_equal_to(int,int);

int main ()
{
    cout << "Enter the number of points?\n";
    //amm is ammount of points there will be
    cin >> amm;
    if (amm < 3)
    {
    return 0;            
    }
    //the x and y arrays store all of the coordinates
    x = new (nothrow) double [amm];
    y = new (nothrow) double [amm];
    if (x == 0 || y == 0)
    {
          cout << "Error has occured while initiazling x and y variables";
          return 0;
    }
    //Here they enter all of the coordinates
    for (int k=0;k<amm;k++)
    {
        cout << "\n>>Ordered pair for coordinate number "<<(k+1)<<":  (0 to Quit.)\nX:";
        cin >> x[k];
        if (x[k] == 0) 
        {
                 break;
                 return 0;
        }
        cout << "\nY:";
        cin >> y[k];
    }
    generate_path();
    return 0;
}

long factorial (long a)
{
  if (a > 1)
   return (a * factorial (a-1));
  else
   return (1);
}

void generate_path()
{
     int paths;
     //the paths variable stores the total number of paths possible
     paths = factorial(amm);
     point=new int[paths];
     srand (time(NULL));
     //Determine three random points to start from.
     point[0]= rand() % amm;
     point[1]= rand() % amm; 
     point[2]= rand() % amm; 
     cout << "\nChoosing initial points at random...\n";
     //this next part makes sure that neither point[1] or point[2] is
     //the same as point[0]
     while
     (point[1]=point[0])
     {
               point[1]=+ rand() % amm;
               if (point[0]!=point[1]) break;
     }           
     while
     ((point[2]=point[0])||(point[2]=point[1]))
     {
               point[2]=+ rand() % amm;
               if ((point[2]!=point[1])&&(point[2]!=point[0])) break;
     }   
     
     //Initial points chosen.
     cout << "Starting points: (" << point[0] << ") (" <<
     point[1] << ") (" << point[2] << ")\n";
     cout << "\nInitial Points Chosen.\n\n";
     
     //Now choose all of the other points. This is the part that is messing up.
     //A for loop starts with point 3 and continues on, each time finding the
     //closest point to the last point chosen (point [h-1]) and adds it to 
     //point[h]. But choosing the point always returns 0. Why??
     for (int h=3;h<amm;h++)
     {
         int jj;
         jj=closest(h-1);
         point[h]=jj;
         cout << "Point no. " << h+1 << " selected. Value: " << point[h] << ".\n";
     } 
     cin >> amm;
     }

double find_distance (int _A,int _B)
{
       //This just finds the distance between point[__A] and point [__B].
       double _D;
       _D=distance (x[_A],x[_B],y[_A],y[_B]);
       return _D;
}

int not_e(int _h, int value)
{
    //This is the function that tests whether or not value
    //has yet been repeated before in the series of points
    //from point[0] to point [__h].
    int result;
    result=1;
    cout << _h << "!!!";
    for (int u=0;u<_h;u++)
    {
    cout << value;
    cout << "/" << point[u] << " ";
    if (value==point[u])
    {
    result=0;
    }
    }
    return result;
}

int closest(int __a)
{
     //The closest function was written by Helios for me, but I slightly 
     //modified it. It is designed to find the closest point to point[__a],
     //that is not already one of points[0....__a] because I can't repeat a
     //point in the series. But when looking for a point it always returns 0?
     int find_closest_to=__a;
     int closest=-1;
     for (int i=0;i<amm;i++){
     	if ( not_e(__a,i) == 0 )
      		continue;
    	if (closest<0 || find_distance(closest,point[find_closest_to])>find_distance(i,point[find_closest_to]))
     		{closest=i; cout<<"AAA";}
       }
       if (closest<0)
           {
       	   cout << "Error occured in locating points.";
           }
     return closest;
}
Well, it'd make us a bit more willing if you at least told us exactly where the program crashes.
Its not a crash, its not functioning correctly, and if you read the comments the problem is explained.

Now, I could be entirely wrong and totally confused, but aren't you accessing a point in an array with -1 the first time you run that loop? That would take you out of the arrays scope and cause undefined behavior...
Last edited on
huumm , let me rum on my compiler .....
I am not getting ur point . are you excessing some junck values ????
Seriously, read the comments >.<
no, my problem is in the "closest" function. i am 95% sure everything else works, but for some reason the closest function does not work properly.
i have written it out in english and rewrote it different ways, and it seems to make sense in my head, but it doesnt work properly in my compiler?
i need it to determine a point that is closest to the last point chosen and that has not already been added to the series
First comment. Your naming convention is completely fked. Naming variables by single letters and changing between varname, _varname and __varname is terrible. Not to mention your for loop is using u. Standard naming convention your loop indexes start from i.

2nd. I think you need to re-design the whole distance check part of your code. Read through the following code, you can then just build a function to pick 3 indexes from pointList and then compare the differences between them. Instead of copy+pasting other peoples code, try to name it inline with your own conventions and understand what the code is doing.
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
#include <iostream>
#include <math.h>

using namespace std;

// Define our point structure
struct point {
  int x;
  int y;

  void setLoc(int X, int Y) {
    x = X;
    y = Y;
  }
};

// Variables
point *pointList;

// getDistanceBetween
double getDistanceBetween(int index1, int index2) {
  double xDiff, yDiff;
  xDiff = pow( pointList[index2].x - pointList[index1].x , 2);
  yDiff = pow( pointList[index2].y - pointList[index1].y , 2);
  return (sqrt (xDiff + yDiff));
}

int main() {

  // Build Sample Points.
  pointList = new point[3];
  pointList[0].setLoc(11, 2);
  pointList[1].setLoc(1, 1);
  pointList[2].setLoc(2, 2);

  double smallestDistance = -1.0;
  int    smallestStart    = 0;
  int    smallestEnd      = 0;

  for (int i = 0; i < 3; ++i) {
    for (int j = i+1; j < 3; ++j) {
      double distanceBetween = fabs(getDistanceBetween(i, j));
      cout << "Distance between " << i << " and " << j << " was " << distanceBetween << endl;

      if ((distanceBetween < smallestDistance) || (smallestDistance < 0.0)) {
        smallestDistance = distanceBetween;
        smallestStart = i;
        smallestEnd = j;
      }
    }
  }

  cout << "Smallest distance between points 0-2 was: " << smallestDistance << endl;
  cout << "This was from point " << smallestStart << " to point " << smallestEnd << endl;

  delete [] pointList;

  return 0;
}



oh my god it worked.
im so freaking out now.
thank you so much to everyone who helped!!!!!!
:D
Topic archived. No new replies allowed.