Re: Counting and Math.random

Where does the code assign any values to the elements in the digitArray array?

There needs to be a loop that indexes through the elements of the array and assigns a number to each element.

Re: Counting and Math.random

how would I do that exactly with 100 different arrays? The programs I've seen only do something like

Code Java:

array[0] = 0
array[1] = 2
array[2] = 5
//etc.

Re: Counting and Math.random

Instead of the numbers: 0,1,2 as the index, use the for loop control variable as the index.

Re: Counting and Math.random

Okay, so I put this in the array loop, not sure if it matters, but now the numbers change, so instead of 0 it would be 3 or 7, but they're all that same number.

Code Java:

for (arrayCount = 1; arrayCount <= digitArray.length; arrayCount++) {
System.out.print(arrayCount + ": ");
System.out.print((digitArray[arrayCount] = numbers) + " | ");
}

But then it also says at the end.

The numbers are just there to tell me how many there are.

Re: Counting and Math.random

If you want random numbers assigned to the array, you need to use the Random class to create each number that is assigned to an element of the array. Like is assigned to the variable: numbers

Quote:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

The index to the array went past the end. Remember that the indexes for an array range in value from 0 to the length of the array minus 1. For a 10 element array, the max index would be 9.

Check that the for loop index does not get to be equal to the length of the array.

Re: Counting and Math.random

I fix the index problem, but for the random numbers, I have to use the Math.random(); so I'm confused on getting that to work properly. because I have the

Code Java:

int numbers = (int)(Math.random() * 10);
&&
System.out.print((digitArray[arrayCount] = numbers) + " | ");

so I don't see exactly what I could be doing wrong because technically I'm assigning values to every element by using the second code right?

Re: Counting and Math.random

If the value of numbers doesn't change then the same number is assigned to all the elements.

Say numbers is assigned a 7, then all the array elements will be assigned a value of 7.

The Math.random() method returns a different number every time it is called. Use that value like it was used with numbers.

Re: Counting and Math.random

Oh, I figured out what you were saying now. Thanks! But now, I'm confused about the counting each integer from that one array.. Some people say to make an array of 10 (0-9) integers then count them that way or to do something way different that confuses me as well.. :/

Re: Counting and Math.random

Use the random number as the index into the array. Increment the indexed element by 1 to count the random number.

Re: Counting and Math.random

Sorry, I don't think I'm understanding well enough. Although I tried to increment each element, it's just the same number for all of them which is what happened before. This was my code

Code Java:

digitArray[0] = counts++;
digitArray[1] = counts++;
digitArray[2] = counts++;
digitArray[3] = counts++;
digitArray[4] = counts++;
digitArray[5] = counts++;
digitArray[6] = counts++;
digitArray[7] = counts++;
digitArray[8] = counts++;
digitArray[9] = counts++;

Re: Counting and Math.random

Treat the element of an int array as an int variable:

Code :

int[] intArray = new int[10]; // define an array
int intVar = 0; // define a variable
intArray[idx]++; // increments the value of intArray[idx]
intVar++; // increments the value of intVar

Use the random number as the index into the array

Re: Counting and Math.random

Okay, so I have no idea what you mean by that to be honest. This is what I have written down.

Code Java:

for (arrayCount = 1; arrayCount < digitArray.length; arrayCount++) {
System.out.print(arrayCount + ": ");
System.out.print((digitArray[arrayCount] = (int)(Math.random() * 10)) + " | ");
}
//Count different integers - new array
int[] countVarArray = new int[11];
int countVar = 0;
for (countVarArray[10]++; arrayCount < digitArray.length; countVar++) {
System.out.println();
}
System.out.println("\n" + countVar);
}

with this as a result
Code Java:

1: 5 | 2: 2 | 3: 6 | 4: 2 | 5: 0 | .... | 95: 9 | 96: 9 | 97: 7 | 98: 2 | 99: 9 | 100: 3 |
0

with the 0 being the count of each integer.. So, I put what you said pretty much, or tried to.

Re: Counting and Math.random

Get rid of that code and start over.

Make a loop that goes around 100 times: "generate random 100 numbers"

