Thread: help me !

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Question help me !

    Code:
          
    
    #include<stdio.h>
          #include<conio.h>
          void main()
          {
          int a[100],temp,x,n,l,r,mid,c,i,j;
          clrscr();
          printf("enter the size");
          scanf("%d",&n);
          printf("enter the values");
          for(i=0;i<n;i++)
          {
          scanf("%d",&a[i]);
          }
          printf("enter the value to searched");
          scanf("%d",&x);
          printf("enter ur choice 1.linaer 2.binary 3.exit\n");
          scanf("%d",&c);
          switch(c)
          {
          case 1:
    
          for(i=0;i<n;i++)
          {
          if(a[i]==x)
          {
          printf("element is found at %d",i+1);
          }
          }
          break;
          case 2:
          for(i=0;i<n;i++)
          {
          for(j=0;j<n;j++)
          {
          if(a[i]<a[j])
          {
          temp=a[i];
          a[i]=a[j];
          a[j]=temp;
          }
          }
          }
          printf("sorted array ");
          for(i=0;i<n;i++)
          {
          printf("\t%d\t",a[i]);
          }
          mid=(l+r)/2;
          while(a[mid]!=x && l<r)
          {
          if(x>a[mid])
          {
          l=mid+1;
          }
          else
          {
          r=mid-1;
          }
          mid=(l+r)/2;
          if(l>=r)
          {
          printf("element not found");
          }
          else
          {
          printf("element found at %d",mid+1);
          }
          }
          break;
          case 3:
          exit(0);
          break;
          default:
          printf("invalid");
          break;
          }
          getch();
          }
    this is a binary search program ,,,,plz make correction in this program do not offer me another binary search progg. and suggest me debugging steps and also explain my fault....it is executing upto "array sorting" efficiently but not after that i.e binary search part !!

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. Do you really think, it is sorting efficiently???
    2. What is l and r??? No initialization... so expecting garbage or 0 for both.
    3.
    Code:
    if(l>=r)
          {
          printf("element not found");
          }
          else
          {
          printf("element found at %d",mid+1);
          }
    write this outside your while loop.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    thnks for da ans .....but l=lower & r=higher index value....
    yes it is sorting efficiently .....coz i had used a binary sorting out there......
    after putting ur code it says element not found but which is a bug ...coz element is there still system cound'nt find it ?????
    entire concept is right bt still this bug is driving me nuts dude....
    & 1 more thing lower & higher index value i.e l & r ......i had not initialize coz i want input frm user (dynamically) ..((

    thnx ....anyways...finally i had fix the dam thing...god feels releif now !!)))
    Last edited by naved; 03-25-2011 at 11:17 AM.

Popular pages Recent additions subscribe to a feed