Close Back

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *		A program on linear search                                 *
 *              ~~~~~~~~~~~~~~~~~~~~~~~~~~                                 *
 *				--> Author Rajesh _ThE gReAt               *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<rajesh.h>

main()
{
	int a[20],n,i,j,key,found = 0,loc[20];	/*loc == location */

	clrscr();

	printf("\nThis program performs a linear serach\n\n\n");

	do
	{
		printf("\nHow many numbers do you want to enter (Below 20)\t");
		scanf("%d",&n);
	}
	while(n <= 0 || n > 20);

	printf("\nEnter the numbers\t");

	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}

	printf("\nEnter the key to be searched\t");
	scanf("%d",&key);

	loc[0] = -1;
	j = 0;

	for(i=0;i<n;i++)
		if(a[i] == key)
		{
			found = 1;
			loc[j] = i;
			j++;
		}
	loc[j] = -1;

	if(found)
	{
		printf("\nThe number %d was found on the following location(s)\n",key);
		for(i=0;i<20 && loc[i] != -1;i++)
		{
			printf("%d\t",loc[i] + 1);
			delay(500);
		}
	}

	else
		printf("\nThe element not found");

	getch();
}