# Thread: Comparing Arrays

1. ## 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. ## 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. 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.

4. ## 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. 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

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

7. ## Thanks For All Your Help

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

Popular pages Recent additions