I would like to compare each element of one array to all elements of another array. What I want to achieve is if an element exists in another array, result =0, otherwise result =1
int m,n;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(i==j) {
result =0;
//perform a task
break;
}
if(i!=j) {
result = 1;
//perform another task
break
}
}
}
however, i fail to achieve what i want in the second if ()
Tweaking your code somewhat (replace
charwith whatever datatype you’re actually using):Note that the “
else” code will run once for every element.We can do better. First make a utility function that searches an array:
Then use it.