structs.c |
| #include <stdio.h> #include <stdlib.h> #include <time.h> #define OCEAN_MAX 10000 //41981 struct ocean_datum { int x; /* grid ref east-west */ int y; /* grid ref west-south */ double conc; /* concentration of O2 in mL/L found at grid-ref (x,y) */ }; void ocean_datum_print(struct ocean_datum *o); void insertion_sort(struct ocean_datum *a, int n){ struct ocean_datum guide; int i; struct ocean_datum value; int j; guide.x=0; guide.y=0; guide.conc=0; value.x=0; value.y=0; value.conc=0; for (i=0; i<n; i++){ value = a[i]; if(a[i].conc < guide.conc){ for(j=i-1; j >=0 && a[j].conc > value.conc; j--){ a[j+1]=a[j]; } a[j+1]=value; } if (value.conc > guide.conc) guide=value; /* printf("Value: "); */ /* ocean_datum_print(&value); */ /* printf("Guide: "); */ /* ocean_datum_print(&guide); */ } } void ocean_datum_print(struct ocean_datum *o){ /* We need to reference the struct ocean_datum pointer to print it out, which is why we write (*o).x rather than o.x. Unfortunately, *o.x doeasn't work, since the "." operator has a higher precedence than the deference. o->x is shorthand for (*o).x and is a very common idiom. */ printf("o: (%d, %d, %.4f)\n", (*o).x, o->y, o->conc); } main(){ clock_t c,d; struct ocean_datum my_datum[OCEAN_MAX]; int num_items=0; int i; while(num_items < OCEAN_MAX && 3 == scanf("%d %d %lg", &my_datum[num_items].x, &my_datum[num_items].y, &my_datum[num_items].conc)){ num_items++; } c=clock(); insertion_sort(my_datum, num_items); d=clock(); // for(i=0; i<num_items; i++) // ocean_datum_print(&my_datum[i]); fprintf(stderr,"%d\t%f\n",num_items,(d-c)/(double)CLOCKS_PER_SEC); return EXIT_SUCCESS; } |
James Little |