If we want a search function to search an array for some val
If we want a search function to search an array for some value and return either the index where the value was found, or -1 if not found, which of the following prototypes would be appropriate?
Solution
Say for suppose you want to search an integer value in an integer array
Suitable method prototype:
int search(int[] targetArray,int searchValue);
explanation:
return value:
returns -1 if number not found else return index.
Parameters:
targetArray- Array to be iterated to search
searchValue-value to be searched
Sample implementation:
int search(int[] targetArray,int searchValue)
{
//iterate throught the target array
for (int i=0;i<targetArray.length;i++)
{
if(targetArray[i]==searchValue)//compare value at each index
return i;//if matches return index
}
return -1;//if match is not found in iteration return -1
}
