Golang : Simple histogram example
This is a simple histogram program written in Golang. Can be useful in situations where you want to roughly assess the probability distribution of a given variable by depicting the frequencies of observations occurring in certain ranges of values.
Here you go!
package main
import (
"fmt"
)
const SIZE = 10
var n = [SIZE]int{20, 7, 11, 13, 1, 3, 30, 4, 9, 2}
func main() {
fmt.Printf("%sss\n", "Element", "Value", "Histogram")
for i := 0; i <= SIZE-1; i++ {
fmt.Printf("%7dd ", i, n[i])
for j := 1; j <= n[i]; j++ {
//fmt.Printf("%c", '*')
fmt.Printf("%c", '∎')
}
fmt.Println()
}
}
Output:
Element Value Histogram
0 20 ∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ 1 7 ∎∎∎∎∎∎∎ 2 11 ∎∎∎∎∎∎∎∎∎∎∎ 3 13 ∎∎∎∎∎∎∎∎∎∎∎∎∎ 4 1 ∎ 5 3 ∎∎∎ 6 30 ∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ 7 4 ∎∎∎∎ 8 9 ∎∎∎∎∎∎∎∎∎ 9 2 ∎∎
Reference:
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11.3k Swift : Convert (cast) Float to String
+21.1k Golang : How to read float value from standard input ?
+8.8k Golang : Simple histogram example
+6.4k Golang : Skip or discard items of non-interest when iterating example
+19.6k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+6.6k Golang : Muxing with Martini example
+4.5k JavaScript: Add marker function on Google Map
+11.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+8.2k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+13.7k Golang : Reverse IP address for reverse DNS lookup example
+11.7k Golang : Sort and reverse sort a slice of runes
+10.7k Golang : Replace a parameter's value inside a configuration file example