Question:10
(Linear search using functions) C Program to take input for “n” elements using an array. Further take input for an elements to search. Check and print whether element is present in the array or not, by passing values to function.
Sol:
#include<stdio.h> void search(int a[],int n,int n1) { int i,z=0; for(i=0;i<n;i++) { if(a[i]==n1) { z=1; break; } } if(z==0) printf("element is not found"); else printf("Element is found"); } int main() { int a[50],i,n,n1; printf("Enter total elements "); scanf("%d",&n); /* input */ for(i=0;i<n;i++) { printf("Enter element "); scanf("%d",&a[i]); } printf("Enter the element to search "); scanf("%d",&n1); /* to display elements */ search(a,n,n1); return(0); }
/* Output */ Case 1: Enter total elements 5 Enter element 25 Enter element 63 Enter element 54 Enter element 85 Enter element 96 Enter the element to search 54 Element is found Case 2: Enter total elements 5 Enter element 25 Enter element 63 Enter element 24 Enter element 85 Enter element 22 Enter the element to search 105 element is not found
Question:11
(Search, count and print position using functions) C Program to take input for “n” elements using an array. Further take input for an elements to search. Check and print whether element is present in the array or not by passing values to function.
Sol:
#include<stdio.h> void search(int a[],int n,int n1) { int i,z=0; for(i=0;i<n;i++) { if(a[i]==n1) { z=z+1; printf("Element found at position %d\n",i+1); } } if(z==0) printf("element is not found"); else printf("Element is found %d times",z); } int main() { int a[50],i,n,n1; printf("Enter total elements "); scanf("%d",&n); /* input */ for(i=0;i<n;i++) { printf("Enter element "); scanf("%d",&a[i]); } printf("Enter the element to search "); scanf("%d",&n1); /* to display elements */ search(a,n,n1); return(0); }
/* Output */ Enter total elements 10 Enter element 25 Enter element 10 Enter element 3 Enter element 6 Enter element 10 Enter element 45 Enter element 10 Enter element 9 Enter element 25 Enter element 45 Enter the element to search 10 Element found at position 2 Element found at position 5 Element found at position 7 Element is found 3 times
Question:12
C Program to take input for “n” elements using an array. Further take input for an elements to search. Check and print whether element is present in the array or not by passing values to function. (Binary search using functions)
Sol:
/* binary search: condition: the numbers should be sorted */ /* program using functions */ #include<stdio.h> void binary_search(int a[],int n,int x) { int first,last,middle,pos; /* to search the array */ first=0; pos=-1; last=n-1; while((first<=last) && (pos==-1)) { middle=(first+last)/2; if (a[middle]==x) pos=middle+1; else if (x>a[middle]) first=middle+1; else last=middle-1; } if (pos>-1) printf("the element is found at %d position\n",pos); else printf("the element is not found\n"); } int main() { int a[20],i,n,pos,x,j,t; /* clrscr(); */ printf("enter the size of the array "); scanf("%d",&n); printf("enter elements of the array in sorted order\n"); for(i=0;i<n;i++) { printf("enter the %d element ",i); scanf("%d",&a[i]); } printf("enter the element to search "); scanf("%d",&x); binary_search(a,n,x); return 0; }
/* Output */ enter the size of the array 5 enter elements of the array in sorted order enter the 0 element 10 enter the 1 element 25 enter the 2 element 36 enter the 3 element 45 enter the 4 element 65 enter the element to search 25 the element is found at 2 position