Slip 5_1, Slip 14_1: .Create a random array of n integers. Accept a value x from user and use linear search algorithm to check whether the number is present in the array or not and output the position if the number is present.

 Solution :
#include<stdio.h>
void linearsearch(int a[10],int n,int sr)
{ int i,p,cnt=0;
 for(i=0;i<n;i++)
 {
 if(a[i]==sr)
 { p=i; //store position
 cnt++;
 break;
 }
 }
 if(cnt>=1)
 printf("element found at %d position",p);
 else
 printf("element NOT found ");
}
main()
{
 int n,i,sr,a[10];
 printf("enter how many values");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
 printf("enter values");
 scanf("%d",&a[i]);
 }
 printf("\n enter search element");
 scanf("%d",&sr);
 linearsearch(a,n,sr);
}

Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 10_1 ,22_1: Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the init(Q), add(Q) and peek(Q) operations. Write a program that includes queue library and calls different queue operations