Object | +---DDiscreteDistribution
The DDiscreteDistribution class implements a (discrete) distribution class. The distribution object is updated with new values. Its task is to determine the length, the average or mean, the variance, the standard deviation, the sum and the squared sum of all values in the distribution. For the scores the length and percentage of every score can be determined. The distribution object is not a collection: the values are not stored in the object; the state of the object is updated with the new value.
#include <stdio.h> #include "ofc/DDiscreteDistribution.h" int main(int argc, char *argv[]) { DDiscreteDistribution *dis = [DDiscreteDistribution new]; DListIterator *iter; double d; // Setup the discrete ranges for (d = 0.0; d < 5.0; d += 0.5) { [dis range :d :d+0.5]; } // Set the values for the distribution if (![dis update :0.1]) printf("Value 0.1 is not valid for the distribution.\n"); if (![dis update :4.2]) printf("Value 4.2 is not valid for the distribution.\n"); if (![dis update :3.6]) printf("Value 4.2 is not valid for the distribution.\n"); if (![dis update :2.2]) printf("Value 4.2 is not valid for the distribution.\n"); if (![dis update :0.5]) printf("Value 4.2 is not valid for the distribution.\n"); if (![dis update :3.0]) printf("Value 4.2 is not valid for the distribution.\n"); if (![dis update :2.1]) printf("Value 4.2 is not valid for the distribution.\n"); if (![dis update :4.9]) printf("Value 4.2 is not valid for the distribution.\n"); // Calculate some numbers printf("Number of values in the distribution:%d.\n", [dis length]); printf("Sum of values in the distribution:%.1f.\n", [dis sum]); printf("SqSum of values in the distribution:%.1f.\n", [dis sumSquared]); printf("Mean of values in the distribution:%.1f.\n", [dis mean]); printf("StdDev of values in the distribution:%.1f.\n", [dis standardDeviation]); // Scores iterator iter = [dis scores]; if (iter != nil) { DScore *sc = [iter first]; while (sc != nil) { printf("Score:[%.1f-%0.1f] sum:%.1f sq-sum:%.1f perc:%.1f%%.\n", [sc min], [sc max], [sc sum], [sc sumSquared], [sc percentage]); sc = [iter next]; } } else printf("No scores in the distribution ?!\n"); [iter free]; [dis free]; // Cleanup return 0; }