Advertisement
Guest User

sachinfarts

a guest
Mar 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include<stdio.h>
  2. void main()
  3. {
  4. int r,c,key,i,j,chk=0,mid,lb,ub;
  5. printf(" enter the no: of rows and columns of the array\n");
  6. scanf("%d %d", &r,&c);
  7. int arr[r][c];
  8. printf(" Enter the elements into the array in sorted order\n");
  9. for(i=0;i<r;i++)
  10. for(j=0;j<c;j++)
  11. scanf("%d", &arr[i][j]);
  12. printf("\n\n The Array Entered is \n");
  13. for(i=0;i<r;i++)
  14. {
  15. for(j=0;j<c;j++)
  16. printf("%d\t", arr[i][j]);
  17. printf("\n");
  18. }
  19. printf("\n Enter the number to be searched\n");
  20. scanf("%d", &key);
  21. for(i=0;i<r;i++)
  22. {
  23. for(j=0;j<c;j++)
  24. {
  25. if(arr[i][j]==key)
  26. {
  27. chk=chk+1;
  28. break;
  29. }
  30. else
  31. continue;
  32. }
  33. if(chk==1)
  34. break;
  35. }
  36. if(chk==1)
  37. {
  38. printf(" Linearly determined that element exists in row %d\n",i+1);
  39. lb=0;
  40. ub=c-1;
  41. mid=(lb+ub)/2;
  42. while(lb<=ub)
  43. {
  44. if(key==arr[i][mid])
  45. {
  46. printf("\n %d found at (%d,%d)\n",key,(i+1),(mid+1));
  47. break;
  48. }
  49. else if(key<arr[i][mid])
  50. ub=mid-1;
  51. else if(key>arr[i][mid])
  52. lb=mid+1;
  53. mid=(lb+ub)/2;
  54. }
  55. }
  56. else
  57. printf("%d Not found In array\n",key);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement