- //Natalie Zarate                   CSC 5                   Chapter 8, P. 487, #8 
- /******************************************************************************* 
-  * DISPLAY TOTAL COMPARISONS 
-  *  
-  * This program will accept a value between the 3rd value of the Fibbonacci  
-  * sequence to the 23rd value as input, then, it will search for that value  
-  * and display how many times it had to compare values using two different  
-  * methods of searching, 
-  * _____________________________________________________________________________ 
-  * INPUT  
-  *  
-  * integers                                  - number of elements in  
-  *                                             pre-intialized array 
-  * array[integers]                           - 3rd to 23rd value of Fibbonacci  
-  *                                             sequence.  
-  * searchNum                                 - inputted value program will  
-  *                                             search for 
-  * lcomparisons                              - number of comparisons made using  
-  *                                             linear search 
-  * bcomparisons                              - number of comparisons made using 
-  *                                             binary search 
-  * LinearSearch(array, integers, searchNum)  - function; searches for value 
-  *                                             using linear search 
-  * BinarySearch(array, integers, searchNum)  - function; searches for value  
-  *                                             using binary search 
-  *  
-  * OUTPUT 
-  *  
-  * DisplayResult(lcomparisons, bcomparisons) - functuon; outputs total number of  
-  *                                             comparisons needed to find value 
-  *                                             for linear and binary search. 
-  ******************************************************************************/  
- #include <iostream> 
- using namespace std; 
-   
- // Prototype for LinearSearch 
- int LinearSearch(int numbers[], int size, int search); 
-   
- //Prototype for BinarySearch 
- int BinarySearch(int numbers[], int size, int search); 
-   
- //Prototype  
- void DisplayResult(int linear, int binary); 
-   
- int main()  
- { 
- 	/******************************************************************** 
- 	 * CONSTANTS 
- 	 * ------------------------------------------------------------------ 
- 	 * integers - size of array holding fibonacci sequence 
- 	 * array[integers] - 3 to 23rd values of fibonacci sequence 
- 	 *******************************************************************/  
- 	int integers = 20;   
- 	int array[integers] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,  
- 	                      377, 590, 967, 1557, 2524, 4081, 6605, 10686}; 
-   
- 	int searchNum;    // INPUT - user-inputted value 
-     int	lcomparisons; // INPUT - number of comparisons when using linear search 
-     int bcomparisons; // INPUT - number of comparisons when using binary search 
-   
- 	// Prompt for number to look for 
- 	cout << "Enter a positive integer: " << endl; 
- 	cin >> searchNum; 
-   
- 	// Call LinearSearch 
- 	lcomparisons = LinearSearch(array, integers, searchNum); 
-   
- 	//Call BinarySort 
- 	bcomparisons = BinarySearch(array, integers, searchNum); 
-   
- 	//Call DisplayResult 
- 	DisplayResult (lcomparisons, bcomparisons); 
-   
- 	return 0; 
- } 
-   
- /******************************************************************************* 
-  * FUNCTION LINEAR SEARCH 
-  *  
-  * This function will search for the user-inputted value using linear searching, 
-  * then return the number of comparisons it needed before finding the value.  
-  *  
-  *______________________________________________________________________________ 
-  * INPUT 
-  *  
-  * subscript - element being searched 
-  * element - location of user-inputted value 
-  * found - flag; indicates if the value was found 
-  *  
-  * OUTPUT 
-  * sortcount - counter for number of times values were compared 
-  ******************************************************************************/ 
-  int LinearSearch (int numbers[], int size, int search) 
-  { 
- 	/******************************************************************** 
- 	 * CONSTANTS 
- 	 * ------------------------------------------------------------------ 
- 	 * subscript - element being searched 
- 	 *******************************************************************/ 
-   
-  	int subscript = 0; 
-   
-  	int element = -1;   // INPUT - location of user-inputted value 
-  	bool found = false; // INPUT - flag; indicates if the value was found 
-  	int sortcount = 0;  // OUTPUT - counter for number of times values were  
-  	                    //          compared 
-   
-  	// step through array 
-  	while (subscript < size && !found) 
-  	{ 
-  		//If value is found 
-  		if (numbers[subscript] == search) 
-  		{ 
-  			found = true; 
-   
-  			//Record value's subscript  
-  			element = subscript; 
-   
-  		} 
-   
-  		//Record comparison 
-  		sortcount++; 
-   
-  		//move on to next subscript 
-  		subscript++; 
-  	} 
-   
-  	return sortcount; 
-  } 
- /******************************************************************************* 
-  * FUNCTION  BINARYSEARCH 
-  *  
-  * This program will find the user-inputted value using binary searching, then  
-  * return the number of comparisons it needed before finding the value.  
-  * _____________________________________________________________________________ 
-  * INPUT 
-  *  
-  * firstElem - first element in array 
-  * middle    - reference point; midpoint in chunk of array 
-  * lastELem  - last element in array 
-  * element   - location of user-inputted value 
-  * found     - flag; indicates if the value was found 
-  *  
-  * OUTPUT 
-  *  
-  * sortcount - number of comparisons before value was found 
-  ******************************************************************************/  
- int BinarySearch(int numbers[], int size, int search) 
- { 
-   
- 	int firstElem = 0;       // INPUT - first element in array 
- 	int middle;              // INPUT - reference point; midpoint in chunk of  
- 	                         //         array 
- 	int lastElem = size - 1; // INPUT - last element in array 
- 	int element = - 1;       // INPUT - location of user-inputted value 
- 	bool found = false;      // INPUT - flag; indicates if the value was found 
- 	int sortcount = 0;       // OUTPUT - number of comparisons before value was  
- 	                         //          found  
-   
- 	// step through array 
- 	while(!found && firstElem <= lastElem) 
- 	{ 
- 		middle = (firstElem + lastElem) / 2; 
-   
- 		// if the user-input is equal to the middle-most value  
- 		if (numbers[middle] == search) 
- 		{ 
- 			found = true; 
- 			element = middle; 
-   
- 			//increment comparison counter 
- 			sortcount++; 
- 		} 
-   
- 		// if user input is greter than middle-most value 
- 		else if (numbers[middle] > middle) 
- 		{ 
- 			//update middle 
- 			lastElem = middle - 1; 
-   
- 			// increment comparison counter 
- 			sortcount++; 
- 		} 
-   
- 		// if user input is less than middle-most value 
- 		else 
- 		{ 
- 			// update middle 
- 			firstElem = middle + 1; 
-   
- 			// increment comparison counter 
- 			sortcount++; 
- 		} 
- 	} 
-   
- 	return sortcount; 
- } 
- /******************************************************************************* 
-  * FUNCTION DISPLAYRESULT 
-  *  
-  * This function will display the total comparisons used in linear and binary  
-  * searches when searching for the user-inputted value. 
-  * _____________________________________________________________________________ 
-  * OUTPUT 
-  *  
-  * Message with total comparisons used in linear and binary search 
-  ******************************************************************************/  
- void DisplayResult(int linear, int binary) 
- { 
- 	cout << "Using Linear Sorting, it took " << linear << " comparisons to"; 
- 	cout << " find the value" << endl; 
- 	cout << "Using Binary Sorting, it took " << binary << " comaprisons to"; 
- 	cout << " find the value"; 
-   
- }