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?

Re: Creating an algorithm to do this

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

Re: Creating an algorithm to do this

Quote:

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 [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.

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.

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.