### Frequency Distribution Formula

I'm in the process of writing a program for school which takes in a set of values stored in a vector and outputs some basic statistical information on the set. We are supposed to have it output frequency distribution like so:

Ex. data values: 1 2 3 4 5
[ 1.00.. 1.40) = 1: 0.20
[ 1.40.. 1.81) = 0: 0.00
[ 1.81.. 2.21) = 1: 0.20
...

It is to be bucketized into 10 intervals. I'm having some trouble coming up with a formula to calculate the range. Any help would be appreciated.
 I'm having some trouble coming up with a formula to calculate the range
Is the range based on the data values? Or do you know the range at compile time?
There are a few steps to this:
1. Populate your vector of values;
2. Make a vector representing the buckets and determine the range of each bucket.
3. for each bucket, go through the list of values and check if it falls in that range, if so, increment a frequency.

To find the range for the buckets do the following:
1. Find the minimum and maximum of the vector of values.
2. for i is 1 to 10, the bucket ranges can be determined by:
minimum = i * (max-min)/numberOfBuckets
maximum = (i+1) * (max-min)/numberOfBuckets.

Here is an algorithm below. If you want to figure it out yourself stop reading now.

 ``123456789101112131415161718192021222324252627282930`` ``````struct bucket { double minimum; double maximum; double frequency; }; vector values; vector buckets; //Populate values here... //get min and max: double min=999999, max=-999999; for (vector::iterator it = values.begin(); it < values.end(); it++) { if (*it < min) min = *it; if (*it > max) max = *it; } //create buckets for (int i = 1; i < 11; i++) { bucket temp = { i * (max-min)/10. , (i+1) * (max-min)/10. , 0.}; buckets.push_back(temp); } //populate the probability density function double weight = 1./(double)values.size(); for (vector::iterator it = buckets.begin(); it < buckets.end(); it++) for (vector::iterator jt = values.begin(); jt < values.end(); jt++) if (*jt <= *it->maximum && *jt > *it->minimum) *it->frequency += weight;``````
Last edited on
Topic archived. No new replies allowed.