C++ code for: Write a program to print a histogram of the frequencies of different characters in its input. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

C++ code for: Write a program to print a histogram of the frequencies of different characters in its input.

Hi everybody. I ask this because this question (Exercise 1-14 from Kernighan - Richie's C book) is making me suffer. It gives you the following program, so the only thing left is adding the histogram (which, I have no idea how to). #include <stdio.h> main( ) { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c=getchar( ))!=EOF) if (c >= '0' && c <= '9') ++ ndigit [c-'0']; else if (c==' ' || c=='\n' || c=='\t') ++nwhite; else ++nother; printf ("digits ="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", blank spaces = %d, other = %d\n", nwhite, nother); i=0; } So.. this program count the frecuency of each of the digits you type (from 0 to 9), the # of blank spaces and other characters. I don't care about the later two. I just want help about the creation of an histogram using the frecuencies of the numbers you type. (Maybe this book is too old and I should know more recent/useful functions, like cin in exchange of getchar().. I dunno. Someone help me :'( )

12th Dec 2017, 7:26 PM
Augusto Alvarez
Augusto Alvarez - avatar
2 Answers
+ 3
To start with, you could try a simple character based graph(maybe vertical rather than horizontal) A **** B ******* C ** .. ( that would be in line with whst was done when that book was first published, and nice graphs were not as popular as now :-))
1st Jan 2018, 3:44 PM
ifl
ifl - avatar
+ 1
The code is written in C, not C++. Take note that C and C++ are different. Hope this would help: https://code.sololearn.com/cAnMmhIOBWpp/#c
16th Jan 2018, 8:06 AM
Hanz
Hanz - avatar