Solved

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

Posted on 2014-11-01
14
208 Views
Last Modified: 2014-11-01
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]);

         Console.Read();
      }
   }

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
Comment
Question by:MBarongan
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 168 total points
ID: 40416956
++ 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

by:phoffric
phoffric earned 166 total points
ID: 40416963
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

by:ozo
ozo earned 166 total points
ID: 40417003
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 84

Expert Comment

by:ozo
ID: 40417004
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

by:AndyAinscow
ID: 40417015
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());
            Console.Read();

Open in new window

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

by:ozo
ID: 40417019
It's the same way that j++ changes the value in j
0
 

Author Comment

by:MBarongan
ID: 40417035
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

by:ozo
ID: 40417039
That is correct.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40417044
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

by:ozo
ID: 40417048
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

by:AndyAinscow
ID: 40417055
@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

by:MBarongan
ID: 40417065
I appreciate everyone's help, especially this early in the morning.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40417067
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

by:AndyAinscow
ID: 40417076
valid but still garbage.

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

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
About delegates in c sharp 3 50
how to check to see if datatable has headers or not 2 30
HttpPostedFile 1 26
Problem to Alipay 10 24
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question