fork download
  1. #include <iostream>
  2. #include <cstddef>
  3.  
  4. int* find(int target, int* start, int* stop)
  5. {
  6. while (start < stop)
  7. {
  8. if (*start == target)
  9. {
  10. return start;
  11. }
  12. ++start;
  13. }
  14.  
  15. return nullptr;
  16. }
  17.  
  18. int main()
  19. {
  20. int someArray[] = {3,10,19,7,3,45,123,4,9,89};
  21.  
  22. int *start = &someArray[0];
  23. int *stop = &someArray[10];
  24.  
  25. std::cout << "Enter a number to find: ";
  26.  
  27. int num;
  28. std::cin >> num;
  29.  
  30. int *found = find(num, start, stop);
  31. if (found)
  32. std::cout << "Found at index " << static_cast<ptrdiff_t>(found - start);
  33. else
  34. std::cout << "Not found";
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 4204KB
stdin
89
stdout
Enter a number to find: Found at index 9