Solved

# Can't figure out how an integer array is being populated

Posted on 2014-11-01
201 Views
Below is a program that simulates rolling a pair of dice 100 times.

class Program
{
static void Main(string[] args)
{
int[] Scores = new int[13];
int dice = 0;
System.Random rnd = new System.Random();
for (int j = 0; j < 100; j++)
{
dice = rnd.Next(1, 7) + rnd.Next(1, 7);
Scores[dice]++;
}
for (int j = 0; j < 13; j++)
Console.WriteLine(Scores[j]);

}
}

The output is different each time. The last time I ran the program I got this to print in the console window:
0
0
2
6
7
12
10
6
15
15
7
2

My question is how did the Scores array get populated with these numbers? In the first for loop, there isn't an assignment operator that's storing values in the array. For example, I'm used to seeing a for loop like this:

for (int j = 0; j < Scores.Length; j++)
{
dice = rnd.Next(1, 7) + rnd.Next(1, 7);
Scores[j] = dice;
}

The Scores[j] = dice is assigning the random values of dice to the array. But I don't understand this loop:

for (int j = 0; j < 100; j++)
{
dice = rnd.Next(1, 7) + rnd.Next(1, 7);
Scores[dice]++;
}

How is Scored[dice]++ assigning values to the array?
0
Question by:MBarongan
• 6
• 5
• 2
• +1

LVL 44

Accepted Solution

AndyAinscow earned 168 total points
++ is an operator that is short for add one to the current value.

so Scores[dice]++ is adding 1 to the contents of Scores[dice] each time it is run.
0

LVL 32

Assisted Solution

phoffric earned 166 total points
In your bottom example, dice is the score value. In the top example, dice is the index to score array.
But there is something awry. The first two values in your array are 0, and that is the way the top example is programmed - probably unintentionally. (Also, you didn't list the entire array, but I am guessing that you just left out the actual results.

Random.Next Method (Int32, Int32)
http://msdn.microsoft.com/en-us/library/2dx6wyd4(v=vs.110).aspx
Return Value:
A 32-bit signed integer greater than or equal to minValue and less than maxValue

Each rnd.Next can return a min value of 1, so dice will be at a minium = 1+1 = 2; but what about indexes, 0 and 1?
0

LVL 84

Assisted Solution

ozo earned 166 total points
The Scores count at indexes, 0 and 1 will never be incremented, because two standard dice can never land with a total of 0 or 1 pips facing up.
(or even if there's some bizarre way they can, it won't be simulated with rnd.Next(1, 7) + rnd.Next(1, 7))
0

LVL 84

Expert Comment

But something does seem awry, because there should have been exactly 100 increments to values in Scores, and you only have a total of 82, which means that Scores[12] must have been 18
18 rolls of 12 in 100 tries seems pretty unlikely (although possible) so there may be reason to suspect the fairness of your random number generator.
0

LVL 44

Expert Comment

hmmm:
I made a minor change - include total in printout
``````            int[] Scores = new int[13];
int dice = 0;
System.Random rnd = new System.Random();
for (int j = 0; j < 100; j++)
{
dice = rnd.Next(1, 7) + rnd.Next(1, 7);
Scores[dice]++;
}
int total = 0;
for (int j = 0; j < 13; j++)
{
Console.WriteLine(Scores[j]);
total += Scores[j];
}
Console.WriteLine("Total " + total.ToString());
``````
It always shows a total of 100 - I suspect the example results in the question aren't the real values.

Anyway back to your question.  Do you understand the increment operation that is occurring inside the loop (the first comment) to assign values into the array?
0

LVL 84

Expert Comment

It's the same way that j++ changes the value in j
0

Author Comment

I think I understand now. The reason I was confused was that I thought int[] Scores = new int[13] created an empty array. I didn't know there were '0' values placed in the array by default. I thought it just had 13 empty slots. That's why Scores[dice]++ confused me because how can you increment something with no value? It's like trying increment null, as in null++. But now that I know an array starts out with all slots filled with 0's I understand the logic.  So in this program, if each roll totaled 12, then dice would equal 12 100 times. So Scores[12]++ means you're incrementing the last element in the array 100 times. So the last element would end up being 100 and the elements from Scores[0] to Scores[11] would remain at 0. Is that correct?

By the way, I ran the program again and got these results this time:
0
0
2
3
8
6
13
20
20
9
11
6
2
0

LVL 84

Expert Comment

That is correct.
0

LVL 44

Expert Comment

Yes.  (.Net fills default values such as zero in this case, some languages would leave an empty array) and yes, 100 times 12 would give 100 in that slot and zero in all the others.
0

LVL 84

Expert Comment

You declared an int array, and empty is not an int.
If it had been empty, then Scores[j] = dice; would not have been valid either, as there would have been no Scores[j] to put it in.
0

LVL 44

Expert Comment

@ozo
C++ would give an int array BUT the contents of the individual array cells would not necessarily be 0, they would be whatever was in that memory before.  So one could get the following as a result after 100 increments:
965
6384
12
6547
...

in other words - garbage
0

Author Closing Comment

I appreciate everyone's help, especially this early in the morning.
0

LVL 84

Expert Comment

965
6384
12
6547
are all valid int values
If the array were empty, trying to get a value from it would be an invalid operation, and the behaviour would be undefined.
0

LVL 44

Expert Comment

valid but still garbage.

>>especially this early in the morning.
I'm just about to have the midday meal here
0

## Join & Write a Comment Already a member? Login.

This article will show, step by step, how to integrate R code into a R Sweave document