Wednesday, 3 September 2014

CLASSWORK

#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

12 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. //FINAL SUBMISSION

      #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

      Delete
  2. What about the duplicacy of element in the array?
    The 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.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. where's ur program? its missing.....please upload the program with desired ouput that you commented..i.e; without duplicate elements...
    for 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'

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. code automatically modified after publish...:/

      Delete
  6. the previous comment was for ROHIT BISHT

    ReplyDelete
  7. //this one is for u Deepa
    #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;
    }

    ReplyDelete

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