romsek's mathematica code from the mathhelpforum website (with the correction suggested by the author). romsek's description of his algorithm:

1) list the weights in time sequence

2) apply a slope detection filter. I'd use 2 or 3 data points per slope estimate.

3) Apply a zero slope threshold detector, i.e. discard points corresponding to the absolute value of slope greater than some threshold. This threshold will have to be varied until the standard deviation of the selected points is small enough.

4) Find the mean of the selected points as your estimated weight.

The mathematica code (ignore the initial period used for spacing.)

Code:

. (* weight data *)
weights =
{-5, 0, 10, 20, 40, 50, 70, 75, 76, 76, 77, 76, 75,
71, 54, 32, 20, 10, 0, 0} // N;
zeroSlopeThresh = 3;
slopeDetectorWindowLength = 3;
In[238]= (* add indices so we can do regression *)
weightData =
Riffle[
Array[# &, Length[weights]],
weights
];
weightData = Partition[weightData, {2}];
(* table of slope estimates *)
tt =
Table[
Normal[
LinearModelFit[
Take[
weightData,
{k, k + slopeDetectorWindowLength - 1}
],
x, x
]
]
[[2, 1]],
{k, 1, Length[weightData] - slopeDetectorWindowLength + 1}
];
(* select the weights corresponding to places
with small enough absolute value slope *)
selectedWeights =
Map[
weights[[#[[1]]]] &,
Position[tt, x_/; Abs[x] < zeroSlopeThresh]
];
(* find the mean of the selected weights *)
averageWeight = Mean[selectedWeights];
(* find the standard deviation of the selected
weights *)
weightStdDeviation = StandardDeviation[selectedWeights];
(* display the results,
the rightmost number is how many weight measurements
were included *)
{averageWeight, weightStdDeviation,
Length[selectedWeights]}
Out[244]= {76., 0.707107, 5}

I do not know mathematica so I can't translate this to C.