# Thread: [RESOLVED] Sum Frequency Trouble

1. ## Re: Sum Frequency Trouble

Before I mark this thread as resolved, is there anything that I need to do to give you a rating or credit or anything to inform that you provided excellent assistance?
Not really... I just provide my help on forums because I enjoy doing it. People like to know their help is appreciated though, that's the main thing.

It seems this could be my last post here though as it looks like you've got things working, but I have a few final things to point out to you just for your benefit in the current code you have. Other than this, good job You got through it on your own, and I like to see when people can do that with the bit of guidance I can provide.

Basically for String.Format(), other than the extra formatting you can apply, if you're just wanting to place arguments in their places within a string, you can think of it almost like the way an indexing system works inside an array. {0} will be a placeholder for the first param, and so on... The syntax is like this, as you've figured out: String.Format("My string to be parsed in a certain format", argument 1, argument 2, etc...), where argument 1 would be indexed with "{0} inside the string, argument 2 would be {1}, etc...

I understand what you're trying to do here:
C# Code:
`int[] frequencies = new int[13]; //frequencies[0] is the frequency of the sum 2, frequencies[10] is for sum 12`

C# Code:
`for (int i = 2; i < 13; i++){    Freqbox.Items.Add(String.Format("Frequency of sum {0} = {1}", i + offset, frequencies[i]));}`

But respectively, you're not using 2 indexes within this array here, and you initialize it to a larger size than it needs to be You aren't using a Dictionary here, perhaps because of a lack of understanding still, but IF you were to use an array, what you could do is give it a size of only the number of elements you need and use the index + 2 to start from 0, but display that it's the sum of # of times 2 showed up.

Look through the code you have though, specifically your offset variable. It's not doing anything because it's always 0.

C# Code:
`Freqbox.Items.Add(String.Format("Frequency of sum {0} = {1}", i + offset, frequencies[i]));`

C# Code:
`frequencies[sum - offset]++;`

1. i + 0
2. frequencies[sum - 0]

It doesn't do anything here It can be removed.

Based on this loop:
C# Code:
`for (int i = 2; i < 13; i++)`

Your comment here is not accurate for what you're doing:
C# Code:
`//frequencies[0] is the frequency of the sum 2, frequencies[10] is for sum 12`

It looks like you're just skipping index[0] and so you're starting 2 indexes higher because you want to use the index to display that you're starting from a sum of 2, since with 2 dice, you can't get a sum of 1 or 0. It's not possible. What that loop does is it iterates from i being 2, all the way up to 1 less than 13, which is 12.

The way you're doing it, is declaring the integer as a size of 13 elements, and you're only using indexes 2 to 12 within the array, and you've kept indexes 0, and 1, unused.

You could set your array to 11. Take a look at my example code so I can show you:
C# Code:
`int[] x = new int[11];for (int i = 0; i < x.Length; i++){    //Note: With Console.WriteLine() we can use string formatting the same way we use it in String.Format() without having to use String.Format()    Console.WriteLine("Sums of {0}", i + 2);}`

This way we have all of the placeholders in our array being used, nothing is wasted. My advice to you would be to NOT use hardcoded sizes in your for loops though when you're using it to loop through an array, because if at a later date you increase the size of the array, you have to manually change the definition within the for loop itself. So use the Length property for the array size like shown in my example above ^^.

What you're doing is this:
C# Code:
`for (int i = 2; i < 13; i++)`

Do this:
C# Code:
`for (int i = 2; i < frequencies.Length; i++)`

Also, for reasons of how the Random class works, you should keep it outside of your method when declaring an instance of it. Don't put it within the RollDicebtn_Click void, keep it outside and reference the same instance, without declaring a new instance everytime that void method is called:
C# Code:
`private Random randomNumber = new Random();private void RollDicebtn_Click(object sender, EventArgs e){}`

2. ## Re: Sum Frequency Trouble

As for the Dictionary<TKey, TValue>, I've provided some examples that you can try out within a method. Button click, your own method called from a button click, or within a console application, it doesn't matter. But after running these codes, you should see a little about how a dictionary works.

C# Code:
`//Note: Dictionaries are structured such that: Dictionary<KEY, VALUE>Dictionary<int, int> dict = new Dictionary<int, int>();int key0 = 0;dict.Add(key0, 10);dict.Add(key0, 10); // Adding the same Key to the Dictionary results in a runtime exception.`

C# Code:
`// If a key already exists within the dictionary, then we can't add it again, BUT...// We can change the value associated with that Key in the Dictionary.// This means we need to check if the Key already exists though. Dictionary<int, int> dict1 = new Dictionary<int, int>();// Our key will be a value of int, which is 0.int key = 0;// Add a new key to the dictionary, with a value of 0, and give it's "Value" a value of 10 to start off with.dict.Add(key, 10); Console.WriteLine("First Value: {0}", dict[key]); // Check of the dictionary already contains the key 0, because we can't add 0 as a key if it already exists as a key// within the dictionary...if (dict.ContainsKey(key)){    // We index a value in a Dictionary based on it's Key in this case.    // Here we change the value in the Key and Value pair within the dictionary, for where the key is 0.    // (And assign it a value of 50, from it's previous value, which was 10)    dict[key] = 50;} Console.WriteLine("New Value: {0}", dict[key]);`

However impractical these exact codes may be, it's simply written to help you understand how a Dictionary works.

With every value within a dictionary, you have a Key. Think of it as a bunch of tote boxes for people's mail, or security boxes at the bank. Every key only works for one tote box, or at least that's they way it should be. Same thing with a dictionary. Every Key, grants access to a value. Hence why it's really a collection of KeyValuePair's, because each Key and Value is a pair, for every element within a Dictionary.

You need the right key to access the right value. Yet, a Key and Value can be of any type you want. For a Dictionary<TKey, TValue> you can have:

Dictionary<string, int>: Key is a type of string to access a value of type int.
Dictionary<string, List<int>>: Key is a type of string to access a value of type List<int> (which is a collection of Integer values).
Dictionary<int, string>: Key is a type of int to access a value of type string.
etc...

The reason why we can't have 2 elements in a Dictionary with the same key is because if we did... Well... Think of the security box analogy I made. If someone gave you a key, and you knew it opened 2 different security boxes, how would you know which one you wanted to open out of the 2 to retrieve the right contents you wanted to get out of the box? One could be the wrong box, and one could be the right box. How would a program decide this? Therefore, we can only have one key to access a specific value in a Dictionary.

If the key is a type of string, you would access it's value in the same way you index an integer, but the "indexing"-style syntax, would have to reflect the typeof key you're looking for. So if it's a string, and the key, which is a type of string, was "stanly", then you would access the value for that key in this way:

C# Code:
`myDictionary["stanly"]`

If in the same Dictionary, you wanted to retrieve the value from the key which is "melissa", same thing:

C# Code:
`myDictionary["melissa"]`

But if the key for the dictionary was defined as an integer, ie. you defined the dictionary in this kind of way: new Dictionary<int ... ... You would access values by providing an integer as the key:

C# Code:
`myDictionary[7]`

But the key you're referencing needs to exist within the Dictionary, so you may want to provide a "check" first...

C# Code:
`if (myDictionary.ContainsKey(7)){    // Do something with myDictionary[7];}`

3. ## Re: Sum Frequency Trouble

Thanks you Ace for the advice and information. I will definitely study this and practice everything you have giving me. I am moving on to the next project so hopefully I will be able to get a better understanding and use the information to help me with the new project. Thanks again, you have been a great asset as well as a great person to learn from.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured

Click Here to Expand Forum to Full Width

×
By using this site, you agree to the Privacy Policy