Storing in an Array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Storing in an Array

  1. #1
    Join Date
    Sep 2004
    Posts
    37

    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]);
    }
    }
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    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.

  3. #3
    Join Date
    Sep 2004
    Posts
    37
    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.

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    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.

    Code:
    Create array of 10 ints
    
    For each customer while customers < 1000, generate random number between 0-9
    
    Increment variable in array
    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.

  5. #5
    Join Date
    Sep 2004
    Posts
    37
    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!

    Thanks again

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    You can use either loop. In fact, a while loop is just a special case of a for loop.

    Code:
    for(; i < 1000;)
    {
        //code goes here
    }
    is exactly the same as
    Code:
    while(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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center