Listing 1: The data array
Listing 1: The data array
// "Object" for five data points
static double x[5], y[5]; // data array
static int npoints = 0; // number of valid points in array
static int index = 0; // location of current minimum
// insert a point at location k; shift left
void insert_left(double X, double Y, int k){
int i;
assert((k >= 0) && (k < 5));
assert(k <= npoints);
if(npoints > 0){
// shift points to the left of k
for(i = 1; i<=k; i++){
x[i-1] = x[i];
y[i-1] = y[i];
}
}
else
npoints = 1;
// insert new point
x[k] = X;
y[k] = Y;
}
// insert a point at location k; shift right
void insert_right(double X, double Y, int k){
int i;
assert((k >= 0) && (k < 5));
assert(k <= npoints);
// shift points to the right of k
for(i = min(npoints,4); i>k; i--){
x[i] = x[i-1];
y[i] = y[i-1];
}
// insert new point
x[k] = X;
y[k] = Y;
npoints = min(npoints+1, 5);
}
// insert a point at end of list
// shift left if list is full
void append(double X, double Y){
if(npoints < 5)
insert_right(X, Y, npoints);
else
insert_left(X, Y, 4);
}
|