Ejemplo n.º 1
0
int find_max_value(int *a, int len)
{
    int i,j;    
    int *ass; 
    int range = len;
    int ass_len = len/2;
    int max_value;
    if(len == 1)
        return a[0];
    if(range % 2 != 0)
    {
        range -= 1;
        ass_len += 1;
    }

    ass = malloc(ass_len * sizeof(int));
    if(ass == NULL)
        return ERROR;
    memset(ass, 0, ass_len * sizeof(int));
    for(i = 0,j = 0; i < range; i+=2)
        ass[j++] = max(a[i], a[i+1]);
    if(range ==  len - 1)
        ass[j++] = a[range];
    max_value = find_max_value(ass, ass_len);
    del_array(&ass);
    return max_value;
}
Ejemplo n.º 2
0
int main(void)
{
    int len = 10;
    int *a;

    int ret = init_array(&a, len);
    printf("print the initial array:\n");
    print_array(a, len);
    printf("max value is %d\n", find_max_value(a, len));
    del_array(&a);

    return 0;
}
int main(int argc, char *argv[]) {
    int list[LIST_LEN] = {13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7};

    // recursive method, described in the book
    section result2 = find_max_subarray(list, 0, LIST_LEN - 1);
    print_section(result2);

    // faster algorithm
    section result1 = find_max_value(list);
    print_section(result1);

    return 0;
}
Ejemplo n.º 4
0
void DisparityTool::LoadShiftMap() {
    _shiftLevels = Load_Shift_Table(_img, _shiftMap, _shiftFunction, _path);
    _shiftMax = _shiftLevels - 1;
    _shiftMin = 0;


    if (_shiftFunctionInv != NULL)
        delete [] _shiftFunctionInv;
    
    int tmpIndex;
    double dmax, dmin;
    dmax = find_max_value(_shiftFunction, _shiftLevels);
    dmin = find_min_value(_shiftFunction, _shiftLevels);

    _shiftFunctionInv = new int[(int)(dmax-dmin)+1];
    for (int i = 0; i <= dmax-dmin; i++) {
        for (int j = 0; j < _shiftLevels; j++) {
            tmpIndex = j;
            if (_shiftFunction[j] >= i+dmin)
            break;
        }
    _shiftFunctionInv[i] = tmpIndex;
    }	
}
Ejemplo n.º 5
0
int makePlot (char *fname, char *dname, int number)
{
	FILE *fpt;
	double *on_energy;
	double *off_energy;
	double ave_on, ave_off;
	double on, off;
	int i, j;

	on_energy = (double *)malloc(sizeof(double)*number);
	off_energy = (double *)malloc(sizeof(double)*number);

	if ((fpt = fopen(fname, "r")) == NULL)
	{
		fprintf (stdout, "Can't open file\n");
		exit(1);
	}

	i = 0;
	while (fscanf(fpt, "%lf %lf", &on, &off) == 2)
	{
		on_energy[i] = on;
		off_energy[i] = off;
		i++;
	}

	if (fclose (fpt) != 0)
		fprintf (stderr, "Error closing\n");

	ave_on = 0.0;
	ave_off = 0.0;

	for (i = 0; i < number; i++)
	{
		ave_on += on_energy[i];
		ave_off += off_energy[i];
	}
	ave_on = ave_on/number;
	ave_off = ave_off/number;

	for (i = 0; i < number; i++)
	{
		on_energy[i] = on_energy[i]/ave_on;
		off_energy[i] = off_energy[i]/ave_on;
	}
	/////////////////////////////////////////////////

	float *xHis_on; // x axis of the histogram
	float *val_on;  // data value of the histogram
	float *xHis_off; // x axis of the histogram
	float *val_off;  // data value of the histogram
	int step = 100; // steps in the histogram

	//char caption[1024];
	//char text[1024];

	float max, max1, max2;

	// make histogram
	xHis_on = (float*)malloc(sizeof(float)*step);
	val_on = (float*)malloc(sizeof(float)*step);
	xHis_off = (float*)malloc(sizeof(float)*step);
	val_off = (float*)malloc(sizeof(float)*step);

	histogram (on_energy, number, xHis_on, val_on, -1.0, 4.0, step);
	histogram (off_energy, number, xHis_off, val_off, -1.0, 4.0, step);

	// plot 
	//cpgbeg(0,"/xs",1,1);
	cpgbeg(0,dname,1,1);

	cpgsch(1); // set character height
	cpgscf(1); // set character font

	// find the max
	max1 = find_max_value(step,val_off);
	max2 = find_max_value(step,val_on);
	max = (max1 >= max2 ? max1 : max2);
	//cpgenv(-5,5,0,4500,0,1); // set window and viewport and draw labeled frame
	cpgenv(-1,4,0,max+0.1*max,0,1); // set window and viewport and draw labeled frame

	//sprintf(caption, "%s", "Flux density histogram");
	cpglab("Flux (mJy)","Number","");
	cpgbin(step,xHis_on,val_on,0);
	cpgsci(2);
	cpgbin(step,xHis_off,val_off,0);
	///////////////////////////////////////////////////////
	cpgend();
	////////////////////

	free(on_energy);
	free(off_energy);
	free(xHis_on);
	free(val_on);
	free(xHis_off);
	free(val_off);

	return 0;
}
int main(int argc, char **argv) {
  struct timeval start, end;

  if (argc != 5) {
    printf("Wrong arguments.\nUsage: program textfile.txt get_word insert_word delete_word\n");
    return 0;
  }

  char * filename = argv[1];
 
  FILE * input = fopen(filename, "r");

  char buffer [BUFFER_SIZE];

  node * tree = NULL;

  clock_t begin, endd;
  double time_spent;


  // insert all 
  begin = clock();
  gettimeofday(&start, NULL);

  while(fscanf(input, "%s", buffer) != EOF) {

    char * word = malloc((strlen(buffer) + 1) * sizeof(char));
    if (word == NULL) {
      printf("Memory Error.\n");
      return 1;
    }
  
    strcpy(word, buffer);
    tolowercase(word);
    tree = add_word(tree, word);
  }
  endd = clock();
  gettimeofday(&end, NULL);
  time_spent = ((double) (endd - begin)) / CLOCKS_PER_SEC;
  time_spent *= 1000000;
  printf("Tree created. Time taken: %f * 10^(-6) secs.\n", time_spent);
  time_print(start, end);

  // search
  gettimeofday(&start, NULL);
  int s = search(tree, argv[2]);
  gettimeofday(&end, NULL);
  printf("Search for: (%s, %d)\n", argv[2], s);
  time_print(start, end);

  // insert one
  gettimeofday(&start, NULL);
  tree = add_word(tree, argv[3]);
  gettimeofday(&end, NULL);
  printf("Insert: %s\n", argv[3]);
  time_print(start, end);

  // delete

  gettimeofday(&start, NULL);
  tree = delete(tree, argv[4]);
  gettimeofday(&end, NULL);
  printf("Delete word: %s\n", argv[4]);
  time_print(start, end);

  // max
  begin = clock();
  gettimeofday(&start, NULL);
  node * max = find_max_value(tree);
  gettimeofday(&end, NULL);
  endd = clock();
  time_spent = ((double) (endd - begin)) / CLOCKS_PER_SEC;
  time_spent *= 1000000;
  printf("Max: (%s, %d). Time taken: %f * 10^(-6) secs\n", 
         max->key, max->value, time_spent);
  time_print(start, end);

  //print_tree(tree);

  return 0;
}