April 1st, 2006, 11:00 PM
Linear search using recursive function
I'm new here and am seeking some help with an assignment. I understand that a recursive function calls itself. However, I've been working on this assignment for some time and can't get it to work. Can someone help by stating what part of my code is wrong and explain why? I'm confused because I don't understand how a recursive function would handle an array. I've searched and only found the Fibonacci Series.
The assignement is to use a recursive function called linearSearch to perform linear search of the array. The function should receive an interger array and the size of the array as arguments. If the search key is found, return the array subscript; otherwise, return -1.
Thank you very much for any help.
using namespace std;
int linearSearch( int , int );
const int arraySize = 100;
for( int i = 0; i < arraySize; i++)
a[i] = 2 * i;
element = linearSearch( a, arraySize );
if( element != -1 )
cout << "Found value in element " << element << endl;
cout << "Value not found." << endl;
int linearSearch( int array, int counter )
if( linearSearch( array, counter - 1 ) != 10 )
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!