Open 20P31A1242 opened 3 years ago
/ a)Write C program that use both recursive and non recursive functions to perform linear search for key value in a given list. /
includes
Void linearsearch(int val, int n, int arr[*]);
int linear_search_re(int val, int n, int arr[*]);
Void main() 6.{
int val , i, n, poss ;
Print("enter the size of an array:");
Scan f("%d",&n) ;
int arr [n];
Print if("\n enter theelements :") ;
for(i=0; i<n;i++)
{
Scan f ("%d" ,&arr[i]);
}
Print f("\n enter the value to
Search: ") ;Scan f("%d" , & val) ;
Print f ( "\n\n searching of an Element using non - recursive Function ") ;
Print f("\n ---------------------------------") ;
linear_ search(val,n,arr);
Print f("\n\n searching of an Element using recursive Function") ;
Print f ("\n-------------------------------");
Pos=linear_search_re(val,n,arr);
if(pos!=0)
Printf("\n element%d is Found at %d position",val, Pos) ;
else
Print f("\n%d is not found in the array",val);
}
Void linear_search(int val, int n, int arr[n])
{
int i, pos=-1;
for( i=0;i<n; i++)
{
if(arr[i]==val)
{
Pos= i ;
Print f("\n %d is
found in the array at position%d",val, i+1);}
}
if( poss==-1)
- Print f("\n %d does Nofound in the array",val);
}
int linear search re( int val, int n,int arr[n])
{
if( n>=0)
{
if (arr[n-1]==val)
Return n ;
else
return linear _ Search_re(val,n-1 ,arr) ;
}
/ a)Write C program that use both recursive and non recursive functions to perform linear search for key value in a given list. /
includes
Void linearsearch(int val, int n, int arr[*]);
int linear_search_re(int val, int n, int arr[*]);
Void main() 6.{
int val , i, n, poss ;
Print("enter the size of an array:");
Scan f("%d",&n) ;
int arr [n];
Print if("\n enter theelements :") ;
for(i=0; i<n;i++)
{
Scan f ("%d" ,&arr[i]);
}
Print f("\n enter the value to
Search: ") ;
Scan f("%d" , & val) ;
Print f ( "\n\n searching of an Element using non - recursive Function ") ;
Print f("\n ---------------------------------") ;
linear_ search(val,n,arr);
Print f("\n\n searching of an Element using recursive Function") ;
Print f ("\n-------------------------------");
Pos=linear_search_re(val,n,arr);
if(pos!=0)
Printf("\n element%d is Found at %d position",val, Pos) ;
else
Print f("\n%d is not found in the array",val);
}
Void linear_search(int val, int n, int arr[n])
{
int i, pos=-1;
for( i=0;i<n; i++)
{
if(arr[i]==val)
{
Pos= i ;
Print f("\n %d is
found in the array at position%d",val, i+1);
}
}
if( poss==-1)
}
int linear search re( int val, int n,int arr[n])
{
if( n>=0)
{
if (arr[n-1]==val)
Return n ;
else
return linear _ Search_re(val,n-1 ,arr) ;
}
}