Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- int main(void){
- unsigned int k; // interval size
- unsigned int n; // data set size
- unsigned int m; // maximum value
- unsigned int l; // number of intervals
- unsigned int * values; // data set
- unsigned int * histogram; // histogram
- // read number of intervals
- cin >> l;
- // read size of the data set
- cin >> n;
- // allocate array to store the data set
- values = new unsigned int[n];
- // read the data set into the values array
- for(unsigned int i=1; i<=n; i++){
- cin >> values[i-1];
- }
- // compute the maximum value (to decide the size of the histogram
- // could be done in the above loop, but just to do one thing at a time
- m = 0;
- for(unsigned int i=1; i<=n; i++){
- if (values[i-1] > m) {
- m = values[i-1];
- }
- }
- // calculate the size of intervals
- k = ceil( m / (double)l);
- // subtle case that arises if m = 0 yielding 0 size
- if (k == 0) k=1;
- // allocate histogram array
- histogram = new unsigned int[l];
- // initialize histogram
- for(unsigned int i=0; i<l; i++){
- histogram[i]=0;
- }
- // compute histogram based on dataset
- for(unsigned int i=0; i<n; i++){
- histogram[min(values[i]/k,l-1)]++;
- }
- // output the result
- for(unsigned int i=0; i<l; i++){
- cout << i*k << ": " << histogram[i] << endl;
- }
- // deallocate arrays array
- delete [] values;
- delete [] histogram;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement