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

C Program to add two polynomials using structures.

C Program to add two polynomials using structures. Structure is a c composite data type, in which we can define all the data types under the same name or object. Size of the Structure is the size of all data types, plus any internal padding. the key word “struct” is used to declare the structure. A Polynomial is a mathematical expression involving a sum of powers in one or more variables multiplied by coefficients. 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 >
#include < conio.h >
#define MAX 20

struct addpolynomial {
int exp, coef;
};

void main() {
struct addpolynomial p1[MAX], p2[MAX], p3[MAX];
int max1, max2, max3;
clrscr();
printf("nEnter first addpolynomial : ");
max1 = read_addpolynomial(p1);
printf("nEnter second addpolynomial : ");
max2 = read_addpolynomial(p2);
max3 = add_addpolynomial(p1, p2, p3, max1, max2);
printf("nFirst addpolynomial is ");
print_addpolynomial(p1, max1);
printf("nSecond addpolynomial is ");
print_addpolynomial(p2, max2);
printf("n The resultant addpolynomial after addition is");
print_addpolynomial(p3, max3);
}

//function to read polynomial
int read_addpolynomial(struct addpolynomial p[]) {
int i, texp;
i = 0;
printf("nEnter exp ( use -1 to exit) : ");
scanf("%d", &texp);
while (texp != -1) {
p[i].exp = texp;
printf("nEnter coef : ");
scanf("%d", &p[i].coef);
i++;
printf("nEnter exp ( use -1 to exit) : ");
scanf("%d", &texp);
}
return (i);
}

//function to print polynomial
int print_addpolynomial(struct addpolynomial p[], intMAX1) {
int i;
for (i = 0; i < max1; i++)
printf("%+dX%d ", p[i].coef, p[i].exp);
return;
}

//function to ad polynomials
int add_addpolynomial( p1, p2, p3, max1, max2)
struct addpolynomial p1[], p2[], p3[];
intMAX1, max2;
{
int i,j,k;
i = j = k = 0;
while ( i <max1 && j <max2)
{
if( p1[i].exp > p2[j].exp)
{
p3[k] = p1[i];
k++;
i++;
}
else
if( p1[i].exp < p2[j].exp)
{
p3[k] = p2[j];
k++;
j++;
}
else
{
p3[k].exp = p1[i].exp;
p3[k].coef = p1[i].coef + p2[j].coef;
i++;
j++;
k++;
}
}
while( i <max1 )
{
p3[k] = p1[i];
k++;
i++;
}
while( j <max2 )
{
p3[k] = p2[j];
k++;
j++;
}
return(k);
}

Array In C

C Data Structures

Simple C Programs

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-1.

K and R C, Solution to Exercise 2-1:
C program to determine the ranges of char, short, int and long variables, both signed and unsigned, by printing appropriate values from standard headers and by direct computation.the ANSIstandard for C specifies that ranges be defined in <limits.h>. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. 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>
#include <limits.h>

int
main ()
{
printf("Range of Char %dn", CHAR_BIT);
printf("Range of Char Max %dn", CHAR_MAX);
printf("Range of Char Min %dn", CHAR_MIN);
printf("Range of int min %dn", INT_MIN);
printf("Range of int max %dn", INT_MAX);
printf("Range of long min %ldn", LONG_MIN);
printf("Range of long max %ldn", LONG_MAX);
printf("Range of short min %dn", SHRT_MIN);
printf("Range of short max %dn", SHRT_MAX);
printf("Range of unsigned char %un", UCHAR_MAX);
printf("Range of unsigned long %lun", ULONG_MAX);
printf("Range of unsigned int %un", UINT_MAX);
printf("Range of unsigned short %un", USHRT_MAX);

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

C Program for Simple ASC order Priority QUEUE Implementation

Data structures using C,Priority QUEUE is a abstract data type in which the objects are inserted with respect to certain priority. In this program, we created the simple ascending order priority queue, here items are inserted in ascending order. 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
***********************************************************/
#define SIZE 5 /* Size of Queue */
int PQ[SIZE],f=0,r=-1; /* Global declarations */

PQinsert(int elem)
{
int i; /* Function for Insert operation */
if( Qfull()) printf("nn Overflow!!!!nn");
else
{
i=r;
++r;
while(PQ[i] >= elem && i >= 0) /* Find location for new elem */
{
PQ[i+1]=PQ[i];
i--;
}
PQ[i+1]=elem;
}
}

int PQdelete()
{ /* Function for Delete operation */
int elem;
if(Qempty()){ printf("nnUnderflow!!!!nn");
return(-1); }
else
{
elem=PQ[f];
f=f+1;
return(elem);
}
}

int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}

