Processing math: 100%

Lesson goal: Histogram the digits of Pi

Previous: Arrays and histograms | Home | Next: Histogramming uniform rnd nums

In a past lesson we counted the occurrences of each digit in π. This lesson is a bit of a repeat of that lesson, but does an analysis using arrays and the creation of a professional looking histogram chart.

In this case, each digit of π is read from the variable bigpi. Each digit is used as an index into an array called hist that keeps track of the number of times a given digit occurs in bigpi. Another array called digit is used to seed the horizontal axis of the histogram, as it is loaded with the numbers from 0 to 9, which are the possible digits in π.

Now you try. Fix the digit[i] = so digit will hold the numbers from 0 to 9. Fix the hist[i]= line so that all counts start at zero. Lastly, fix the hist[d] = line so that it is increased by one inside of the if statement.

Type your code here:

17
 
1
hist={}
2
digit={}
3
4
for i=0,9 do
5
 digit[i] = ???
6
 hist[i] = ???
7
end
8
9
for i=1,string.len(bigpi) do
10
 d = string.sub(bigpi,i,i)
11
 if (d ~= ".") then
12
   d=tonumber(d)
13
   hist[d] = ???
14
 end
15
end
16
17
drawchart(digit,hist,"bar")

See your results here: