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?
1 Resposta
+ 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;
}