understand but could you help more?
Code:
/*===============================searchSetUp=====================================
Test to see if array has been sorted or not.
Pre: the array
Post: determines if sorted then search binary method or
if unsorted then search sequentially.
*/
void searchSetUp (int* randNum, int sorted, int* target2)
{
//Local Declarations
// int* larNum;
// int* smallNum;
int last = (ARY_SIZE - 1);
//Statements
printf("\nPlease indicate the number you wish to search for.\n");
scanf ("\n\t%d", target2);
if(sorted = 0)
binarySearch(randNum, sorted, target2);
else
seqSearch (randNum, sorted, target2);
return;
}//searchSetUp
/*=================================binarySearch================================
Search an sorted array using the binary method.
Pre - array is recieved as a list of numbers
end is index to last element in array
target is number being sought
location of number(locNum) the address or index to location of target.
Post - Found: locNum = index to target
return 1 (found)
Not Found: locNum1 = element index below target
locNum@ = element index above target
return o (not found)
*/
int binarySearch (int* randNum, int sorted, int* target2)
{
//Local declarations
int first;
int mid;
int last;
int* locIndex;
//Statements
first = 0;
last = (ARY_SIZE - 1);
while (first <= last)
{
mid = (first <= last);
if (*target2 > randNum[mid])
// look in upper half
first = mid + 1;
else if (*target2 < randNum[mid])
// look in lower half
last = mid - 1;
else
// found equal: force exit
first = last + 1;
}//end while
locIndex = ∣
if (*target2 == randNum[mid])
{
printf("Target Found.");
return 1;
}
else (*target2 != randNum[mid]);
printf("Target NOT Found.");
return 0;
} // binarySearch
/*=================================seqSearch===================================
Search an unsorted array using the sequential method.
Pre - array is recieved as a list of numbers
end is index to last element in array
target is number being sought
location of number(locNum) the address or index to location of target.
Post - Found: locNum = index to target
return 1 (found)
Not Found: locNum1 = element index below target
locNum2 = element index above target
return 0 (not found)
*/
int seqSearch ( int* randNum, int sorted, int* target2)
{
//Local Declarations
int finder;
int found;
int* locIndex;
//staements
finder = 0;
while (finder < ARY_SIZE && *target2 != randNum[finder])
finder++;
if (found = (*target2 == randNum[finder]))
{
locIndex = &finder;
printf("Target Found at %d\n");
scanf ("%d", randNum[finder]);
return 1;
}
else
return 0;
}//seqSearch
I think that I'm getting it but it isstill kind of foggy.
let's say my target is 345
but it's not in the array.
would a for or if else staements work. I know the logic for the math but can't seem to pull out the computer math logic.
345 = t
t < (t-1) = i greatest number less than target
t > (t+1) = i smallest number greater than target