Solved

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

Posted on 2014-11-01
14
201 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
Comment Utility
++ 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
Comment Utility
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
Comment Utility
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

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
It's the same way that j++ changes the value in j
0
 

Author Comment

by:MBarongan
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
That is correct.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
I appreciate everyone's help, especially this early in the morning.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
valid but still garbage.

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now