/*************************************************
This program reads in numbers in the range 0 to 100
and prints out a histogram of the numbers, with
"bins" for the ranges [0,10), [10-20), ... ,[90,100].
*************************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//-- PROTOTYPES -----------------------------//
int bin(double x);
void rep(char c, int k, ostream& OUT);
//-- MAIN -----------------------------------//
int main()
{
// Create and initialize hostgram
int *H = new int[10];
for(int i = 0; i < 10; i++)
H[i] = 0;
// Read values and update histogram counts
ifstream IN("data.txt");
double value;
while(IN >> value)
H[ bin(value) ]++;
// Print out histogram in ASCII
for(int k = 0; k < 10; k++)
{
cout << k << ':';
rep('*',H[k],cout);
cout << endl;
}
return 0;
}
//-- FUNCTION DEFINITIONS -------------------//
// Categorizes value x into bins 0, 1, 2, ..., 9
int bin(double x)
{
int i = x/10;
if (i > 9)
i = 9;
if (i < 0)
i = 0;
return i;
}
void rep(char c, int k, ostream& OUT)
{
for(int i = 0; i < k; i++)
OUT << c;
}