Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int main(void){
  6.    
  7.     unsigned int k;             // interval size
  8.     unsigned int n;             // data set size
  9.     unsigned int m;             // maximum value
  10.     unsigned int l;             // number of intervals
  11.     unsigned int * values;      // data set
  12.     unsigned int * histogram;   // histogram
  13.    
  14.     // read number of intervals
  15.     cin >> l;
  16.     // read size of the data set
  17.     cin >> n;
  18.    
  19.     // allocate array to store the data set
  20.     values = new unsigned int[n];
  21.    
  22.     // read the data set into the values array
  23.     for(unsigned int i=1; i<=n; i++){
  24.         cin >> values[i-1];
  25.     }
  26.    
  27.     // compute the maximum value (to decide the size of the histogram
  28.     // could be done in the above loop, but just to do one thing at a time
  29.     m = 0;
  30.     for(unsigned int i=1; i<=n; i++){
  31.         if (values[i-1] > m) {
  32.             m = values[i-1];
  33.         }
  34.     }
  35.    
  36.     // calculate the size of intervals
  37.     k = ceil( m / (double)l);
  38.    
  39.     // subtle case that arises if m = 0 yielding 0 size
  40.     if (k == 0) k=1;
  41.    
  42.     // allocate histogram array
  43.     histogram = new unsigned int[l];
  44.    
  45.     // initialize histogram
  46.     for(unsigned int i=0; i<l; i++){
  47.         histogram[i]=0;
  48.     }
  49.    
  50.     // compute histogram based on dataset
  51.     for(unsigned int i=0; i<n; i++){
  52.         histogram[min(values[i]/k,l-1)]++;
  53.     }
  54.    
  55.     // output the result
  56.     for(unsigned int i=0; i<l; i++){
  57.         cout << i*k << ": " << histogram[i] << endl;
  58.     }
  59.    
  60.     // deallocate arrays array
  61.     delete [] values;
  62.     delete [] histogram;
  63.    
  64.     return 0;
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement