### How do you display the number of iterations

How do you display the number of iterations it takes to find a number that the user would input into the binary search?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586`` ``````/********** * * * ***********/ #include #include #include #include using namespace std; int selectionSort(int [], int); int binarySearch(int[], int, int); int main() { srand (time_t(NULL)); int narray[100]; for (int i=0; i<100; i++) { narray[i]=rand()%999; } int i, moves; moves = selectionSort(narray, 100); cout << "The sorted list, in ascending order is: \n"; for (i=0; i < 100; i++) cout << " " << narray[i]; cout << endl << moves << " moves were made to sort this list. \n"; int item, location,iterations; cout<<"Enter the number you wish to find:"; cin>> item; location=binarySearch(narray, 100, item); iterations=selectionSort(narray, 100); if (location > -1) cout << "The number was found at index location " << location << " after " << iterations << " iterations."< list[midpoint]) left = midpoint + 1; else right = midpoint - 1; } return -1; }``````
put this outside any while, if, for loop condition.
`int counter =0;`
Line 19 would be my recommendation.

Every time the user inputs something you want to count
`counter++;`

Show Total
cout << "Your counter = " << counter << endl;
Topic archived. No new replies allowed.