answersLogoWhite

0


Best Answer

/* Write C programs that use both recursive and non recursive functions to perform the following searching operation for a Key value in a given list of integers : i) Linear search */

#include <stdio.h>

#define MAX_LEN 10

void l_search_recursive(int l[],int num,int ele);

void l_search(int l[],int num,int ele);

void read_list(int l[],int num);

void print_list(int l[],int num);

void main()

{

int l[MAX_LEN], num, ele;

int ch;

printf("======================================================");

printf("\n\t\t\tMENU");

printf("\n=====================================================");

printf("\n[1] Linary Search using Recursion method");

printf("\n[2] Linary Search using Non-Recursion method");

printf("\n\nEnter your Choice:");

scanf("%d",&ch);

if(ch<=2 & ch>0) {

printf("Enter the number of elements :");

scanf("%d",&num);

read_list(l,num);

printf("\nElements present in the list are:\n\n");

print_list(l,num);

printf("\n\nElement you want to search:\n\n");

scanf("%d",&ele);

switch(ch) {

case 1:

printf("\n**Recursion method**\n");

l_search_recursive(l,num,ele);

getch();

break;

case 2:

printf("\n**Non-Recursion method**\n");

l_search_nonrecursive(l,num,ele);

getch();

break;

}

}

getch();

}

/*end main*/

/* Non-Recursive method*/

void l_search_nonrecursive(int l[],int num,int ele)

{

int j, f=0;

for(j=0; j<num; j++) if( l[j] ele) {

printf("\nThe element %d is present at position %d in list\n",ele,num);

f=1;

}

else {

if((num==0) && (f==0)) printf("The element %d is not found.",ele);

else l_search(l,num-1,ele);

}

getch();

}

void read_list(int l[],int num) {

int j;

printf("\nEnter the elements:\n");

for(j=0;j<num;j++) scanf("%d",&l[j]);

}

void print_list(int l[],int num) {

int j;

for(j=0;j<num;j++) printf("%d\t",l[j]);

}

(Note: Edited for clarity.)

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to use recursive and non-recursive functions to perform linear search operation for a key value in a given set of integers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a c program to find GCD of two given integers by using both recursive n non recursive functions?

i love u darling


What are the types of operands and operation?

bytes integers long integers short integers word double word strings


Give a recursive definition for the language L of positive integers divisible by 2 or 7?

What alphabet are you using, and how are the natural numbers represented in this alphabet?


Under which operation is the set of odd integers closed?

addition


Is addition of integers commutative?

Yes it is : a + b = b + a for all integers a and b. In fact , if an operation is called addition you can bet that it is commutative. It would be perverse to call an non-commutative operation addition.


Is the set of all negative integers closed for operation of addition?

yes


Is the set of all integers closed under the operation of multiplication?

Yes.


How do you write a C program to find the GCD of two given integers using non recursive functions?

Use the following function: int gcd (int a, int b) { while (b != 0) { a %= b; a ^= b ^= a ^= b; } return a; } Note that a ^= b ^= a ^= b is an efficient method of swapping two values.


How are addition and subtraction of integers related?

Addition and subtraction are inverse functions.


Which operation used to close the set of positive even integers?

1) addition


What are two same integers that would end up with the same answer?

The answer depends on what the operation is.


Is the set of positive integers a commutative group under the operation of addition?

No. It is not a group.