int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}

display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(" n Empty Queuen");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",PQ[i]);
printf("<-Rear");
}
}

main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf("n ### Priority Queue Operations(ASC order) ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("nnRead the element to be Inserted ?");
scanf("%d",&elem);
PQinsert(elem); break;
case 2: elem=PQdelete();
if( elem != -1)
printf("nnDeleted Element is %d n",elem);
break;
case 3: printf("nnStatus of Queuenn");
display(); break;
case 4: printf("nn Terminating nn"); break;
default: printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}


Read more Similar C Programs
Data Structures

Learn C Programming

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 implementing two Stacks on Single Array.

Data structures using C, Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are aded or deleted from only one end, i.e. top of the stack. In this program, we implement the two stacks using the single array. 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
***********************************************************/
#define SIZE 10 /* Size of Stack */
int s[SIZE],top[3]={0,-1,SIZE};
/* Global declarations */
push(int elem,int stno)
{
int pos; /* Function for PUSH operation */
if( Sfull()) printf("nn Overflow!!!!nn");
else
{
if(stno==1) pos= ++top[stno];
else pos=--top[stno];
s[pos]=elem;
}
}


int pop(int stno)
{ /* Function for POP operation */
int elem,pos;
if(Sempty(stno)){ printf("nnUnderflow!!!!nn");
return(-1); }
else
{
pos=top[stno];
elem=s[pos];
if(stno == 1)top[stno]--;
else
top[stno]++;
return(elem);
}
}

int Sfull()
{ /* Function to Check Stack Full */
if(top[1] == top[2]-1) return 1;
return 0;
}

int Sempty(stno)
{
/* Function to Check Stack Empty */
switch(stno)
{
case 1: if(top[1] == -1) return 1; else return 0;
case 2: if(top[2] == SIZE) return 1;else return 0;
}
}

display(int stno)
{ /* Function to display status of Stack */
int i;
if(Sempty(stno)) printf(" n Empty Stackn");
else
{
if(stno == 1)
{
for(i=0;i<=top[stno];i++)
printf("%dn",s[i]);
printf("^Top");
}
else
{
for(i=SIZE-1;i>=top[stno];i--)
printf("%dn",s[i]);
printf("^Top");
}
}
}

