English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

what function should i use to compare each element of the first array to all the elements in the second one?

2007-01-19 19:18:37 · 8 answers · asked by Sammy Baby 1 in Computers & Internet Programming & Design

im using c language, how will you do it using loops?(any loop will do)

2007-01-19 19:29:47 · update #1

what if i want to compare each element with all the elements of the other set of array? ex, array[5] & array2[5]:
i want to compare array[0] with array2[0], array2[1], array2[2], and so on... how???

2007-01-19 21:43:10 · update #2

8 answers

It would help if you said what language you are using.

Otherwise, if the array lengths and dimensions match, and each element contains the same value, they are the same. So you can always check their lengths and dimensions, then iterate through those dimensions and check their values.

2007-01-19 19:26:35 · answer #1 · answered by Anonymous · 0 1

You mentioned you were using C, so here is a function using C-style syntax. This example only allows two similar sized arrays. It's should hopefully be obvious enough how to change that to allow for two different sized arrays.

#include
#include

// Define your length of the arrays here.
#define ARY_LENGTH 10

int main(int argc, char *argv[])
{

int array1[ARY_LENGTH];
int array2[ARY_LENGTH];
int i;

// Populate your array here. This was a simple example
// I used.
for( i=0; i < ARY_LENGTH; i++) {
array1[ i] = i;
array2[ i] = i;
}
// End of example.

printf( "Simularities : %d", CompareArrays( array1, array2));
return 0;
}

int CompareArrays(int *ary1, int *ary2) {

int similarities = 0;
int index1 = 0;
int index2 = 0;

for( index1 = 0; index1 < ARY_LENGTH; index1++) {
for( index2 = 0; index2 < ARY_LENGTH; index2++) {
if( ary1[ index1] == ary2[ index2]) {
similarities++;
}
}
}

return similarities;
}

2007-01-21 02:13:58 · answer #2 · answered by Kookiemon 6 · 0 0

Something like that:

int array[20];
int array2[20];
int i;

for (i = 0; i < 20 ; ++i) {
if (array[i] != array2[i])
return 0;
}
return 1;

2007-01-19 19:57:05 · answer #3 · answered by Yuval 2 · 0 1

Nested for loops that take a value from the first array and compare it to values in the second until it matches. If no match, return false. It's pretty simple.

2016-05-24 00:02:10 · answer #4 · answered by ? 4 · 0 0

Function: int memcmp (const void *a1, const void *a2, size_t size)

The function memcmp compares the size bytes of memory beginning at a1 against the size bytes of memory beginning at a2. The value returned has the same sign as the difference between the first differing pair of bytes (interpreted as unsigned char objects, then promoted to int).

If the contents of the two blocks are equal, memcmp returns 0.

2007-01-19 19:21:18 · answer #5 · answered by charlyvvvvv 3 · 0 0

I think this should work:

int a[5],b[5],i,j;

for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
{
if(a[i]!=b[ j])
return 0;
else
return 1;
}
}

2007-01-19 22:39:00 · answer #6 · answered by Anonymous · 1 0

In PHP, I would use ($array1 === $array2)

2007-01-19 19:27:44 · answer #7 · answered by the DtTvB 3 · 0 1

int memcmp(const void *b1, const void *b2, size_t len)

Check for 0.

memcmp(array1,array2, sizeof(array1))

2007-01-19 19:21:58 · answer #8 · answered by To Be Free 4 · 0 0

fedest.com, questions and answers