# Help!

• November 11th, 2012, 05:52 PM
erer2116
Help!
Hi I'm barely learning how to write java code and I need help with an assignment. The assignment consists of making an array that creates fifty random integers between zero and nine and displays the count to each number. Up to now I only know how to create the random number generator BUT I dont know how to display the count to each number(for example if the output was twenty zeros and maybe five 4's etc. from the fifty random numbers from zero to nine). This is what I have up to now:
int i= 0;
for{i=0; i<50; i++)
{
double x= Math.random();
int random = (int)(x*9);
System.out.println(random);
}
Thanks I really appreciate all help.
• November 11th, 2012, 11:25 PM
Zaphod_b
Re: Help!
I'm not sure why you need an array of 50 integers, but here's the drill:

Create an array of 50 ints. Suppose the name of the array is iArray.

Then the loop is executed for i = 0, 1, ..., 49, so each time through the loop you can store the random number in iArray[i].

Now, supposing that the random numbers that you generate have values 0, 1, ..., 9, to be able to tell the number of occurrences of each of the digits 0, 1, ..., 9, you need an array of ten ints. Suppose the name of the array is counts.

As you create the random digits 0, 1, ... , 9 in the loop you want to do the following:

If the random digit is 0, increment counts[0].

If the random digit is 1, increment counts[1].

.
.
.

If the random digit is 9, increment counts[9].

Now you don't need ten if statements to tally the counts; If you think about it, you should see that you can simply do the following each time through the loop:
Code java:

` ++counts[random]; // Assumes that random has value 0, 1, ... , 9`

Then, after all of the numbers have been generated, make a loop that shows the values of all of the counts:
Code java:

``` for (int i = 0; i < 10; i++) { System.out.println("counts[" + i + "] = " + counts[i]); }```

Or some such thing.

Cheers!

Z
• November 12th, 2012, 06:12 PM
erer2116
Re: Help!
Thank you! This really helped