+ 2

Do you know how to find if an integer is in one array?

I want to know is int x is in one of three next arrays: Arr1, Arr2, Arr3. These arrays have values so If I type cin>> x; I want to kniw if x is part of Arr1, 2 or 3. I tried creating arrays of 0's 1's and 2's but I realized I have to learn more...

17th Oct 2017, 11:27 AM
Diana Contreras
Diana Contreras - avatar
2 Answers
+ 3
you mean searching right. Maybe my code makes it clear for you : https://code.sololearn.com/cCc4wly5hiDh/?ref=app
17th Oct 2017, 12:23 PM
RZK 022
RZK 022 - avatar
0
bool isInArray(int x, int *arr, int size) { for (int i = 0; i < size; i++) { if (arr[i] == x) return true; } return false; } int main() { int arr[100]; int arrSize = (sizeof(arr)/sizeof(*arr)); for (int i = 0; i < arrSize; i++) { arr[i] = i; } int a; cin >> a; bool aIsInArray = isInArray(a, arr, arrSize); cout << a << (aIsInArray ? " is " : " isn't ") << "in arr" << endl; return 0; }
17th Oct 2017, 12:41 PM
deFault