Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
530 B
6
Indexable
Never
#include <iostream>
using namespace std;

int search(int array[], int n, int x) {

  // Going through array sequencially
  for (int i = 0; i < n; i++)
    if (array[i] == x)
      return i;
  return -1;
  cout<<"element not found"<<endl;
}

int main() {
  int array[] = {2, 4, 0, 1, 9};
  int x ;
  cout<<"enter the element you want to search:"<<endl;
  int n = sizeof(array) / sizeof(array[0]);

  int result = search(array, n, x);

  (result == -1) ? cout << "Element not found" : cout << "Element found at index: " << result;
}