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
+24.9k Golang : Convert uint value to string type
+5k Golang : Get FX sentiment from website example
+13.7k Golang : Get current time
+11.3k CodeIgniter : Import Linkedin data
+7.4k Gogland : Where to put source code files in package directory for rookie
+13.9k Golang : Get uploaded file name or access uploaded files
+12.2k Golang : Exit, terminating or aborting a program
+8.1k Golang : Implementing class(object-oriented programming style)
+13k Golang : Date and Time formatting
+6.8k Golang : A simple forex opportunities scanner
+11.3k Golang : Display a text file line by line with line number example
+12.3k Golang : Remove or trim extra comma from CSV