@jkmyoung
Hi jkmyoung
Can you help me to apply your pseudo code in this program because honestly I just got about two months studying by myself C++ . THANKS
#include <iostream>
#include <iomanip>
using namespace std;
const int depth = 4;
const int width = 5;
int BinarySearch(int [], int, int);
int main()
{
cout<<endl;
int array[depth][width] = {{5,10,22,32,45},{20,23,34,40,56},
{12,14,16,34,45},{2,6,34,45,47}};
int num;// location;
for(int i=0; i<depth; i++)
{
for(int j=0; j<width; j++)
{
cout<<setw(5)<<array[i][j];
}
cout<<endl;
}
cout << "Enter the number you are searching for: ";
cin >> num;
location= BinarySearch(array, width, num);
if (location > -1)
cout << "The number was found at index location "
<< location << endl;
else
cout << "The number was not found in the list\n";
return 0;
}
int BinarySearch(int list[], int size, int key)
{
int left, right, midpt;
left = 0;
right = size - 1;
while (left <= right)
{
midpt = (int) ((left + right) / 2);
if (key == list[midpt])
{
return midpt;
}
else if (key > list[midpt])
left = midpt + 1;
else
right = midpt - 1;
}
return -1;
}
// pseudo code
// int[depth][width] array
// int searchTerm
// for(i = 0; i < depth; i++)
// if BinarySearch(searchTerm, array[i], 0, width - 1) returns found, print "row" + i