Wednesday, 3 September 2014

final submition of lab prog(Searching an element and their posion in an array)

#include<stdio.h>
#include<conio.h>
main()
{
      int list[100],n,i,x,p;
     
printf("\n Enter size of the list....");
scanf("%d",&n);
printf("\n Enter the elements to the list....\n");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
p=0;
   
printf("\n Enter the element to be searched ...... ");
scanf("%d",&x);
for(i=0;i<n;i++)
  {
      if(list[i]==x)
        p=i+1;
  }
  if(p>0)
  printf("\n The list of the element %d is found at position %d",x,p);
  else      
  printf("\n The element %d is not found,TRY AGAIN",x);

getch();
}
                                       
                                       
                                       
                                       
                                       
                                       
                                       

1 comment:

  1. The code doesn't deal with duplicacy of elements.
    Try This:
    Input array size: 4
    Input array elements: {2,2,2,2}
    Element has to find: 2
    Output: position of element 2 is 4
    But the position of element 2: 1st, 2nd, 3rd also

    ReplyDelete

Note: only a member of this blog may post a comment.