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
+25.5k Golang : Daemonizing a simple web server process example
+5.7k Cash Flow : 50 days to pay your credit card debt
+5.9k Golang : Compound interest over time example
+10.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+4.6k MariaDB/MySQL : Form select statement or search query with Chinese characters
+6.1k Golang : Scan forex opportunities by Bollinger bands
+6.5k Unix/Linux : How to get own IP address ?
+8.7k Golang : Get final balance from bit coin address example
+14k Golang : Fix image: unknown format error
+20.6k Golang : Read directory content with os.Open
+21.4k Golang : Encrypt and decrypt data with TripleDES
+7.4k Golang : Rot13 and Rot5 algorithms example