|
-
Storing in an Array
Hi everyone.
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[0] = 23; (23 people going through this turnstile
for example)
counter[1] = 56;
counter[2] = 89;
and so on up to
counter[9] = 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:
class AttendenceCounts3
{
public static void main(String[] args)
{
int[]counter1 = new int[10];
int[]counter2;
counter2=counter1;
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);
counter2[patron]++;
System.out.println("counter [" + i + "] = " +counter2[+ patron]);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks