previous | start | next

Precursor to template functions: pointers and arrays



bool find(int *start, int* limit, int x)
{
  for(int * p = start; p != limit; p++) {
     if ( *p == x ) {
        return true;
     }
  }
  return false;
}

int main()
{
  int a[] = {1,2,3,5,8,13,21,34};
  int *limita;
  limita = a + sizeof(a)/sizeof(int);
  int x;

  cin >> x;
  if ( find(a, limita, x) ) {
        cout << x << " found!" << endl;
  } else {
        cout << x << " not found!" << endl;
  }
}



previous | start | next