K & R C Programs Exercise 3-1.

K and R C, Solution to Exercise 3-1:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to implement binary Search. Binary search technique is simple searching technique which can be applied if the items to be compared are either in ascending order or descending order. The general idea used in binary search is similar to the way we search for the telephone number of a person in the telephone directory. Binary search is the divide and conquer strategy. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

/*C Program for Binary search */

#include<stdio.h>
int main() {

int n, a[30], item, i, j, mid, top, bottom;
printf("Enter how many elements you want:n");
scanf("%d", &n);
printf("Enter the %d elements in ascending ordern", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("nEnter the item to searchn");
scanf("%d", &item);
bottom = 1;
top = n;

do {
mid = (bottom + top) / 2;
if (item < a[mid])
top = mid - 1;
else if (item > a[mid])
bottom = mid + 1;
} while (item != a[mid] && bottom <= top);

if (item == a[mid]) {
printf("Binary search successfull!!n");
printf("n %d found in position: %dn", item, mid + 1);
} else {
printf("n Search failedn %d not foundn", item);
}
return 0;
}
Read more Similar C Programs
C Basic
C Search Algorithms
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to implement Towers of Hanoi and Binary Search

C Recursion:
C Program to implement Towers of Hanoi and Binary Search using the Recursion method. Recursive functions solves the complexity of the problem by calling the function again and again itself. Using recursive methods we can save execution time and memory. In this program we have two recursive functions for Binary search and the Tower of Hanoi problem. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
main() {
int n, a[50], key, opn, i, pos;

do {
clrscr();
printf(
" nn Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quitn");
scanf("%d", &opn);
switch (opn) {
case 1:
printf(" How Many Elements?");
scanf("%d", &n);
printf(" Read all the elements is ASC order n");
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf(" Read the Key Elementn");
scanf("%d", &key);
pos = BS(a, key, 1, n);
if (pos)
printf(" Success: %d found at %d n", key, pos);
else
printf(" Falure: %d Not found in the list ! n", key);
break;
case 2:
printf("nn How Many Disks ?");
scanf("%d", &n);
printf("nn Result of Towers of Hanoi for %d Disks n", n);
tower(n, 'A', 'B', 'C');
printf("nn Note: A-> Source, B-> Intermediate, C-> Destinationn");
break;
case 3:
printf(" Terminating n");
break;
default:
printf(" Invalid Option !! Try Again !! n");
}
printf(" Press a Key. . . ");
getch();
} while (opn != 3);
}

int BS(int a[], int key, int low, int high) {
int mid;

if (low > high)
return 0; /* failure */
else {
mid = (low + high) / 2;
if (a[mid] == key)
return mid; /* Success */
if (key < a[mid])
return (BS(a, key, low, mid - 1));
return (BS(a, key, mid + 1, high));
}
}

tower(int n, char src, char intr, char dst) {
if (n > 0) {
tower(n - 1, src, dst, intr);
printf("Move disk %d from %c to %c n", n, src, dst);
tower(n - 1, intr, src, dst);
}
}

Read more Similar C Programs

Data Structures


C Recursion

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

Binary search in C

C Program to implement Binary search. Binary search technique is simple searching technique which can be applied if the items to be compared are either in ascending order or descending order. The general idea used in binary search is similar to the way we search for the telephone number of a person in the telephone directory. Binary search is the divide and conquer strategy.Read more about C Programming Language.
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* This program was originally published at
* http://www.c-program-example.com/2011/09/c-program-for-binary-search.html
* Happy Coding
***********************************************************/
/*C Program for Binary search */
#include<stdio.h>
int main() {
int n, a[30], item, i, j, mid, top, bottom;
printf("Enter how many elements you want:\n");
scanf("%d", &n);
printf("Enter the %d elements in ascending order\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nEnter the item to search\n");
scanf("%d", &item);
bottom = 1;
top = n;
do {
mid = (bottom + top) / 2;
if (item < a[mid])
top = mid - 1;
else if (item > a[mid])
bottom = mid + 1;
} while (item != a[mid] && bottom <= top);
if (item == a[mid]) {
printf("Binary search successfull!!\n");
printf("\n %d found in position: %d\n", item, mid + 1);
} else {
printf("\n Search failed\n %d not found\n", item);
}
return 0;
}
view raw binary_search.c hosted with ❤ by GitHub

Read more Similar C Programs
C Basic
Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com