home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
array6.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-26
|
4KB
|
163 lines
// C++ program that searches arrays using the linear
// and binary searches methods
#include <iostream.h>
const int MAX = 10;
const int TRUE = 1;
const int FALSE = 0;
const int NOT_FOUND = -1;
int obtainNumData()
{
int m;
do { // obtain number of data points
cout << "Enter number of data points [2 to "
<< MAX << "] : ";
cin >> m;
cout << "\n";
} while (m < 2 || m > MAX);
return m;
}
void inputArray(int intArr[], int n)
{
// prompt user for data
for (int i = 0; i < n; i++) {
cout << "arr[" << i << "] : ";
cin >> intArr[i];
}
}
void showArray(int intArr[], int n)
{
for (int i = 0; i < n; i++) {
cout.width(5);
cout << intArr[i] << " ";
}
cout << "\n";
}
void sortArray(int intArr[], int n)
// sort the first n elements of array intArr
// using the Comb sort method
{
int offset, temp, inOrder;
offset = n;
do {
offset = (8 * offset) / 11;
offset = (offset == 0) ? 1 : offset;
inOrder = TRUE;
for (int i = 0, j = offset; i < (n - offset); i++, j++) {
if (intArr[i] > intArr[j]) {
inOrder = FALSE;
temp = intArr[i];
intArr[i] = intArr[j];
intArr[j] = temp;
}
}
} while (!(offset = 1 && inOrder == TRUE));
}
int linearSearch(int searchVal, int intArr[], int n)
// perform linear search to locate the first
// element in array intArr that matches the value
// of searchVal
{
int notFound = TRUE;
int i = 0;
// search through the array elements
while (i < n && notFound)
// no match?
if (searchVal != intArr[i])
i++; // increment index to compare the next element
else
notFound = FALSE; // found a match
// return search outcome
return (notFound == FALSE) ? i : NOT_FOUND;
}
int binarySearch(int searchVal, int intArr[], int n)
// perform binary search to locate the first
// element in array intArr that matches the value
// of searchVal
{
int median, low, high;
// initialize the search range
low = 0;
high = n - 1;
// search in array
do {
// obtain the median index of the current search range
median = (low + high) / 2;
// update search range
if (searchVal > intArr[median])
low = median + 1;
else
high = median - 1;
} while (!(searchVal == intArr[median] || low > high));
// return search outcome
return (searchVal == intArr[median]) ? median : NOT_FOUND;
}
void searchInUnorderedArray(int intArr[], int n)
// manage the linear search test
{
int x, i;
char c;
// perform linear search
cout << "Search in unordered array? (Y/N) ";
cin >> c;
while (c == 'Y' || c == 'y') {
cout << "Enter search value : ";
cin >> x;
i = linearSearch(x, intArr, n);
if (i != NOT_FOUND)
cout << "Found matching element at index " << i << "\n";
else
cout << "No match found\n";
cout << "Search in unordered array? (Y/N) ";
cin >> c;
}
}
void searchInSortedArray(int intArr[], int n)
// manage the binary search test
{
int x, i;
char c;
// perform binary search
cout << "Search in sorted array? (Y/N) ";
cin >> c;
while (c == 'Y' || c == 'y') {
cout << "Enter search value : ";
cin >> x;
i = binarySearch(x, intArr, n);
if (i != NOT_FOUND)
cout << "Found matching element at index " << i << "\n";
else
cout << "No match found\n";
cout << "Search in sorted array? (Y/N) ";
cin >> c;
}
}
main()
{
int arr[MAX];
int n;
n = obtainNumData();
inputArray(arr, n);
cout << "Unordered array is:\n";
showArray(arr, n);
searchInUnorderedArray(arr, n);
sortArray(arr, n);
cout << "\nSorted array is:\n";
showArray(arr, n);
searchInSortedArray(arr, n);
return 0;
}