main()
{ /* Main Program */
int opn,elem,stno;
do
{
clrscr();
printf("n ### Stack Operations ### nn");
printf("n Stack Number (1,2): ");
scanf("%d",&stno);
printf("n Press 1-Push, 2-Pop,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);

switch(opn)
{
case 1: printf("nnRead the element to be pushed ?");
scanf("%d",&elem);
push(elem,stno); break;
case 2: elem=pop(stno);
if( elem != -1)
printf("nnPopped Element is %d n",elem);
break;
case 3: printf("nnStatus of Stack %d nn",stno);
display(stno); break;
case 4: printf("nn Terminating nn"); break;
default: printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}


Read more Similar C Programs
Data Structures

Learn C Programming

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 1-23.

K and R C, Solution to Exercise 1-23: C program that removes the all comments from a C Program. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. 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"
void rcomment(int c);
void in_comment(void);
void echo_quote(int c);
main();
{
int c ,d;
while((c= getchar()) !=EOF)
rcomment(c);
return 0;
}
void rcomment(int c) {
int d;
if(c == '/')
if((d= getchar))=='*')
in_comment();
else if(d== '/') {
putchar(c);
rcomment(d);
} else {
putchar(c);
putchar(d);
}
else if(c=='' || c=='"')
echo_quote(c);

}
void in_comment() {
int c, d;
c = getchar();
d = getchar();
while (c!= '*' || d!!='/') {
c = d;
d = getchar();
}
}
void echo_quote(int c) {
int d;
putchar(c);
while ((d = getchar()) != c) {
putchar(d);
if (d == '\')
putchar(getchar());

}
putchar(d);
}
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 1-21.

K and R C, Solution to Exercise 1-21:
C Program to replace string of blanks by the minimum number of tabs and blanks to achieve the same spacing. K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. 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"
#define TABSIZE 6 /*Tab increment size*/
main() {
int c, no_blanks, pos, no_tabs;
no_blanks = 0; /*number of blamks required*/
no_tabs = 0; /* number of tabs required*/

for (pos = 1; (c = getchar()) != EOF; ++pos) {
if (c == ' ') {
if (pos % TABSIZE != 0)
++no_blanks;
else {
no_blanks = 0;
++no_tabs;
}
} else {
for (; no_tabs > 0; --no_tabs)
putchar('t');
if (c == 't')
no_blanks = 0;
else
for (; no_blanks > 0; --no_blanks)
putchar(' ');
putchar(c);
if (c == 'n')
pos = 0;
else if (c == 't')
pos = pos + (TABSIZE - (pos - 1) % TABSIZE);

}
}
}

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 1-20.

K and R C, Solution to Exercise 1-20:
C Program to replace the tabs by proper number of blanks. K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. 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"
#define TABSIZE 6 /*Tab increment size*/
main()
{
int c, no_blanks,pos;
no_blanks = 0; /*number of blamks required*/
pos = 1; /* position of the character*/
while((c=getchar()) != EOF){
if(c == 't'){
no_blanks = TABSIZE-(pos-1)%TABSIZE;
while(no_blanks>0){

putchar(' ');
++pos;
--no_blanks;
}
else if(c == 'n'){
putchar(c);
pos = 1;
} else {
putchar(c);
++pos;
}
}
}
}

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

C Program to generate Graph using grphics.h

C Program to Generate the graph sheet using the grphics.h library. To use graphics.h, we have to install the drivers in to the the system by using the initgraph() function. Here  we derive the graph of input sizes verses time taken for input sizes. x axis represents inputs(0,10000,20000,—-), y axis rep time(0,0.05,0.1,0.15—). 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"
#include "conio.h"
#include "graphics.h"
void main() {
int gd = DETECT, gm;
int y = 0, x = 10, m[20], k[20], n, a[20], i;
float b[20];
initgraph(&gd, &gm, "c:\tc\bgi");
printf("nntGenerating the Graphsnn");
printf("nEnter the no. of inputst");
scanf("%d", &n);
printf("nEnter the input sizes and corresponding time takenn");
for (i = 0; i < n; i++) {
printf("nEnter input sizet");
scanf("%d", &a[i]);
printf("nEnter time takent");
scanf("%f", &b[i]);
}
cleardevice();
//represents y axis
line(10, 0, 10, 400);
//represents x axis
line(10, 400, 600, 400);
while (y <= 400) {
line(0, y, 10, y);
y = y + 20;
}
while (x <= 600) {
line(x, 400, x, 410);
x = x + 20;
}
outtextxy(20, 440, "1unit=20 pixels , origin is (10,400)");
outtextxy(
20,
450,
"x axis represents inputs(0,10000,20000,----), yaxis rep time(0,0.05,0.1,0.15---)");
setcolor(5);
for (i = 0; i < n; i++) {
k[i] = (a[i] * 0.002);
m[i] = (400 - (b[i] * 400));
putpixel(k[i], m[i], 11);
}
for (i = 0; i < n - 1; i++)
line(k[i], m[i], k[i + 1], m[i + 1]);
getch();
}
Read more Similar C Programs
Learn C Programming

Number System

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 Interpolation Search

Interpolation search is an algorithm used for searching a given value in an ordered indexed array. Interpolation search is sometimes called as extrapolation search. For uni formally distributed data items Interpolation search is the best method. for example: library books directory. 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"
#include "stdlib.h"

#define MAX 200

int interpolation_search(int a[], int bottom, int top, int item) {

int mid;
while (bottom <= top) {
mid = bottom + (top - bottom)
* ((item - a[bottom]) / (a[top] - a[bottom]));
if (item == a[mid])
return mid + 1;
if (item < a[mid])
top = mid - 1;
else
bottom = mid + 1;
}
return -1;
}

int main() {
int arr[MAX];
int i, num;
int item, pos;

printf("nEnter total elements (num< %d) : ", MAX);
scanf("%d", &num);

printf("Enter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);

printf("nELEMENTS AREn: ");
for (i = 0; i < num; i++)
printf("%dt", arr[i]);

printf("nSearch For : ");
scanf("%d", &item);
pos = interpolation_search(&arr[0], 0, num, item);
if (pos == -1)
printf("nElement %d not foundn", item);
else
printf("nElement %d found at position %dn", item, pos);

return 0;
}
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!

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