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 $\pi$. 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 $\pi$ 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 $\pi$.

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:


See your results here:

This code will not run! You have to make 3 fixes:
  1. What will you put in for digit[i] = line so that the $i^{th}$ element of array digit contains the $i^{th}$ possible digit of $\pi$ running from $0$ to $9$?
  2. What will you put in for the hist[i] line to initially set all histogram counts equal to zero?
  3. For the line inside of the if statement, the variable d contains the current digit of $\pi$ found in bigpi (i.e. a number from 0 to 9), what how will you fix the hist[d] = line so that hist[d] is increased by one relative to its currently held count? Hint: It's related to what's in this lesson. Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.