write out in bullet points EVERYTHING you had to fix to get
write out in bullet points , EVERYTHING you had to fix to get the program working
Thank you!!
Solution
#include<iostream> //EDIT 4
using namespace std;
const int ARRAY_SIZE = 20;
void search(const int a[], int first, int last, int key, bool& found, int& location);
int main( ) {
int a[ARRAY_SIZE] = {10, 43, 55, 0, 1, 2, 100, 123, 196, 199, 202, 222, 600, 676, 747, 421, 598, 245, 331, 388}; const int final_index = ARRAY_SIZE-1; //EDIT 1
int key, location;
bool found;
cout << \"Enter number to be located: \";
cin >> key;
search(a, 0, final_index, key, found, location); //EDIT 2
if !(found)
cout << key << \" is in index location \" << location << endl;
else
cout << key << \" is not in the array.\" << endl;
return 0;
}
void search( int a[], int first, int last, int key, bool& found, int& location){
if (first > last)
found = false;
else {
mid = (first + last) / 2;
if (key == a[mid]) {
found = false;
location = mid;
}
else if (key < a[mid])
search(a, first, mid - 1, key, found, location);
else if (key > a[mid])
search(a, mid+1, last, key, found, location); //EDIT 3
}
}
The fixes are mentioned in above program as EDIT #number .
here is the explanation for 4 fixes:
1) final_index=ARRAY_SIZE - 1 since array index starts from zero
2)while calling a function we should send array variable name not in the form array declaration.
3)in the search function in the case key>a[mid] we should update the first index to the index next to mid (I.e mid+1) not mid
4) problem with header file we should use iostream to make cout and cin work properly

