#include #include #include #include "knnSerialDeclarations.h" void getArguments(int argc, char** argv){ if (argc != 6) { printf("Usage: %s p d k filename tfilename\nwhere:\n", argv[0]); printf("\tp is the the number of points\n"); printf("\td is the number of dimensions of each point\n"); printf("\tk is the number of neighbors to search for\n"); printf("\tfilename is the filename of the dataset file\n"); printf("\tfilename is the filename of the test file\n"); exit(1); } numberOfPoints = atoi(argv[1]); numberOfDimensions = atoi(argv[2]); numberOfNeighbors = atoi(argv[3]); if (numberOfNeighbors >= numberOfPoints) { numberOfNeighbors = numberOfPoints - 1; } pointsFilename = argv[4]; testFilename = argv[5]; } void init(double ***pointsArray, neighbor ***sortedNeighborsArray){ //Allocates continuous memory for points array *pointsArray = cMalloc(numberOfPoints, numberOfDimensions); //Allocates memory for neighbors array if ( (*sortedNeighborsArray = (neighbor**)(malloc((sizeof(neighbor *)) * numberOfPoints)) ) != NULL){ for (int row = 0; row < numberOfPoints; ++row){ if ( ((*sortedNeighborsArray)[row] = (neighbor*)(malloc((sizeof(neighbor)) * numberOfNeighbors)) ) == NULL){ printf("Error allocating memory\n"); exit(1); } } } else { printf("Error allocating memory\n"); exit(1); } //Reads coordinates from the file if (readPointsArrayFromFile(pointsArray)){ printf("Press any key to exit.\n"); getchar(); exit(1); } //Initializes neighbors array distances and ID's to -1 for (int point=0; point pointsDistance){ //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; } } } int test(neighbor ***sortedNeighborsArray, char *testFilename){ FILE *testFile = fopen(testFilename, "r"); if (testFile == NULL){ printf("Couldn't open test file.\n"); perror("fopen"); return 1; } for (int point=0; point