helo,
I want to modify a linear Search program which is currently using a loop to search for a number in an array to a recursive one. But don't know where to start. Pls direct. Thnx in advance. The current iterative version of linearSearch is :
To modify this code to recursive, i can pass two arguments to the function: 1. integer array 2. size of the arrayCode:#include <stdio.h> #define SIZE 100 int linearSearch( const int [], int, int ); int main() { int a[ SIZE ], x, searchKey, element; for ( x = 0; x <= SIZE - 1; x++ ) /* create data */ a[ x ] = 2 * x; printf( "Enter integer search key:\n" ); scanf( "%d", &searchKey ); element = linearSearch( a, searchKey, SIZE ); if ( element != -1 ) printf( "Found value in element %d\n", element ); else printf( "Value not found\n" ); return 0; } int linearSearch( const int array[], int key, int size ) { int n; for ( n = 0; n <= size - 1; ++n ) if ( array[ n ] == key ) return n; return -1; }
Is it possible? Pls show.



LinkBack URL
About LinkBacks


