Total members 11890 |It is currently Sat Apr 20, 2024 10:07 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Print histogram in C++
cpp code
/*
* Compute a histogram of the input file.
*
* Author: Tom Bennet
*/

#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <cctype>
#include <math.h>
#include <stdlib.h>

using namespace std;

/*
* Print one line of the histogram. It receives the character to print,
* the number of times it apeared, the maximum count, and the number of
* digits to display in the character count values.
*/
void prhistline(int chcode, int count, int maxct, int cntdigits)
{
// Names for the low-valued control characters of the ASCII set.
string ascnames[] = {
"nul", "soh", "stx", "etx", "eot",
"enq", "ack", "bel", "bs ", "ht ",
"lf ", "vt ", "ff ", "cr ", "so ",
"si ", "dle", "dc1", "dc2", "dc3",
"dc4", "nak", "syn", "etb", "can",
"em ", "sub", "esc", "fs ", "gs ",
"rs " };

// If the count is zero, omit the line.
if(count == 0) return;

// Print the character code, base 16 filled with zeros to width 2.
cout << "x" << setw(2) << setfill('0') << hex << chcode << " "
<< setfill(' ') << dec;

/* Print the character. */

// See if it is one of the ASCII control codes.
if(chcode < sizeof ascnames / sizeof ascnames[0])
// Low ASCII control codes.
cout << ascnames[chcode];
else if(chcode == 127)
// The only high ASCII control code.
cout << "del";
else if(chcode > 127)
// Byte above the ASCII set. Input must be a binary file.
// Just put spaces here.
cout << " ";
else
// An actual character.
cout << (char)chcode << " ";

// Print the count.
cout << " " << setw(cntdigits) << count << " ";

// Print the bar. This takes the ratio of the actual count to the
// maximum count, then computes that portion of the available space
// as the bar length. */
int barsize = (79 - 9 - cntdigits) *
((double)count / (double)maxct) + 0.5;
while(barsize--) cout << "#";
cout << endl;
}

/*
* Read the file and print the histogram.
*/

const int HISSIZE = 256;
int main()
{
int hist[HISSIZE]; /* Count of bytes. */
int maxct = 0; /* Maximum of any count. */
int cntdigits; /* Num digits for printing count. */

// Zero out all the counts. */
memset(hist, 0, sizeof hist);

/* Read the input file and count each character. */
char inch;
while(cin.get(inch)) {
/* Update the count, and the maximum. */
if(++hist[inch] > maxct)
maxct = hist[inch];
}

/* Compute the number of spaces needed for printing the counts */
cntdigits = log10((float)maxct) + 1.00001;

/* Print the histogram. */
for(int m = 0; m < HISSIZE; ++m)
prhistline(m, hist[m], maxct, cntdigits);
}


Character Histogram Generator



_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Histogram Generator
 drawing the histogram of image     -  
 image equalization and cumulative histogram     -  
 Image Colors Frequencies Histogram in Java     -  
 Sequence Generator JPA     -  
 Calendar Generator     -  
 JPA entity with table generator     -  
 Prime Generator Algorithm     -  
 HTML Table Of Contents Generator     -  
 Pseudo Randon Number Generator code??     -  



Topic Tags

C++ Algorithms
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com