Write a program that will find the location of a giver item in an array using Linear Search.
Write a program that will find the location of a giver item
in an array using Linear Search.
Problem Name: Write a program that will find the
location of a giver item in an array using Linear Search.
Algorithm: Linear Search (DATA, N, ITEM, LOC)
Step-01: Set DATA[N+1] = ITEM.
Step-02: Set LOC = 1.
Step-03: Repeat
while DATA[LOC] ≠ ITEM.
Set
LOC = LOC + 1.
Step-04: If Loc = N + 1 then
Set
LOC =0.
Step-05: Exit.
Code:
#include<stdio.h>
void main(){
int
a[50],i,n,item;
printf("How many element: ");
scanf("%d",&n);
printf("\n\nEnter the %d element: \n",n);
for
(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n\nEnter element you want to search: ");
scanf("%d",&item);
for(i=0;i<n;i++){
if(a[i]==item){
printf("\n%d is found at location %d\n",item,i+1);
break;
}}
if(i==n)
printf("\n%d is not found in array.",item);
}
Input:
Output:
লেবেলসমূহ: Data Structure
0টি মন্তব্য:
একটি মন্তব্য পোস্ট করুন
এতে সদস্যতা মন্তব্যগুলি পোস্ট করুন [Atom]
<< হোম