Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creating an algorithm to do this

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating an algorithm to do this

    I'm struggling to wrap my head around how I could calculate a seemingly simple algorithm and am wondering if anyone can provide some guidance.

    I have a 2D array of type int that stores one digit values. It looks something like this:
    [0][0][0][0]
    [0][1][0][0]
    [1][0][1][1]
    [1][2][0][1]
    [0][0][1][0]
    [1][2][0][0]

    My function takes in two int values which correspond to a column in this 2D array. When given the values 0 and 1, I need to check how many times the combination of
    [0][0],[0][1],[0][2],[1][0],[1][1],[1][2] appears when looking at the 0th and 1st column, and use that to calculate entropy (which is besides the issue right now).

    Anyone have any ideas on how I could achieve this?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Creating an algorithm to do this

    What steps would you take to solve this problem with a pencil?

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an algorithm to do this

    Quote Originally Posted by jps View Post
    What steps would you take to solve this problem with a pencil?
    I've asked myself that many times. This is my train of thought:

    "What's the biggest number we have to search for?" --I have a function that will tell me this so I have this part solved.
    "How many times does [0][0] appear in columns 0 and 1?" -- 2 times
    "How many times does [0][1] appear in columns 0 and 1?" -- 1 time
    "How many times does [0][2] appear in columns 0 and 1?" -- 0 times
    "I've reached the biggest value that can appear in column 1, so move on to next part...
    "How many times does [1][0] appear in columns 0 and 1?" -- 1 time
    "How many times does [1][1] appear in columns 0 and 1?" -- 0 time
    "How many times does [1][2] appear in columns 0 and 1?" -- 2 times

    I see the pattern of:
    [i][j] where j is a loop nested within i but I just don't know how I should do it in my program...I've already tried using counters within multiple sets of loops but that proved to be real messy and I think it can be done better.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Creating an algorithm to do this

    This is a trick I used to get my head around nesting loops. Visualize the grid in terms of i,j,(k etc). Once I know how I am reading the array (map) it is easier to understand how to compare elements to achieve the goals.

    [i=0,j=0] [i=1,j=0] [i=2,j=0]
    [i=0,j=1] [i=1,j=1] [i=2,j=1]
    [i=0,j=2] [i=1,j=2] [i=2,j=2]
    [i=0,j=3] [i=1,j=3] [i=2,j=3]
    [i=0,j=4] [i=1,j=4] [i=2,j=4]
    [i=0,j=5] [i=1,j=5] [i=2,j=5]
    (and for the next loop this entire grid is grid#k=0 and so on)

    Now you can see the order in which the elements are accessed, and understanding this mapping is important to using it as a data structure. Be careful which order i and j are used, it matters. The top row of the grid could be i=0 from left to right instead of the current design where i=0 top to bottom in the first column. This is determined by which loop is inner/outer and which order you use i and j in the array. myArray[i][j] or myArray[j][i]. Unfortunately there is no standard on this and I see code in every possible variation.

  5. #5
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Creating an algorithm to do this

    How about searching for some code on Google that does this kind of loop? Maybe "nested loops" or "looping over 2D array" might help. Even if it's code in C, C++, C# or some other language you might be able to get the idea.

Similar Threads

  1. having problem on creating this shortest job first nonpreemptive algorithm
    By morphling18 in forum Java Theory & Questions
    Replies: 10
    Last Post: September 29th, 2012, 12:57 PM
  2. MWM Algorithm
    By andreas90 in forum Algorithms & Recursion
    Replies: 5
    Last Post: January 11th, 2012, 02:43 AM
  3. CRC algorithm
    By aldykid in forum Algorithms & Recursion
    Replies: 1
    Last Post: August 7th, 2011, 10:50 AM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. all i need is algorithm
    By coder.freak in forum Paid Java Projects
    Replies: 3
    Last Post: April 6th, 2011, 11:11 AM