Retro Weekend Challenge — Histogram in C
Bored with all plug and play JS dev, I thought of going retro this weekend and took out the K&R. In the introduction, there was an exercise which author deemed bit challenging:
Write a program to print a histogram (vertical) of the frequencies of different characters in its input.
Here is my take on it.
First of all, the following are the constraints or limitations of this solution:
- Shows histogram of only English alphabets and digits
- Avoid any external helper libraries
- Limited to only *nix/ Mac platforms
- bar == ‘-‘
Depth First
The first approach I thought of trying is drawing out all the bars for a given digit/alphabet and then somehow move the cursor back to the initial position and start drawing again for next array element. However, nothing came on top of my head to solve that somehow part. So I went for the next approach. (Apparently, this can be done with terminal control escape sequence. )
Breadth First
In the second approach, I first found the max count among all elements of the digit/alphabet array. Then I ran a loop from s-> 1 to the max, meanwhile scanning each array element. If an array element has a count >= s, we draw a bar or else space. After one complete iteration across all array elements, program moves to the next line and repeat the exercise.
Moody terminal
The extra complexity crept into solution due to possible variations in the terminal width. To solve this, I use the *nix system APIs to get the current terminal width and deal with the case where it’s lesser than what’s required to display all array elements. Simply put, we create and process batches of the set of array elements as per terminal width (including the possible extra elements in left after the last batch).
Hope you enjoyed this as much as I did. Please do let me any you find any cool optimizations or better clever solutions for this.
Possible improvements
Use of ncurses — a terminal-independent library
As a side note, there is this Rust project Notty in progress which aims to take a totally modernistic approach to the terminal, including rich text formatting, inline media contents etc. Interesting 😉
Originally published at unprogrammer.com on December 23, 2018.