Implement a C Code That Accept An Array of N Element and Search Integer.After Search Program Print Successful Search If Found and Unsuccessful Search if Not.
#include <stdio.h> int main() { int arr[20]; int i, low, mid, high, num, size,found=0; printf("Enter the size of an array\n"); scanf("%d", &size); printf("Enter the array elements\n"); for(i = 0; i < size; i++) { scanf("%d", &arr[i]); } printf("Search For:\n"); scanf("%d", &num); /* search begins */ low = 0; high = (size - 1); for(i=0;i<size;i++) { mid = (low + high)/2; if(num == arr[mid]) { printf("SUCCESSFUL SEARCH\n"); found=1; break; /*To Get Out The for Loop When The Number is Found */ } else if(num < arr[mid]){ high = mid - 1; } else low = mid + 1; } if(found==0){ /* For Loop Done Size Times and The Number Not Found still (Found==0) */ printf("UNSUCCESSFUL SEARCH\n"); } return 0 ; }
0 Comment to "Search An Array"
Post a Comment