fork download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. int arrayLength = 10;
  8. //cout << "How many items?";
  9. //cin >> arrayLength;
  10. int inputArray[arrayLength];
  11. int needle;
  12. int currentLeft = 0;
  13. int currentRight = round(arrayLength/2);;
  14. int current;
  15. int iniPoint = round(arrayLength/2);
  16. int i = 0;
  17. //inputArray = new int[arrayLength];
  18. for(int j = 0 ; j < arrayLength; j++) {
  19. //cout << "Enter item" << j;
  20. inputArray[j] = j+5;
  21. }
  22.  
  23. //cout << "Eneter Needle";
  24. needle = 7;
  25.  
  26. current = inputArray[iniPoint];
  27. cout << "checking " << iniPoint << " val = " << current << endl;
  28. while (current != needle && currentLeft >= currentRight) {
  29. if(current < needle) {
  30. iniPoint = currentLeft + ((currentRight - currentLeft)/2);
  31. currentRight = iniPoint;
  32. } else {
  33. iniPoint = currentRight + ((currentRight - currentLeft)/2);
  34. currentLeft = iniPoint;
  35. }
  36. current = inputArray[iniPoint];
  37. cout << "checking " << iniPoint << " val = " << current << endl;
  38. }
  39.  
  40.  
  41. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
checking 5 val = 10