#include #include #include #define DEBUG 0 typedef struct neighbor{ double distance; int neighborId; } neighbor; void getArgs(int argc, char** argv); void init(double ***pointsArray, neighbor ***sortedNeighborsArray); int readPointsArrayFromFile(double ***array); void printArray(int rows, int columns, double ***array); void addDistanceAndShift(neighbor ***sortedNeighborsArray, int firstPointId, int secondPointId, double pointsDistance); void cleanUp(double ***pointsArray, neighbor ***sortedNeighborsArray); int numberOfPoints, numberOfDimensions, numberOfNeighbors; int main(int argc, char **argv){ double **pointsArray; neighbor **sortedNeighborsArray; getArgs(argc, argv); init(&pointsArray, &sortedNeighborsArray); //printArray(numberOfPoints, numberOfDimensions, &pointsArray); //printf("HERE\n"); for (int firstPoint=0; firstPoint= numberOfPoints) { numberOfNeighbors = numberOfPoints - 1; }*/ numberOfPoints = 10; numberOfDimensions = 10; numberOfNeighbors = 5; } void init(double ***pointsArray, neighbor ***sortedNeighborsArray){ //Allocates memory for points array if ((*pointsArray = malloc((sizeof(double *)) * numberOfPoints)) != NULL){ for (int row = 0; row < numberOfPoints; ++row){ if (((*pointsArray)[row] = malloc((sizeof(double)) * numberOfDimensions)) == NULL){ if (DEBUG){ printf("Error allocating memory\n"); } exit(1); } } } else { if (DEBUG){ printf("Error allocating memory\n"); } exit(1); } //Allocates memory for neighbors array if ((*sortedNeighborsArray = malloc((sizeof(neighbor *)) * numberOfPoints)) != NULL){ for (int row = 0; row < numberOfPoints; ++row){ if (((*sortedNeighborsArray)[row] = malloc((sizeof(neighbor)) * numberOfNeighbors)) == NULL){ if (DEBUG){ printf("Error allocating memory\n"); } exit(1); } } } else { if (DEBUG){ printf("Error allocating memory\n"); } exit(1); } //Reads coordinates for the file if (readPointsArrayFromFile(pointsArray)){ if (DEBUG){ printf("Press any key to exit.\n"); getchar(); } exit(1); } //Initializes neighbors array distances to -1 for (int row=0; row pointsDistance){ //Distance at the this index is greater than the distance being inserted //Shifts right all non empty columns holding distances lower than pointsDistance for (int moveColumn=numberOfNeighbors-2; moveColumn>=arrayIndex; --moveColumn){ double tempDistance = (*sortedNeighborsArray)[firstPointId][moveColumn].distance; if (tempDistance == -1){ //Skips empty columns continue; } (*sortedNeighborsArray)[firstPointId][moveColumn+1].distance = tempDistance; (*sortedNeighborsArray)[firstPointId][moveColumn+1].neighborId = (*sortedNeighborsArray)[firstPointId][moveColumn].neighborId; } //Inserts pointsDistance in the space created after shifting (*sortedNeighborsArray)[firstPointId][arrayIndex].distance = pointsDistance; (*sortedNeighborsArray)[firstPointId][arrayIndex].neighborId = secondPointId; break; } } } void cleanUp(double ***pointsArray, neighbor ***sortedNeighborsArray){ for (int row=0; row