Hi! How can you search an array to see if two arrays contain the same number.
For example:
Code:
arr1 = {1, 2, 3, 4, ...}
arr2 = {6, 5, 1, 0, ...}
int foundAt = linearSearch(arr1, 10, arr2[0]);
This will only compare if the first number of arr2 exists in arr1, but I want to compare for all of the numbers that exist in arr2.
I get that I probably should use a for-loop somehow, but don't really get how.
Idea
Code:
int i;
for (i = 0; i < 10; i++) {
int j;
for (j = 0; j < 10; j++) {
foundAt[i] = linearSearch(arr1, 10, arr2[j]);
if (foundAt[i] == -1) {
// Add number to array
}
else {
// Don't add number to array
}
}
}
Code:
int linearSearch(const int arr[], int arrSize, int taken) {
int found = 0;
int location = -1;
int i;
for (i = 0; i < arrSize && !found; i++) {
if (taken == arr[i]) {
found = 1;
location = i;
}
}
return location;
}