inside that loop generate a random number: "from 0 to 9"

Use that random number to index into an array and select an element

Increment that element by 1 to count the number of random numbers generated with that value.

Re: Counting and Math.random

So, do you mean to put another loop inside of the first loop? or put it all in one loop? Sorry I don't really get this.. This is what I put.

Code Java:

int[] digitArray = new int[101];
int[] counts = new int[10];
//Generate 100 random numbers
for (int arrayCount = 0; arrayCount < digitArray.length; arrayCount++) {
int randNum = (int)(Math.random() * 10);
System.out.print((digitArray[arrayCount] = randNum) + " ");
//count
for (randNum = 0; randNum < counts.length; randNum++) {
System.out.print(randNum);
}
}

Re: Counting and Math.random

I meant there to be a single loop for counting the random numbers.

There could be another loop to print out the results AFTER the counting was done.

The code defines an array to hold the counts but doesn't use it.

Does the assignment say that the values of all the random numbers need to be saved?

Or does it ask for a count of the number of times specific numbers occurred?

You didn't follow my instructions. Where is the code to do this:

Use that random number to index into an array and select an element

Increment that element by 1

That code would be like this: theCountingArray[theRandomNumber]++;

Re: Counting and Math.random

The assignment asks that for the program to count the number of times each distinct numbers occurs. So, it wants to show how many 0s, how many 1s, etc.

Re: Counting and Math.random

Ok, that is what I thought. There are supposed to be 100 randomly chosen values in the range 0 to 9.

Did you rewrite the code?

Re: Counting and Math.random

Yep, not sure if this is totally what you meant, but this is what I got from it. I tried to print it by putting System.out.println(counts); but that only gave me '[I@1b845568' and I have no idea what that code is. This is what I have so far.

Code Java:

int[] digitArray = new int[101];
int[] counts = new int[10];
int arrayCount = 0;
//Generate 100 random numbers
for (arrayCount = 1; arrayCount < digitArray.length; arrayCount++) {
int randNum = (int)(Math.random() * 10);
System.out.print(arrayCount + ": ");
System.out.print((digitArray[arrayCount] = randNum) + " | ");
}
//count
while (arrayCount < digitArray.length) {
counts[0]++;
counts[1]++;
counts[2]++;
counts[3]++;
counts[4]++;
counts[5]++;
counts[6]++;
counts[7]++;
counts[8]++;
counts[9]++;
}
System.out.println(counts[0]);

Re: Counting and Math.random

Can you do this a few steps at a time. There is a lot of code in you post that don't make sense.

Start with these first steps:

1) define an array with 10 slots to hold the counts

2) define a loop that goes around 20 times (later it will be changed to 100)

3) inside the loop, write code to generate a random number from 0 to 9

4) for testing> print out the value of the random number

AND Nothing more

Re: Counting and Math.random

Code Java:

//array
int[] counts = new int[10];
//random number loop
for(int r = 0; r < 20; r++) {
int randNum = (int)(Math.random() * 10);
System.out.print(randNum + " ");
}

output:

Code Java:

8 8 4 3 1 8 4 4 3 2 1 8 9 8 6 3 0 8 1 3

Re: Counting and Math.random

now add ONE line of code that uses the random number as an index to the counts array to access one of its elemens.

Increment that element by one.

Re: Counting and Math.random

Code Java:

//array
int[] counts = new int[10];
//random number loop
for(int r = 0; r < 20; r++) {
int randNum = (int)(Math.random() * 10);
System.out.print(randNum + " ");
counts[0]++;
}

output:

Code Java:

8 8 4 3 1 8 4 4 3 2 1 8 9 8 6 3 0 8 1 3

Re: Counting and Math.random

Why does the code use the value 0 for the index into the array?

I said:

" uses the random number as an index to the counts array "

Re: Counting and Math.random

So like this? Since I can't control what numbers show up?

Code Java:

//array
int[] counts = new int[10];
//random number loop
for(int r = 0; r < 20; r++) {
int randNum = (int)(Math.random() * 10);
System.out.print(randNum + " ");
counts[randNum]++;
}