Compare all array with one argument at once. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Compare all array with one argument at once.

Example : Arr[5] = {5,6,2,9,1}; int x; How to compe all arr with x just once. Not by every index from 0 to 4. Is any sintax ability in C++ for it?

23rd Jan 2017, 1:19 AM
Denis Zinkov
Denis Zinkov - avatar
1 Answer
+ 1
Here is a way. This ofcourse can be written better because I am no expert. #include <iostream> using namespace std; //The variable && Array int Arr[5] = {5, 6, 2, 9, 1}; int x; //Just gets the size of array int arrSize = sizeof(Arr)/sizeof(Arr[0]); int main() { //A way to get the value of x cin >> x; //Checks x with each element in the array for (int i = 0; i < arrSize; i++ ) { if (x == Arr[i]) { cout << "A match!" << endl; cout << Arr[i] << endl; break; } else { cout << "No match" << endl; cout << Arr[i] << endl; } } return 0; }
23rd Jan 2017, 4:14 PM
Dawzy
Dawzy - avatar