Example #1
0
int main(int argc, char *argv[])
{
  int tam, i;
  printf("Forneca o tamanho do vetor ");
  scanf("%d", &tam);
  int vet[tam], * par, * unico, maiorFrequencia;
  
  for (i=0;i<tam;i++){
      printf("Forneca o valor da %d posicao ", i+1);
      scanf("%d", &vet[i]);
  }
  
  ordenaVetor(vet, 0, tam-1);

  par = closestPair(vet, tam);  // exec 1
  /*printf("\n");
  for (i=0;i<2;i++){
      printf("%d  ",par[i]);
  }*/
  
  unico = elementUniqueness(vet, tam); // exec 2
  /*printf("\n");
  for (i=0;i<tam;i++){
      printf("%d  ",unico[i]);
  }*/
  
  maiorFrequencia = frequencyDistribution(vet, tam); //exec 3    
  //printf("%d",maiorFrequencia);
  
  system("PAUSE");	
  return 0;
}
int main(int argc, char *argv[]){
	FILE *fp_r_inputSet;
    unsigned int record_count = 0, running_count = 0;
    _Bool record_count_specified = false;
 	int **points, **points_sorted_onX, **points_sorted_onY;
    char usage[] = "usage: ./ClosestPair \
[Input_file_name No_of_points]";

	int **closest_points;

    switch (argc) {
        case 1:
            fp_r_inputSet = fopen("InputPointSet.txt", "r");
            break;
        case 2:
            fp_r_inputSet = fopen(argv[1], "r");
            break;
        case 3:
            fp_r_inputSet = fopen(argv[1], "r");
            record_count = atoi(argv[2]);
            record_count_specified = true;
            break;
        default:
            printf("Invalid arguments.\n%s\n", usage);
    }

    if (fp_r_inputSet == NULL) {
        printf("Input file doesn't exist. Exiting\n");
        exit(-1);
    }

    /* see one note chapter on Trics in notebook- C Programming for details */
    if (!record_count_specified) {
        while (EOF != (fscanf(fp_r_inputSet, "%*[^\n]"), 
        		fscanf(fp_r_inputSet, "%*c")))
            ++record_count;
    }
    rewind(fp_r_inputSet);
    printf("Number of pairs: %d\n", record_count);
    if (record_count == 0) {
    	printf("Exiting! 0 records found!\n");
    	exit(-1);
    }
    
   	/* allocating space to store all the points */
    points = (int **) malloc(2 * sizeof(int));
    points[0] = (int *) malloc(record_count * sizeof(int));
    points[1] = (int *) malloc(record_count * sizeof(int));
    
    closest_points = (int **) malloc(2 * sizeof(int));
    closest_points[0] = (int *) malloc(record_count * sizeof(int));
    closest_points[1] = (int *) malloc(record_count * sizeof(int));
    
    /*Creating copies of points sorted based on x and y coordinates
    	arr_sorted_onX
    	arr_sorted_onY, respectively
    */
   	//alocating space
   	points_sorted_onX = (int **) malloc(2 * sizeof(int));
    points_sorted_onX[0] = (int *) malloc(record_count * sizeof(int));
    points_sorted_onX[1] = (int *) malloc(record_count * sizeof(int));
    points_sorted_onY = (int **) malloc(2 * sizeof(int));
    points_sorted_onY[0] = (int *) malloc(record_count * sizeof(int));
    points_sorted_onY[1] = (int *) malloc(record_count * sizeof(int));

    /* reading all the records in array */
    for(running_count = 0; running_count < record_count; running_count++) {
        fscanf(fp_r_inputSet, "%d\t%d", &points[0][running_count], 
        		&points[1][running_count]);
		points_sorted_onX[0][running_count] = points[0][running_count];
		points_sorted_onX[1][running_count] = points[1][running_count];
		points_sorted_onY[0][running_count] = points[0][running_count];
		points_sorted_onY[1][running_count] = points[1][running_count];
    }
    
    printf("printing pair of points\n"); 
    for(running_count = 0; running_count < record_count; running_count++) {
        printf("%d\t%d\n", points[0][running_count], 
        		points[1][running_count]);
    }

    
    //calling merge sort on these one  by one
    mergeSort(points_sorted_onX[0], points_sorted_onX[1], record_count);
    mergeSort(points_sorted_onY[1], points_sorted_onY[0], record_count);
    
    printf("printing pair of points\n"); 
    for(running_count = 0; running_count < record_count; running_count++) {
        printf("%d\t%d\n", points_sorted_onX[0][running_count], 
        		points_sorted_onX[1][running_count]);
    }

    printf("printing pair of points\n"); 
    for(running_count = 0; running_count < record_count; running_count++) {
        printf("%d\t%d\n", points_sorted_onY[0][running_count], 
        		points_sorted_onY[1][running_count]);
    }
   

	//calling function to calculate closest pair
	closest_points = closestPair(points_sorted_onX, points_sorted_onY, 
						record_count);
	
	printf("=================== Printing closest pair =====================\n");
	printf("(%d, %d) and (%d, %d)\n", closest_points[0][0], closest_points[1][0],
										closest_points[0][1], closest_points[1][1]);
/*    free(points[0]);
    free(points[1]);
    free(points);
    free(points_sorted_onX[0]);
    free(points_sorted_onX[1]);
    free(points_sorted_onY[0]);
    free(points_sorted_onY[1]);
    free(points_sorted_onX);
    free(points_sorted_onY);
*/    

	return EXIT_SUCCESS;
}