Storing in an Array
I am writing this program that uses a global int array to simulate turnstile counts. Each element of the array represents the count on a turnstile. There are 10 turnstiles and I must run a program that has 1000 people passing through these 10 turnstiles randomly.
THe output must be like this:
counter = 23; (23 people going through this turnstile
counter = 56;
counter = 89;
and so on up to
counter = 34;
In my program I can't manage to place the randomly created numbers to only be placed in the 10 different elements. It seems that when I do a loop to run 1000times it prints out the individual counters 1000 times each and then has values beside them. How do I just simulate how many times a counter has been randomly "walked through" and then add that just to the value inside of that element.
I also need to create a function that returns the sum and average of the 10 array elements and then display these to screen as well.
Here is my code and thanks for your time:
public static void main(String args)
intcounter1 = new int;
for(int i = 0;i<counter2.length;i++)
counter2[i] = 0;
for (int i=0;i<counter1.length;i++)
for (int j = 0; j < 20; j++)
int patron = (int)(Math.random() * 10);
System.out.println("counter [" + i + "] = " +counter2[+ patron]);
Well firstly, why do you have variables named counter1 and counter 2? What do you want these 2 variables to do? I have a feeling that they're not doing what you want them to do.
You have them both pointing at the same array, so whatever you do to counter1 will also happen to counter2. They aren't seperate.
Also, in your for loop you only loop through 20 times. I thought you were meant to do it 1000 times?
One final thing, you don't want the system.out.println(); statement inside the loop. This will make it print out once each time the loop itterates. You need the System.out.println statements at the end.
Hi Mike, thanks for your reply. I thought I would use the two counters as I thought I would need one to point at the array while the values in the element are changed. I am a bit confused but I think I can see why this doesn't work now. I have only iterated the loop 20 times as I was just testing it out.
I am still stuck as to how I add the random value (1-10) to it's appropiate address in the array. Do I only need to use the one counter and then do I use the random function I already have? Is this correct? Also if you have any info. on how write a function that returns the sum and average of the 10 array elements that would be good.
Thanks for your time and help Mike, I know I have posted a few queries on here but I really want to get ahead in Java before the subject really kicks in. Your help has been wonderful.
Well the first thing to remember is that the array doesn't run from 1-10, it runs from 0-9 (still 10 array elements though). This means that you have to generate a random number between 0 and 9 as you have done. When you have the random number between 0-9, you increment the integer at that element in the array. What you have there looks like it should work.
You don't need the two pointers. They are both attached to the same array. I'm not sure exactly what you thought it was doing having both of them, but it certainly isn't necessary. I think you're trying to make things a little too complicated :)
What i'll do now is write in pseudocode what you need to be doing for the turnstyle. You seem to have the right idea, but this will just clarify it.
As for summing the numbers in the array, you need to use a for loop. You'll have a variable that you use to count the numbers, then for each element in turn you'll add the integer contained there to the variable. At the end you'll have an integer representing the sum of all the numbers. To get the average, simply divide by the number of turnstyles. Bear in mind that you'll need to divide by a double and not an int, otherwise you won't get the real average.
Create array of 10 ints
For each customer while customers < 1000, generate random number between 0-9
Increment variable in array
Thanks Mike, I will give it a go. But do I use a for loop for counting the turnstiles or a while statement?Am not too sure how to increment the array but I will give it a shot.
And trust me in a lot of the other programs I have been doing I DO have a habit of complicating things!
You can use either loop. In fact, a while loop is just a special case of a for loop.
is exactly the same as
for(; i < 1000;)
//code goes here
In this case it would probably be easier to use a for loop. You would initialise an in to 0, have the stopping condition as i < 1000, and after each increment add one to i.
while(i < 1000)
//code goes here