Input/Output: Graphics and Arrays: A Bar graph – Tracing

Let’s do some tracing with a smaller set of positive integers.

raw data array
{4,5,6,5,5,12,4,5,4,5,4,4,11,5,…} 100 random positive integers of range 1 to 20 inclusive

the goal in this assignment is to have the least number of “if” statements (if any at all)

the counter array initialy all elements are equal to ZERO — this is an array of 20 elements
counter array
{0,0,1,0,5,5,….} this is going to be the height of the rectangle

i: 0 –> 20 barCounter[0,0,0,0,0,0,0,0,0,..]

barCounter[rawData[i]]++

i = 0
rawData[0] –> 4
barCounter[rawData[0]]++
barCounter[4]++ –> 1 barCounter[0,0,0,0,1,0,0,0,0,..]

i = 1
rawData[1] –> 5
barCounter[rawData[1]]++
barCounter[5]++ –> 1 barCounter[0,0,0,0,1,1,0,0,0,..]

i = 2
rawData[2] –> 6
barCounter[rawData[2]]++
barCounter[6]++ –> 1 barCounter[0,0,0,0,1,1,1,0,0,..]


i = 3
rawData[3] –> 5
barCounter[rawData[3]]++
barCounter[5]++ –> 2 barCounter[0,0,0,0,1,2,1,0,0,..]

i = 4
rawData[4] –> 5

barCounter[5]++ –> 3 barCounter[0,0,0,0,1,3,1,0,0,..]

i = 20 [0,1,0,0,1,6,9,0,18,19,17……,1,1] // height of
//each rectangle
// the index is the x coordinate of the bargraph rectangle
for ( int i = 0; i < 100; i++)
barCounter[rawData[i]]++;

for loop to draw the rectangles where x is the index of barCounter array
and y is the value of the array barCounter with index x

// a short program to trace it with print statements
public class bargraph0
{
    public static void main(String [] args)
    {
        int raw = 0;
        int counter =0;
      int [] rawData = {4,5,6,5,5,12,4,5,4,5,4,4,11,5};
      int [] barCounter = new int[13]; // since 12 is the max value
      for ( int i = 0; i < rawData.length; i++)
      {
          System.out.println("i " + i + " rawData " + rawData[i]);
            barCounter[rawData[i]]++;
            raw = rawData[i];
            counter = barCounter[rawData[i]];
            System.out.println("barCounter array");
            for ( int j = 0; j < barCounter.length; j++)
            {
                System.out.print(barCounter[j] + " ");
            }
            System.out.println();
        }
    }
    
}
Take a look at the output:
Inital  state of the barCounter array
0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop with i from 0 to 13
i 0 rawData 4
barCounter array
0 0 0 0 1 0 0 0 0 0 0 0 0 
i 1 rawData 5
barCounter array
0 0 0 0 1 1 0 0 0 0 0 0 0 
i 2 rawData 6
barCounter array
0 0 0 0 1 1 1 0 0 0 0 0 0 
i 3 rawData 5
barCounter array
0 0 0 0 1 2 1 0 0 0 0 0 0 
i 4 rawData 5
barCounter array
0 0 0 0 1 3 1 0 0 0 0 0 0 
i 5 rawData 12
barCounter array
0 0 0 0 1 3 1 0 0 0 0 0 1 
i 6 rawData 4
barCounter array
0 0 0 0 2 3 1 0 0 0 0 0 1 
i 7 rawData 5
barCounter array
0 0 0 0 2 4 1 0 0 0 0 0 1 
i 8 rawData 4
barCounter array
0 0 0 0 3 4 1 0 0 0 0 0 1 
i 9 rawData 5
barCounter array
0 0 0 0 3 5 1 0 0 0 0 0 1 
i 10 rawData 4
barCounter array
0 0 0 0 4 5 1 0 0 0 0 0 1 
i 11 rawData 4
barCounter array
0 0 0 0 5 5 1 0 0 0 0 0 1 
i 12 rawData 11
barCounter array
0 0 0 0 5 5 1 0 0 0 0 1 1 
i 13 rawData 5
barCounter array
0 0 0 0 5 6 1 0 0 0 0 1 1 
i 0 rawData 4
barCounter array
0 0 0 0 1 0 0 0 0 0 0 0 0 
i 1 rawData 5
barCounter array
0 0 0 0 1 1 0 0 0 0 0 0 0 
i 2 rawData 6
barCounter array
0 0 0 0 1 1 1 0 0 0 0 0 0 
i 3 rawData 5
barCounter array
0 0 0 0 1 2 1 0 0 0 0 0 0 
i 4 rawData 5
barCounter array
0 0 0 0 1 3 1 0 0 0 0 0 0 
i 5 rawData 12
barCounter array
0 0 0 0 1 3 1 0 0 0 0 0 1 
i 6 rawData 4
barCounter array
0 0 0 0 2 3 1 0 0 0 0 0 1 
i 7 rawData 5
barCounter array
0 0 0 0 2 4 1 0 0 0 0 0 1 
i 8 rawData 4
barCounter array
0 0 0 0 3 4 1 0 0 0 0 0 1 
i 9 rawData 5
barCounter array
0 0 0 0 3 5 1 0 0 0 0 0 1 
i 10 rawData 4
barCounter array
0 0 0 0 4 5 1 0 0 0 0 0 1 
i 11 rawData 4
barCounter array
0 0 0 0 5 5 1 0 0 0 0 0 1 
i 12 rawData 11
barCounter array
0 0 0 0 5 5 1 0 0 0 0 1 1 
i 13 rawData 5
barCounter array
0 0 0 0 5 6 1 0 0 0 0 1 1 
Final state of the barCounter array.