Searching Multiple Arrays?

This is a discussion on Searching Multiple Arrays? within the C Programming forums, part of the General Programming Boards category; I'm looking for the best way to search through multiple arrays. For example, I have 4 arrays: A1[8], A2[8], A3[8], ...

  1. #1
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72

    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.

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by Nightowl View Post
    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...

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 01:57 AM
  4. classs with multiple arrays
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 05-06-2007, 08:31 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21