# Comparing Arrays

• 04-24-2008
cjohnman
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
• 04-24-2008
cjohnman
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++ }```
• 04-24-2008
Quote:

Originally Posted by cjohnman
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.
• 04-24-2008
cjohnman
Can you help me since the arrays are different sizes
Quote:

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
• 04-24-2008
matsp
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
• 04-24-2008
```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.```