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

# Thread: Creating an algorithm to do this

1. ## 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:







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
,,,,, 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?  Reply With Quote

3. ## Re: Creating an algorithm to do this

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

4. ## Re: Creating an algorithm to do this Originally Posted by jps 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  appear in columns 0 and 1?" -- 2 times
"How many times does  appear in columns 0 and 1?" -- 1 time
"How many times does  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  appear in columns 0 and 1?" -- 1 time
"How many times does  appear in columns 0 and 1?" -- 0 time
"How many times does  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.  Reply With Quote

5. ## 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.  Reply With Quote

6. ## 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.  Reply With Quote