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

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?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Freelance programmer / ConsultantCommented:
++ 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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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
Commented:
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
Commented:
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
Freelance programmer / ConsultantCommented:
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
Commented:
It's the same way that j++ changes the value in j
0
Author Commented:
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
Commented:
That is correct.
0
Freelance programmer / ConsultantCommented:
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
Commented:
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
Freelance programmer / ConsultantCommented:
@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 Commented:
I appreciate everyone's help, especially this early in the morning.
0
Commented:
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
Freelance programmer / ConsultantCommented:
valid but still garbage.

>>especially this early in the morning.
I'm just about to have the midday meal here
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Algorithms

From novice to tech pro — start learning today.