Thread: Comparing Arrays

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Comparing Arrays

    I four arrays called coil_array.coil[i], min_coil_array[i], coil_array.cc[i] and min_cc[i] all declared int [1000] belonging to a larger structure. I need to compare coil_array.coil[i] and min_coil_array[i] and when they hold the same value record the number[i]. I can then compare the contents of min_cc[i] and coil_array.cc[i]. If these two are not equal I need to replace coil_array.cc[i] with the contents in min_cc[i]. I need some type of loop and if anyone can help me I greatly appreciate it.

    -carl

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Here is one of my pitiful attempts

    Code:
    y=0;
    
    while(1)
    {
    if coil_array.coil[y] = min_coil_array[y];
     {
       if  coil_array.cc[y] != min_cc[y]
            coil_array.cc[y] = min_cc[y];
      }
    y++
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by cjohnman View Post
    I four arrays called coil_array.coil[i], min_coil_array[i], coil_array.cc[i] and min_cc[i] all declared int [1000] belonging to a larger structure. I need to compare coil_array.coil[i] and min_coil_array[i] and when they hold the same value record the number[i]. I can then compare the contents of min_cc[i] and coil_array.cc[i]. If these two are not equal I need to replace coil_array.cc[i] with the contents in min_cc[i]. I need some type of loop and if anyone can help me I greatly appreciate it.

    -carl
    Maybe something like this:

    Code:
    for(i = 0, hold = -1; i < SIZE OF THESE ARRAYS; i++)   {
       if(coil_array.coil[i] == min_coil.array[i])
          hold = i;
    
    // if(hold > -1)      //use only if the second if statement should be executed only when the first if statement, is true
       if(min_cc[i] != coil_array.cc[i])
          coil_array.cc[i] = min_cc[i];
    
       hold = -1;
    }
    Note that "hold" is not needed, since the value of "i" is available to both if statements.

    If the arrays are NOT the same size, you'll need additional code to handle that.

    Please don't name your array's "array" anything. We know it's an array, and long array names are very annoying.

    I can't tell from your description whether the second if statement should be done only when the first if statement is true, or not.
    Last edited by Adak; 04-24-2008 at 10:36 AM.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Can you help me since the arrays are different sizes

    I can't tell from your description whether the second if statement should be done only when the first if statement is true, or not.
    - Yes I want to make coil_array.cc[i] = min_cc[i] when coil_array.coil[i] == min_coil.array[i] and coil_array.cc[i] != min_cc[i].

    - The sizes of the array are different. I will try to figure out how to handle that but if you could help that would be great!

    Anyways thanks for your help up to this point and I will never name my array's "array" anything again.

    -Carl

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The conditon can be written like this.
    Code:
    if (coil_array.coil[i] == min_coil.array[i] && coil_array.cc[i] != min_cc[i])
       coil_array.cc[i] = min_cc[i];
    But it's probably just as good to do:
    Code:
    if (coil_array.coil[i] == min_coil.array[i])
       coil_array.cc[i] = min_cc[i];
    There is no point in actually comparing coil_array.cc[i] with min_cc[i] and then only setting them if different - you may just as well set it every time - there's very little difference between writing the data and comparing it - it's all got to been loaded into the processor anyways.

    Of course, if you have other work that needs to be done ONLY if you change something, then that's a different story. Now you do need to compare and only change if it's different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The only way to handle this with arrays of different sizes is to have logic for each of the possibilities. The below assumes that none of the arrays values will ever be zero

    Something like:

    Code:
    if(array1[i] && !array2[i])   {
       while(array1[i])   {
          //do whatever needs to be done when array1 still has data, and array2 has no more data
          //perhaps call a function just to handle this possibility
       }
    
    Repeat for the other arrays, in the combinations you need. With four arrays it can get messy. This should be right in 
    your for or while outer loop.
    You'll have to enumerate each way that the data can tail off on the end, and handle that with your code. Be sure to test it very well.
    Last edited by Adak; 04-24-2008 at 03:08 PM.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Thanks For All Your Help

    I will play with these examples and see what happens. It makes sense. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-14-2007, 12:20 AM
  2. Comparing Arrays
    By Raison in forum C++ Programming
    Replies: 10
    Last Post: 04-20-2004, 02:21 PM
  3. comparing arrays
    By dustyrain84 in forum C Programming
    Replies: 1
    Last Post: 01-21-2004, 05:52 PM
  4. Comparing Character Arrays
    By LostNotFound in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 12:42 PM
  5. Comparing Arrays
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-23-2001, 08:07 PM