100% Linear search
#include <stdio.h>
void main()
{
int p,numbered_packets[5],key,count=0;
/*let there be some packets to be sent by Indian postal department which are numbered with some codes.After receiving them, we wish to search for a particular numbered packet becoz all packets might have got jumbled up */
for(p=0;p<5;p++)
{
printf("\n enter the code of numbered_packets[%d]:",p);
scanf("%d",&numbered_packets[p]);
}
printf("\n enter the key code to be searched:");
scanf("%d",&key);
for(p=0;p<5;p++)
{
if(numbered_packets[p]==key)
{
printf("\n packet with desired key found");
exit(0);
}
count=count+1;
}
if(count==5)
printf("\n packet with desired key not found");
}
enter the code of numbered_packets[1]:7
enter the code of numbered_packets[2]:5
enter the code of numbered_packets[3]:2
enter the code of numbered_packets[4]:-1
enter the key code to be searched:4
enter the code of numbered_packets[1]:7
enter the code of numbered_packets[2]:5
enter the code of numbered_packets[3]:2
enter the code of numbered_packets[4]:-1
enter the key code to be searched:7
void main()
{
int p,numbered_packets[5],key,count=0;
/*let there be some packets to be sent by Indian postal department which are numbered with some codes.After receiving them, we wish to search for a particular numbered packet becoz all packets might have got jumbled up */
for(p=0;p<5;p++)
{
printf("\n enter the code of numbered_packets[%d]:",p);
scanf("%d",&numbered_packets[p]);
}
printf("\n enter the key code to be searched:");
scanf("%d",&key);
for(p=0;p<5;p++)
{
if(numbered_packets[p]==key)
{
printf("\n packet with desired key found");
exit(0);
}
count=count+1;
}
if(count==5)
printf("\n packet with desired key not found");
}
ఔట్పుట్ తెరమీద
enter the code of numbered_packets[0]:9enter the code of numbered_packets[1]:7
enter the code of numbered_packets[2]:5
enter the code of numbered_packets[3]:2
enter the code of numbered_packets[4]:-1
enter the key code to be searched:4
packet with desired key not found
మరోసారి Run చేస్తే ఔట్పుట్ తెరమీద
enter the code of numbered_packets[0]:9enter the code of numbered_packets[1]:7
enter the code of numbered_packets[2]:5
enter the code of numbered_packets[3]:2
enter the code of numbered_packets[4]:-1
enter the key code to be searched:7
packet with desired key found
/*తాళాలు వేయని గ్రామం ఏది అని అడిగితే విద్యార్థుల్లో ఒకరు (23-551) 'శనిసింగనాపూర్ అని చెప్పినారు*/
/*reference NPTEL lecture video on 'Linear Search': Problem Solving through Programming in C, IIT Kharagpur. Prof. Anupam Basu*/
కామెంట్లు
కామెంట్ను పోస్ట్ చేయండి
దయచేసి మీ సలహాలను సూచనలను స్పష్టంగా పేర్కొనగలరు. plz see that ur comments are 'acceptable' in a value based society.