Find if an element exists in C++ array -
#include <iostream> float x[10], k; int n, i; cout<<"n= "; cin>>n; (i=0; i<n; i++){ cout<<"x["<<i<<"]= "; cin>>x[i]; } cout<<"array's elements: "; (i=0; i<n; i++) cout<<x[i]<<", "; cout<<endl<<"k= "; cin>>k; for(i=0; i<n; i++) if(x[i]!=k){ cout<<endl<<"k doesn't exist in array."; cout<<endl<<"k= "; cin>>k; }
i trying find if element exists in array, if doesn't exist want re-type element , repeat whole array , check it. mine doesn't start (i=0).
there standard function called std::find
in header <algorithm>
:
#include <iostream> #include <algorithm> int main() { int myarray[6]{10, 4, 14, 84, 1, 3}; if (std::find(std::begin(myarray), std::end(myarray), 1) != std::end(myarray)) std::cout << "it exists"; else std::cout << "it not exist"; return 0; }
Comments
Post a Comment