#include<stdio.h>
int main()
{
int list[100],n,i,x,p,found=0;
printf("\n Enter the size of the list:"); //entering the size of the array
scanf("%d",&n);
printf("\n Enter the elements:\n"); //entering the elements of the array
for(i=0;i<n;i++)
{
scanf("%d",&list[i]);
}
p=0;
printf("\n Enter the element to be searched:"); //entering the KEY element to be searched
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(list[i]==x) //if KEY element is found then
{
p=i; //store the index value of the array in another variable
found=1;
}
}
if(found>0)
{
printf("\n The position of element %d is %d",x,p+1); //print the KEY element along with its position in the array
}
else
{
printf("\n Try Again");
}
return 0; //end of program
}
//OUTPUT
Enter the size of the list: 5
Enter the elements:
54
89
72
36
45
Enter the element to be searched: 89
The position of element 89 is 2
int main()
{
int list[100],n,i,x,p,found=0;
printf("\n Enter the size of the list:"); //entering the size of the array
scanf("%d",&n);
printf("\n Enter the elements:\n"); //entering the elements of the array
for(i=0;i<n;i++)
{
scanf("%d",&list[i]);
}
p=0;
printf("\n Enter the element to be searched:"); //entering the KEY element to be searched
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(list[i]==x) //if KEY element is found then
{
p=i; //store the index value of the array in another variable
found=1;
}
}
if(found>0)
{
printf("\n The position of element %d is %d",x,p+1); //print the KEY element along with its position in the array
}
else
{
printf("\n Try Again");
}
return 0; //end of program
}
//OUTPUT
Enter the size of the list: 5
Enter the elements:
54
89
72
36
45
Enter the element to be searched: 89
The position of element 89 is 2
This comment has been removed by the author.
ReplyDelete//FINAL SUBMISSION
Delete#include
int main()
{
int list[100],n,i,x,p,found=0; /*initialised a new variable to keep track whether key element is found or not*/
printf("\n Enter the size of the list:"); //entering the size of the array
scanf("%d",&n);
printf("\n Enter the elements:\n"); //entering the elements of the array
for(i=0;i0)
{
printf("\n The position of element %d is %d",x,p+1); /*print the KEY element along with its position in the array*/
}
else
{
printf("\n Try Again");
}
return 0; //end of program
}
//OUTPUT
Enter the size of the list: 5
Enter the elements:
89
46
31
96
21
Enter the element to be searched: 89
The position of element 89 is 1
What about the duplicacy of element in the array?
ReplyDeleteThe position of any element in the array is related to the index value of the element in the array if is present in the array. Your code give the normal position of the element not the index value in the array.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWhere are the comments????? :/
ReplyDeleteu r late....rohit
Deletewhere's ur program? its missing.....please upload the program with desired ouput that you commented..i.e; without duplicate elements...
ReplyDeletefor eg.if no of elements=5
and the elements are 1 2 2 3 5..let the searched element be 2...than the output be like 'the location of 2 is 1 & 2'
This comment has been removed by the author.
Deletecode automatically modified after publish...:/
Deletethe previous comment was for ROHIT BISHT
ReplyDelete//this one is for u Deepa
ReplyDelete#include
#include"conio.h"
float main()
{
int list[100],n,i,x,t,p[100],j;
printf("Enter the size of list: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i=0)
{
printf("\t%2d",p[i]+1);
}
else if(p[i]<=0)
{
printf("\nNot found, Try again");
}
i++;
}while(i<j);
getch();
return 0;
}