[RESOLVED] Sum Frequency Trouble - Page 2-VBForums
Page 2 of 2 FirstFirst 12
Results 41 to 43 of 43

Thread: [RESOLVED] Sum Frequency Trouble

  1. #41
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    685

    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:
    1. int[] frequencies = new int[13]; //frequencies[0] is the frequency of the sum 2, frequencies[10] is for sum 12

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

    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:
    1. Freqbox.Items.Add(String.Format("Frequency of sum {0} = {1}", i + offset, frequencies[i]));

    C# Code:
    1. 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:
    1. for (int i = 2; i < 13; i++)

    Your comment here is not accurate for what you're doing:
    C# Code:
    1. //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:
    1. int[] x = new int[11];
    2. for (int i = 0; i < x.Length; i++)
    3. {
    4.     //Note: With Console.WriteLine() we can use string formatting the same way we use it in String.Format() without having to use String.Format()
    5.     Console.WriteLine("Sums of {0}", i + 2);
    6. }

    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:
    1. for (int i = 2; i < 13; i++)

    Do this:
    C# Code:
    1. 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:
    1. private Random randomNumber = new Random();
    2. private void RollDicebtn_Click(object sender, EventArgs e)
    3. {
    4. }
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    VB.NET Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  2. #42
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    685

    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:
    1. //Note: Dictionaries are structured such that: Dictionary<KEY, VALUE>
    2. Dictionary<int, int> dict = new Dictionary<int, int>();
    3. int key0 = 0;
    4. dict.Add(key0, 10);
    5. dict.Add(key0, 10); // Adding the same Key to the Dictionary results in a runtime exception.


    C# Code:
    1. // If a key already exists within the dictionary, then we can't add it again, BUT...
    2. // We can change the value associated with that Key in the Dictionary.
    3. // This means we need to check if the Key already exists though.
    4.  
    5. Dictionary<int, int> dict1 = new Dictionary<int, int>();
    6. // Our key will be a value of int, which is 0.
    7. int key = 0;
    8. // 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.
    9. dict.Add(key, 10);
    10.  
    11. Console.WriteLine("First Value: {0}", dict[key]);
    12.  
    13. // 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
    14. // within the dictionary...
    15. if (dict.ContainsKey(key))
    16. {
    17.     // We index a value in a Dictionary based on it's Key in this case.
    18.     // Here we change the value in the Key and Value pair within the dictionary, for where the key is 0.
    19.     // (And assign it a value of 50, from it's previous value, which was 10)
    20.     dict[key] = 50;
    21. }
    22.  
    23. 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:
    1. myDictionary["stanly"]

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

    C# Code:
    1. 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:
    1. 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:
    1. if (myDictionary.ContainsKey(7))
    2. {
    3.     // Do something with myDictionary[7];
    4. }
    Last edited by AceInfinity; Feb 5th, 2013 at 10:52 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    VB.NET Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #43

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    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.

Page 2 of 2 FirstFirst 12

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

Survey posted by VBForums.