Inf1 OP : Lab Sheet Week 3 Q6 - Mode
Overview

In this question, you will calculate the mode of some input data. The mode of an array of numbers is the most commonly occurring value. For example, the mode of

7 2 7 3 4 7 3 4 7 7 2

would be 7, because there are five occurrences of it, which is higher than for all the other numbers. We will be dealing with just numbers of type int in this task, and they will always be between 0 and 9.

Calculate the Mode

The approach to this task could be as follows:

  1. Read the integers given as command-line arguments into an array of integers dataset[].
  2. Create a second array count[] with 10 elements, each of which will be 0. Use this array to store the number of occurrences of each integer. For example count[0] will contain the number of 0s in the dataset array, count[1] will contain the number of 1s, and so forth.
  3. Use a for-loop over the dataset[] array to increment the value in the corresponding count[] array.
  4. Print out the count[] array.
  5. Find the largest value in the count[] array; the index that gives this value will be the mode.

You can assume that there will only ever be one mode.

Write a program Mode to produce a table of the results, specifying the number of instances of each value, followed by the corresponding number of dots, as a simple visualisation. Finally, your program should print out the mode. Here is an illustration showing the required output format:

: java Mode 1 1 1
[0s: 0]
[1s: 3] ...
[2s: 0]
[3s: 0]
[4s: 0]
[5s: 0]
[6s: 0]
[7s: 0]
[8s: 0]
[9s: 0]
Mode: 1

: java Mode 1 1 1 7 7 8 3 9 8 8 8
[0s: 0]
[1s: 3] ...
[2s: 0]
[3s: 1] .
[4s: 0]
[5s: 0]
[6s: 0]
[7s: 2] ..
[8s: 4] ....
[9s: 1] .
Mode: 8

An automated test has been created for this exercise: ModeTest.java.