noob programmer
noob programmer

Reputation: 35

The increment in my Java array

Can someone explain what does ++freq in my given program below?

package array;

import java.util.Random;

public class CounterArray {
     public static void main(String[] args){
         Random Ran = new Random();
         int freq[] = new int [7];

         for(int roll=1;roll<100;roll++){
             ++freq[1+Ran.nextInt(6)];
         }
          System.out.println("face\tFrequency");
         for(int face=1;face<freq.length;face++){
             System.out.println(face+"\t"+freq[face]);
         }
     }
}

Upvotes: 0

Views: 492

Answers (5)

5gon12eder
5gon12eder

Reputation: 25409

What the program does is “rolling a dice” 99 times (which smells like an “off by one” error) and count how often each number was found. The array freq holds at position i how often i was diced.

The code in question generates the next random number and then increments the respective slot in the array.

++freq[1 + Ran.nextInt(6)];

It will become more clear if we break it apart.

  • First, Ran.nextInt(6) is evaluated which yields a random integer from the set {0, 1, 2, 3, 4, 5}. To this, 1 is added to get a number from the set {1, 2, 3, 4, 5, 6}. Let's store this intermediate result away in a separate variable for clearness: int result = Ran.nextInt(6) + 1.
  • Next, the respective frequency is looked up in the array and incremented by one. ++freq[result] which has the same effect as freq[result] += 1.

The length of the array freq was made to be 7 so the “natural” index can be used. That is, the element freq[0] is wasted. (I doubt that this is very good style.)

Another word on style: It is generally accepted practice to reserve capital names for types and not use them for variables, so Ran should really be named ran to avoid confusion.

One further addition: The difference between the post-increment operator as in i++ and the pre-increment operator as in ++i is that the result of the former expression is the value of i before the increment while that of the latter is the value after the increment. In your case, it makes no difference because you are not using the result anyway. To see how this is working, try running the following code:

int i = 10;
System.out.printf("i   = %d%n", i);
System.out.printf("i++ = %d%n", i++);
System.out.printf("i   = %d%n", i);
System.out.printf("++i = %d%n", ++i);
System.out.printf("i   = %d%n", i);

Upvotes: 5

MC Emperor
MC Emperor

Reputation: 22977

You need to know something about some elements used:

  • The ++ operator increments the preceding or following variable.
  • The method Random.nextInt(int) returns an integer.
  • You have multiple operations on this line: ++freq[1+Ran.nextInt(6)].

What happens is this:

  1. Random.nextInt(6) returns any integer between 0 and 5, say 4.
  2. With this snippet: 1 + Ran.nextInt(6), you increment 4 with 1, which results in 5.
  3. So you're calling index 5 of the freq integer array.
  4. Since you have only initialized the size of the array, and not set any values, all values default to 0a.
  5. freq[5] returns 0, because the value of position 5 of the freq array is 0a.

If we rewrite the snippet ++freq[1+Ran.nextInt(6)], it will look like this:

int diceResult = Ran.nextInt(6); // Returns, say, 4
int index = 1 + diceResult; // We increment dice result by 1
freq[5] = freq[5] + 1;

a Of course, when the loop is repeated, these values might be greater than 0.

Upvotes: 1

Zanon
Zanon

Reputation: 30710

It is the increment operator and the behavior will be like the following:

int temp = 1+Ran.nextInt(6);
freq[temp] = freq[temp] + 1;

The part Ran.nextInt(6) will generate a number between 0 and 5. So 1+Ran.nextInt(6) will return a number between 1 and 6. The code will use this number as the index of the array to find which element should be increased by 1.

Upvotes: 1

Todd
Todd

Reputation: 31660

It increments by one a random element from the array freq.

 ++freq[1+Ran.nextInt(6)];

The Ran.nextInt(6) part gets a random integer between 0 and 5. The 1+ part adds one to the random number. So if the random number is say, 3, you effectively have ++freq[4] (after addition). Since freq[4] is just an integer reference, you can increment it. By using the prefix ++, you are saying "add one to whatever number comes after me, and set/return it".

So what you are doing is finding a random element in the array and incrementing it by 1.

Upvotes: 2

ale64bit
ale64bit

Reputation: 6242

It simply increments by one the value at the position of the array obtained by 1+Ran.nextInt(6). In a more "descriptive" way, you can interpret it as simulating that the dice face you obtained was 1+Ran.nextInt(6), and so, the frequency of that face augments by one.

Upvotes: 1

Related Questions