# Searching Multiple Arrays?

• 03-19-2009
Cell
Searching Multiple Arrays?
I'm looking for the best way to search through multiple arrays.

For example, I have 4 arrays: A1[8], A2[8], A3[8], and A4[8].

I want to search through all 4 arrays only for the first 4 positions to see if there is a '1'. If so, I'll store the number of occurrences of 1 in a counter variable.

I also want to search the last 4 positions for a 1 also, and add the number of 1 occurrences to another counter variable.

Now, I think I have this down pat. However, I have to do this for every single array and it gets ugly. For my post I used only 4 arrays, but I have 10 in my code. It wastes so much space.

What I'm essentially doing is:

Code:

```                for (int i = 0; i <= 3; i++){   for (int j = 4; j <= 8; j++){     if (A1[i] = 1)       first_grp = first_count + 1;     if (A1[j] = 1)       sec_grp = second_count + 1;   }                                    }```
So if A1[8] = {0, 1, 1, 0, 0, 0, 0, 1}, the result will be:

first_grp = 2
sec_grp = 1

That is for just ONE array. I have 10 of those code blocks in my code right now.

Is there a way to shorten this?

Thanks.
• 03-19-2009
Nightowl
Only way is to use an array like A[4][8]. (Only way I know of, anyhow.)

I don't see what would be wrong with an array like that instead of A1[8], A2[8], A3[8], and A4[8] . . . perhaps I'm missing something?
• 03-19-2009
Cell
Quote:

Originally Posted by Nightowl
Only way is to use an array like A[4][8]. (Only way I know of, anyhow.)

I don't see what would be wrong with an array like that instead of A1[8], A2[8], A3[8], and A4[8] . . . perhaps I'm missing something?

Trust me - I'm new to this, so if someone is missing something, it's most likely me.

I do see what you're saying about using a A[4][8] array. However, I'm not exactly sure how I'd search that.

Would I search it row by row, going over each column in a row before moving to the next row? I.E. - I'd count 8 times for each column, then update my row-offset counter to move to the next row?

That looks like a much more elegant solution... :)
• 03-19-2009
Nightowl
What I meant, was rather than something like
Code:

```search A1 search A2 search A3 search A4```
Rather, something more like

Code:

```for(x = 0; x < 4; x ++) {     search A[x] }```
. . . in pseudocode, of course. ;)