# Array counting problem

• July 29th, 2013, 08:30 PM
Variumzky
Array counting problem
Hello, so i saw a challenge on youtube which wanted you to generate 100 values from 1-6 and show the percentages I can't figure out what to put into the last bit of code, the diceCount brackets.
Code Java:

```import java.util.Random; public class tutorial_23 { public static void main(String args[]){ Random dice = new Random(); int number; int diceCount[]=new int[6]; for(int counter=1;counter<=100;counter++){ number = 1+dice.nextInt(6); if (number==1) ++diceCount[0];   else if (number==2) ++diceCount[1];   else if (number==3) ++diceCount[2];   else if (number==4) ++diceCount[3];   else if (number==5) ++diceCount[4];   else if (number==6) ++diceCount[5];   System.out.println("Index\tValue"); for(int arrayCount = 0; arrayCount < diceCount.length; arrayCount++){ System.out.println(1+arrayCount + "\t" + diceCount[HERE I GET AN ERROR, DONT KNOW WHAT TO PUT IN HERE!] + "%"); }       //this is the tutorial, with the challenge that creates 100 values from 1-6 and then determines how many 1-6s it counted.   } } }```
• July 30th, 2013, 12:11 AM
jps
Re: Array counting problem
[HERE I GET AN ERROR, DONT KNOW WHAT TO PUT IN HERE!]

...what is the error?
...how should we know what to put in there? What should it do?

My suggestion is to put the code to the side for a minute, and outline how you would solve the problem if you walked into a room and was handed a pencil and paper. How would you keep track of how many of each number was rolled?
Once you have that outline finished translate it to code. This is how you figure out "what to put in there", you figure out "what it should do, exactly, in step by step order"
• July 31st, 2013, 04:52 PM
llowe29
Re: Array counting problem
dice count wouldnt be an array it would seperate integers for each coun
• August 1st, 2013, 04:15 AM
syedbhai
Re: Array counting problem
Hello,
Just think of mathematics.

Syed.
• August 1st, 2013, 05:53 AM
GregBrannon
Re: Array counting problem
A frequency counter is possibly one of the coolest ways to use an array, and no switch statement is required.

For a random number generator that generates numbers 0 - n
Create an array to count the frequency of occurrence of each random number of size 'n + 1'
For each random number generated, increment the corresponding array counter, freqArray[number]++

No switch or if required!

Then for the last part of the question, iterate the array (an enhanced for loop would be excellent for this) and print the result of each element of the array.
• August 1st, 2013, 04:13 PM
syedbhai
Re: Array counting problem
Guys,
Actually the values of array itself are what he needed.
For example, no. of 1's is available in dicecount[0], no. of 2's in dicecount[1] and so on.
Thats it.
You can just print somewhat like this:
System.out.println("count of 1's:"+dicecount[0]);
System.out.println("count of 2's:"+dicecount[1]);
...

Syed.
• August 1st, 2013, 04:15 PM
llowe29
Re: Array counting problem
I still dont understand why you would initialize dice count as an array.
• August 2nd, 2013, 01:40 AM
elizabethyang
Re: Array counting problem
Try this,
...
• August 2nd, 2013, 01:24 PM
copeg
Re: Array counting problem