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

K & R C Programs Exercise 2-10.

K and R C, Solution to Exercise 2-10:
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 Function lower, which converts the lower case, with a conditional expression instead of if-else.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
***********************************************************/

int lower(int c)
{
return c >= 'A' && c<= 'Z' ? c + 'a' - 'A': c;
}
Read more Similar C Programs
C Basic

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

K & R C Programs Exercise 2-9.

K and R C, Solution to Exercise 2-9:
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.
In C two complement number system, x &= (x-1) deletes the rightmost one bit in x.
We take (x-1) and add 1 to it to produce x. The rightmost 0-bit of x-1 changes to 1 in the result x. Therefore, the rightmost 1-bit of x has a corresponding 0-bit in x-1. This is why x & (x-1), in a two’s complement number system, will delete the rightmost 1-bit in x. 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
***********************************************************/
int bitcount(unsigned x)
{
int b;
for(b = 0;x != 0;x &= x-1)
++b;
return b;
}
Read more Similar C Programs
C Basic

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

K & R C Programs Exercise 2-8.

K and R C, Solution to Exercise 2-8:
 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 that returns the value of the integer x rotated to the right by n bit positions. 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
***********************************************************/

unsigned rightroot(unsigned x, int n)
{
int wordlength(void);
int rbit; //rightmost bit

while(n-->0){
rbit=(x & 1)<<(wordlength()-1);
x=x>>1;
x=x|rbit;
}
return x;
//word length computes the wor lengt of machine
int wordlength()
{
int i;
unsigned v = (unsigned~0;
for(i = 1;(v = v >> 1) > 0;i++)
;
return i;
}
//main function to test the program,, you can try in different ways!
#include <stdio.h>

int main(void)
{
unsigned x;
int n;

for(x = 0; x < 200; x += 25)
for(n = 1; n < 8; n++)
printf("%u, %d: %un", x, n, rightrot(x, n));
return 0;
}

Read more Similar C Programs
C Basic

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

K & R C Programs Exercise 2-7.

K and R C, Solution to Exercise 2-7:
 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 function invert(x, p, n) that returns x with the n bits that begin at position p inverted(i.e, 1 changed into 0 and vice versa), leaving the others unchanged.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
***********************************************************/

unsigned invert(unsigned x, int p, int n)
{
return x ^ (~(~0U << n) << p);
}

/* Program for testing */

#include <stdio.h>

int main(void)
{
unsigned x;
int p, n;

for(x = 0; x < 500; x += 49)
for(n = 1; n < 8; n++)
for(p = 1; p < 8; p++)
printf("%u, %d, %d: %un", x, n, p, invert(x, n, p));
return 0;
}

Read more Similar C Programs
C Basic

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