Original Post
[color=#000000][font=verdana, arial, helvetica, sans-serif]I've got the logic right and the algo seems to work fine. Now, that's epic for me
I've got the logic right and the algo seems to work fine. Now, that's epic for me
But..[/font]
[color=#000000][font=verdana, arial, helvetica, sans-serif]I want to add 2 more features to this code:[/font]
[color=#000000][font=verdana, arial, helvetica, sans-serif]1) Exit on pressing 'x'. For now I'm using -1 to exit.[/font]
[color=#000000][font=verdana, arial, helvetica, sans-serif]2) Print a message "Element not found" when an element not present in the array is searched for. Yeah, I'm not able to figure that out :|[/font]
[color=#000000][font=verdana, arial, helvetica, sans-serif]Well, Here goes my code:[/font]
[font=verdana, arial, helvetica, sans-serif][color=#000000][/font]
#include<iostream>
using namespace std;
int BinSearch(int data[], int numElements, int searchKey); // Prototype
int main()
{
int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
int numElements = 23; // Size of the Array is 23!!!
int searchKey;
int found;
for(int i = 0; i < numElements; i++)
cout << data << ", ";
cout << endl;
for(;;)
{
cout << "Enter search key: (-1 to exit) "; // I want to quit when I press 'x'. Please let me know.
cin >> searchKey;
if(searchKey == -1)
{
cout << "Exiting\n";
break;
}
found = BinSearch(data, numElements,searchKey);
cout << searchKey << " is in position " << found << endl;
}
}
int BinSearch(int data[], int numElements, int searchKey)
{
int position, mid;
int beg = 0;
int end = numElements;
// Algorithm Begins.
for(int i = beg; i <= end;)
{
mid = (beg+end)/2;
if(data[mid] == searchKey)
{
return mid;
}
else if(data[mid] > searchKey)
{
end = mid - 1;
}
else if(data[mid] < searchKey)
{
beg = mid + 1;
}
}
}
[font=verdana, arial, helvetica, sans-serif][color=#000000]<h3>[/font]
Thanks,
--Sid
[color=#000000][font=verdana, arial, helvetica, sans-serif]
1) Exit on pressing 'x'. For now I'm using -1 to exit.[/font]
[color=#000000][font=verdana, arial, helvetica, sans-serif]2) Print a message "Element not found" when an element not present in the array is searched for. Yeah, I'm not able to figure that out :|[/font]
[color=#000000][font=verdana, arial, helvetica, sans-serif]Well, Here goes my code:[/font]
[font=verdana, arial, helvetica, sans-serif][color=#000000][/font]
#include<iostream>
using namespace std;
int BinSearch(int data[], int numElements, int searchKey); // Prototype
int main()
{
int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
int numElements = 23; // Size of the Array is 23!!!
int searchKey;
int found;
for(int i = 0; i < numElements; i++)
cout << data << ", ";
cout << endl;
for(;;)
{
cout << "Enter search key: (-1 to exit) "; // I want to quit when I press 'x'. Please let me know.
cin >> searchKey;
if(searchKey == -1)
{
cout << "Exiting\n";
break;
}
found = BinSearch(data, numElements,searchKey);
cout << searchKey << " is in position " << found << endl;
}
}
int BinSearch(int data[], int numElements, int searchKey)
{
int position, mid;
int beg = 0;
int end = numElements;
// Algorithm Begins.
for(int i = beg; i <= end;)
{
mid = (beg+end)/2;
if(data[mid] == searchKey)
{
return mid;
}
else if(data[mid] > searchKey)
{
end = mid - 1;
}
else if(data[mid] < searchKey)
{
beg = mid + 1;
}
}
}
[font=verdana, arial, helvetica, sans-serif][color=#000000]<h3>[/font]
Thanks,
--Sid
[color=#000000][font=verdana, arial, helvetica, sans-serif]
Well, Here goes my code:[/font]
[font=verdana, arial, helvetica, sans-serif][color=#000000][/font]
#include<iostream>
using namespace std;
int BinSearch(int data[], int numElements, int searchKey); // Prototype
int main()
{
int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
int numElements = 23; // Size of the Array is 23!!!
int searchKey;
int found;
for(int i = 0; i < numElements; i++)
cout << data << ", ";
cout << endl;
for(;;)
{
cout << "Enter search key: (-1 to exit) "; // I want to quit when I press 'x'. Please let me know.
cin >> searchKey;
if(searchKey == -1)
{
cout << "Exiting\n";
break;
}
found = BinSearch(data, numElements,searchKey);
cout << searchKey << " is in position " << found << endl;
}
}
int BinSearch(int data[], int numElements, int searchKey)
{
int position, mid;
int beg = 0;
int end = numElements;
// Algorithm Begins.
for(int i = beg; i <= end;)
{
mid = (beg+end)/2;
if(data[mid] == searchKey)
{
return mid;
}
else if(data[mid] > searchKey)
{
end = mid - 1;
}
else if(data[mid] < searchKey)
{
beg = mid + 1;
}
}
}
[font=verdana, arial, helvetica, sans-serif][color=#000000]<h3>[/font]
Thanks,
--Sid
[/font]
#include<iostream>
using namespace std;
int BinSearch(int data[], int numElements, int searchKey); // Prototype
int main()
{
int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
int numElements = 23; // Size of the Array is 23!!!
int searchKey;
int found;
for(int i = 0; i < numElements; i++)
cout << data << ", ";
cout << endl;
for(;;)
{
cout << "Enter search key: (-1 to exit) "; // I want to quit when I press 'x'. Please let me know.
cin >> searchKey;
if(searchKey == -1)
{
cout << "Exiting\n";
break;
}
found = BinSearch(data, numElements,searchKey);
cout << searchKey << " is in position " << found << endl;
}
}
int BinSearch(int data[], int numElements, int searchKey)
{
int position, mid;
int beg = 0;
int end = numElements;
// Algorithm Begins.
for(int i = beg; i <= end;)
{
mid = (beg+end)/2;
if(data[mid] == searchKey)
{
return mid;
}
else if(data[mid] > searchKey)
{
end = mid - 1;
}
else if(data[mid] < searchKey)
{
beg = mid + 1;
}
}
}
[font=verdana, arial, helvetica, sans-serif][color=#000000]<h3>[/font]Thanks,
--Sid