How to write a C Program to search an element using linear search or binary search (menu driven program) in C Programming Language ?
This C Program to search an element using linear search or binary search (menu driven program).
Solution:
- #include<stdio.h>
- void main()
- {
- int a[25],beg,item,last,n,num,i,ch,mid,f=0;
- printf("menu\n");
- printf("\n 1.linear search");
- printf("\n 2.binary search");
- printf("\n enter the choice");
- scanf("%d",&ch);
- if(ch==1)
- {
- printf("\n enter the number of elements in the array");
- scanf("%d",&n);
- printf("\n enter the sorted array");
- for(i=0;i<n;i++)
- scanf("%d",&a[i]);
- printf("\n enter the item to be searched");
- scanf("%d",&item);
- for(i=0;i<n;i++)
- {
- if(a[i]==item)
- {
- printf("\n item found at position%d",i+1);
- break;
- }
- }
- if(i==n)
- printf("\n item not found");
- }
- if(ch==2)
- {
- printf("\nenter the number of elements in the array");
- scanf("%d",&n);
- printf("enter the sorted array");
- for(i=0;i<n;i++)
- scanf("%d",&a[i]);
- printf("item to be searched");
- scanf("%d",&item);
- last=n-1;
- mid=(beg+last)/2;
- while(beg<=last)
- {
- if(item==a[mid])
- {
- printf("\n item found at position %d",mid+1);
- break;
- }
- else if(a[mid]>item)
- last=mid-1;
- else beg=mid+1;
- mid=(beg+last)/2;
- }
- }
- }
Learn More :
Binary Search
Linear Search
Element
- Given a numerical value, check if at least one of the elements of the vector is equal to the numerical value if so, say where in the negative case, say that there is no.
- C program to Given a vector (integer or real), determine what is the maximum value of element what is the position in which this element is located
- Write the procedure , which is one of a sum , a product and a geometric average in the panel for the NxM elements are located on opposite diagonal and main diagonal . Remind ! Counting only odd elements !
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program Array NxM Elements Geometric/Arithmetic
- C Program To Returns the nth element of the Fibonacci sequence.
- Fibonacci Multi Threaded C Program To Print Element Of Fibonacci Series
- C Program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C program to reverse an array elements using Dynamic Memory Allocation
- C Program to calculate the sum of elements of upper triangle of a n*n matrix using DMA
- C Program to accept n numbers from user store these numbers into an array & reverse an array elements using function
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- Calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation
- Calculate sum of non-diagonal element in m*n matrix C Program
- Calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation
- C Program to Implemention Bubble Sort using array
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Creation and Display of Elements in Both Forward and Reverse Direction in a Doubly Linked List
Search
- Dictionary Word Search C Program
- C Depth First Search
- C Program To Search A Number Inside The Array
- C Program To Write Data In A File And Search Data From File
- C Program Performs a search and replace for a specified target and replacement string
- C Program Recursive function that searches for a given file in a given folder
- Pre Order, Post order, In order Implement Binary Tree using linked list
- BST Tree in C
- C Program Search Function For Sequence
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list