/* 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.)
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.
Algorithm can be defined as an interpretable, finite set of instructions for dealing with contigencies and accompanying task that has recognizable end-points for given inputs. It is a tool for solving a well computational problem. A recursive algorithm is one which calls itself.
The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }
for two positive integers: public static int gcd(int i1, int i2) { // using Euclid's algorithm int a=i1, b=i2, temp; while (b!=0) { temp=b; b=a%temp; a=temp; } return a; }
write a program in C that prompts the user with the following lines: a) Add two integers c) Compare two integers for the larger t) Test an integers for odd or even q) Quit
i love u darling
bytes integers long integers short integers word double word strings
What alphabet are you using, and how are the natural numbers represented in this alphabet?
addition
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.
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.
yes
Yes.
The inverse operation of division of integers is multiplication. When you divide one integer by another, you can reverse that operation by multiplying the quotient by the divisor to retrieve the original dividend. For example, if you divide 12 by 3 to get 4, you can multiply 4 by 3 to get back to 12.
add subtract divide multiplication
1) addition
The answer depends on what the operation is.