// problem 2.3
Node* find_previous(Node *root) 
{
  if(root==NULL)
  {
    return root;
  }
  Node * parent=root->parent;
  if(root->a != NULL)
  {
    return find_largest(root->a);
  }
  if(parent==NULL)
  {
    return parent;
  }
  if(parent->c!=NULL && parent->c->value==root->value)
  {
    if(parent->b!=NULL)
      return find_largest(parent->b);
    else
      return parent;
  }
   if(parent->b!=NULL && parent->b->value==root->value)
  {
      return parent;
  }
  if(parent->a!=NULL && parent->a->value==root->value)
  {
      return find_previous(parent);
  }
}
// problem 2.2
Node* find_largest(Node *root) 
{
if(root==NULL || (root->b==NULL && root->c==NULL))
{
  return root;
}
if(root->c==NULL && root->b!=NULL)
{
  find_largest(root->b);
}
else
{
  find_largest(root->c);
}
}
Example #3
0
int main(void)
{
	int temperature[7][24], find, *p, result, i = 1, maximum, m = 0;
	for (p = &temperature[0][0]; p <= &temperature[7 - 1][24 - 1]; p++){
		*p = m++;
	}

	printf("The following is the array you entered: \n");
	for (p = &temperature[0][0]; p <= &temperature[7 - 1][24 - 1]; p++, i++){
		printf("%d\t", *p);
		if (i % 24 == 0){
			printf("\n");
		}
	}

	printf("Enter the value you want to find: ");
	scanf("%d", &find);
	result = search(temperature, find);
	if (result){
		printf("Find the value %d in array\n", find);
	}
	else{
		printf("Cannot find value %d in array\n", find);
	}

	printf("The maximum value for each row: \n");
	for (int i = 0; i < 7; i++){
		maximum = find_largest(i, temperature);
		printf("Day %d: %d\n", i + 1, maximum);
	}
	return 0;
}
int main()
{
	int j,a[5]={1,2,3,8,7};
	printf("随意一个含有5个元素的数组为:\n");
    for(j=0;j<5;j++)
    printf("%d,",a[j]);
    printf("\n");
	printf("最大的数是%d\n",*find_largest(a,5));
	return 0;
}
int main() {

  Node* root = NULL;

  // create the diagram on the test handout
  insert(root,30);
  insert(root,10);
  insert(root,32);
  insert(root,70);
  insert(root,5);
  insert(root,16);
  insert(root,24);
  insert(root,31);
  insert(root,52);
  insert(root,64);
  insert(root,92);
  insert(root,200);
  insert(root,12);
  insert(root,19);
  insert(root,45);
  insert(root,59);
  insert(root,150);
  insert(root,210);
  insert(root,450);
  insert(root,13);
  insert(root,4);
  insert(root,1);

  // print the tree before iteration
  print(root,"");
  std::cout << std::endl;
  std::cout << std::endl;

  // loop over the structure
  int count = 0;
   Node *tmp = find_largest(root);
   std::cout<<tmp->value;
  int prev = tmp->value;
  while (tmp != NULL) {
    std::cout << tmp->value << " ";
    tmp = find_previous(tmp);
    if (tmp != NULL) { 
      assert (prev > tmp->value);
      prev = tmp->value;
    }
    count++;
  }
  std::cout << std::endl;
  assert (count == 22);

  // cleanup so we have no memory leaks
  destroy (root);
}
Example #6
0
/* void selection_sort(int *a, int n), recusive fuction the repatedly finds the largest value of the array int *a points to swaping the largest value to the back to the array then recalling the selection_sort fuction with int n being reduced by 1 to keep the pervious largest value at the end of array a[N] the is called in the main fuction. recursion ends when n equals 1.
 */ 
void selection_sort(int *a, int n)
{   

	if (n == 1)
	{
    		return;
	}

	int *largest = find_largest(a, n); /* find largest value */

	swap(largest, (a + (n - 1))); /* swap largest to end of array */
 
	selection_sort(a, n - 1); /* perform actions again ignoring last array value */
}
Example #7
0
/********************************************************
 * main: implement find_largest() function
 ********************************************************/
int main(void)
{
	PRINT_FILE_INFO

	int *lrg_ptr; /* stores index to largest element within a[] */
	int a[MAX];

	random_fill(MAX, a, MAX);

	for(int i = 0; i < MAX; ++i)
		printf("a[%d]: %d\n%s", i, a[i], i == MAX -1 ? "\n" : "");

	lrg_ptr = find_largest(a, MAX);
	printf("*lrg_ptr:\t %02d\n\n", *lrg_ptr);

	printf("lrg_ptr: %ld, &a[0]: %ld, a: %ld\n", lrg_ptr, &a[0], a);
	printf("lrg_ptr - &a[0]: %02ld\n", lrg_ptr - &a[0]);
	printf("lrg_ptr - a[0]:\t %02ld\n", lrg_ptr - a);

	return 0;
}
Example #8
0
int main() {
    int array[] = {1, -2, 3, 10, -4, 7, 2, -5};
    //int array[] = {-8, -1, -3, -10, -4, -7, -2, -5};
    //int array[] = {-8, -1, -3, 10, -4, -7, -2, -5};
    int len = 8;
    res r;
    if(!check_neg(array, len)) {
        r = find_greatest_sum_with_memory(array, len);
    } else {
        r = find_largest(array, len);
    }
    printf("start: %d\n", r.start);
    printf("end: %d\n", r.end);
    printf("sum: %d\n", r.sum);
    int i = 0;
    printf("The sequence : ");
    for(i = r.start; i <= r.end; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");
    return 0;
}
int main(int argc, const char * argv[]) {
    int *p;
    int a[N_row][N_line] = {{1,3,4,5,6,3,2,5,6,8},{4,3,1,7,8,3,2,9,6,0}};
    find_largest(p, a);
    return 0;
}