From d624d8483e15efa59c9165d6f883caf65175bd4e Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 6 Oct 2016 12:16:37 +0200 Subject: Initial --- histogram.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 histogram.cpp (limited to 'histogram.cpp') diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..ab6d651 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const char *argv0; + +void usage(){ + cerr<<"Usage: "< ] []"< values; + while(true){ + double v; + cin>>v; + if(!cin)break; + values.push_back(v); + if(vmaxval)maxval=v; + } + if(!havebounds){ + low=minval; + high=maxval; + } + + sort(values.begin(),values.end()); + + vector histogram(nbins); + + int binidx=0,tally=0,maxtally=-1; + for(double v : values){ + if(vhigh){ + cerr<<"Point "<=0&&binmaxtally)maxtally=tally; + binidx=bin; + tally=0; + } + tally++; + } + histogram[binidx]=tally; + if(tally>maxtally)maxtally=tally; + + char fullbar[BARWIDTH+1]; + memset(fullbar,'#',BARWIDTH); + fullbar[BARWIDTH]='\0'; + char emptybar[BARWIDTH+1]; + memset(emptybar,' ',BARWIDTH); + emptybar[BARWIDTH]='\0'; + + for(int i=0;i<(int)histogram.size();i++){ + int tally=histogram[i]; + int width=BARWIDTH*tally/maxtally; + cout<