# Thread: Compare integers in an array

1. ## Compare integers in an array

Hello. I'm new to C programming and attending a web course. We learned about arrays but I still struggle to understand how to compare values in an array. Say I have
Code:
`{2 4 6 1 5}`
and I want to compare index 0 with index 1,2,3,4 not by typing
Code:
`array`
and
Code:
`array`
but
Code:
`array[k] with array[?]`
. 2. I'm going to assume that you're trying to compare "every element to every other element" or some such. This is a use case that occurs frequently in dealing with arrays.

To do this, you'll want two variables. Traditionally, these are i and j. When you get to a third variable, that will be k. (This comes from Fortran, where variable type (integer, real, etc) was determined by the first letter.)

Code:
```extern SomeType a[];
extern size_t alen;

for (int i = 0; i < alen; ++i) {
for (int j = i + 1; i < alen; ++j) {
// do something with a[i], a[j]
}
}``` 3. Originally Posted by emikash Hello. I'm new to C programming and attending a web course. We learned about arrays but I still struggle to understand how to compare values in an array. Say I have
Code:
`{2 4 6 1 5}`
and I want to compare index 0 with index 1,2,3,4 not by typing
Code:
`array`
and
Code:
`array`
but
Code:
`array[k] with array[?]`
.

Code:
```int main(void)
{
int array = {2, 4, 6, 1, 5};
int j, k;

for (j =0; j < 5; j++)
for (k = 0; k < 5; k++)
{
char compare;
if (array[j] < array[k])
compare = '<';
else if (array[j] > array[k])
compare = '>';
else
compare = '=';

printf(array[%d] %c array[%d]\n", j, compare, k);
}

}``` Popular pages Recent additions array, arrays in c, compare, index 