C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
#include //In easy way Binary search and replacement through recursion Cheema's idea int new_array(int *a , int j ,int n); int binary(int *a, int i, int search ,int temp); int main() {int i , a[20], search , temp , n, j=1; printf("how long u want an array\n "); scanf("%d",&n); printf("\nEnter array elements:\n"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } printf("Enter value for search"); scanf("%d",&search); printf("Enter new element for replacement at ur search\n"); scanf("%d",&temp);
binary(a , i, search, temp); new_array(a , j, n); getch(); } int binary(int *a, int i, int search, int temp) { if(i==0) { return 0; } if(a[i]==search) {printf("\nfound at location %d\n",i); a[i]=temp; printf("\nNew element added to array at location %d and its value is %d",i, a[i]); } i--; binary(a , i ,search, temp); } int new_array(int *a , int j ,int n) { if(j==n+1) { return; } printf("\nNew array is %d \n",a[j]);
if(l<=u){ mid=(l+u)/2; if(m==a[mid]){ c=1; } else if(m<a[mid]){ return binary(a,n,m,l,mid-1); } else return binary(a,n,m,mid+1,u); } //error else //error return c; it will search if and only if the given input are in ascending order
its just a simple search from last list(number) to first list(number).... for binary search the list or array should be in ascending order and we will search the number from mid term....
/*binary search using recursion*/ int binary_search(int ptr[], int item, int low, int high){ int mid = 0; if (low <= high){ mid = (low + high) / 2; if (ptr[mid] == item) return 1; else if (low == high) return 0; else if (ptr[mid] < item) binary_search(ptr, item, mid + 1, high); else if (ptr[mid] > item) binary_search(ptr, item, low, mid - 1); } else return 0; }
int main(){ int arr[] = { 10, 20, 30, 40, 50, 60, 70 }; int search, found=0; printf("Enter the number to be searched: "); scanf("%d",&search); found = binary_search(arr, search, 0, 6); if (found == 1) printf("found\n"); else printf("not found\n"); return 0; }
16 comments:
there are 1 error
pls check it at first
please provide prototype for the binary function before declaring main:
int binary(int[],int,int,int,int);
it is waste pass "n" (size of the array) as argument at calling binary() in main and also in recursive calls.........
just check it...
#include //In easy way Binary search and replacement through recursion Cheema's idea
int new_array(int *a , int j ,int n);
int binary(int *a, int i, int search ,int temp);
int main()
{int i , a[20], search , temp , n, j=1;
printf("how long u want an array\n ");
scanf("%d",&n);
printf("\nEnter array elements:\n");
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]); }
printf("Enter value for search");
scanf("%d",&search);
printf("Enter new element for replacement at ur search\n");
scanf("%d",&temp);
binary(a , i, search, temp);
new_array(a , j, n);
getch();
}
int binary(int *a, int i, int search, int temp)
{
if(i==0) { return 0; }
if(a[i]==search)
{printf("\nfound at location %d\n",i);
a[i]=temp;
printf("\nNew element added to array at location %d and its value is %d",i, a[i]);
}
i--;
binary(a , i ,search, temp);
}
int new_array(int *a , int j ,int n)
{ if(j==n+1) { return; }
printf("\nNew array is %d \n",a[j]);
j++;
new_array(a , j, n); }
yeah mothafocka..lets play sm shit ove here..
i am my cocaine and i was in the dork knoight
thanks alot for Naveed Cheema for correction in code...as it help me in understanding that code easily...
if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
} //error
else //error
return c;
it will search if and only if the given input are in ascending order
its not a binary search....
its just a simple search from last list(number) to first list(number).... for binary search the list or array should be in ascending order and we will search the number from mid term....
#include
/*binary search without using recursion*/
int main(){
int arr[] = {10, 20, 30, 40, 50, 60, 70};
int search;
int low, mid, high;
int found = 0;
printf("Enter the item to be search: ");
scanf("%d", &search);
low = 0;
high = (sizeof(arr) / sizeof(int)) - 1;
while (low <= high){
mid = (low + high) / 2;
if (arr[mid] == search){
found = 1;
break;
}
else if (arr[mid] > search)
high = mid - 1;
else if (arr[mid] < search)
low = mid + 1;
}
if (found == 1)
printf("Item found\n");
else
printf("Item not found\n");
return 0;
}
#include
/*binary search using recursion*/
int binary_search(int ptr[], int item, int low, int high){
int mid = 0;
if (low <= high){
mid = (low + high) / 2;
if (ptr[mid] == item)
return 1;
else if (low == high)
return 0;
else if (ptr[mid] < item)
binary_search(ptr, item, mid + 1, high);
else if (ptr[mid] > item)
binary_search(ptr, item, low, mid - 1);
}
else
return 0;
}
int main(){
int arr[] = { 10, 20, 30, 40, 50, 60, 70 };
int search, found=0;
printf("Enter the number to be searched: ");
scanf("%d",&search);
found = binary_search(arr, search, 0, 6);
if (found == 1)
printf("found\n");
else
printf("not found\n");
return 0;
}
this code has a error
👌
Just copy the function part and put it above the main(), it should work..
Post a Comment