Solved

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

Posted on 2014-11-01
14
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 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