C Program to implement Simpson method.

Write a C Program to implement Simpson method.
Simpson method is used for approximating integral of the function.
Simpson’s rule also corresponds to the 3-point Newton-Cotes quadrature rule.
In this program, We use the stack to implement the Simpson method. 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<math.h>

char post_fix[80];

float stack[80];

char stack1[80];

int top=-1,top1=-1;

float eval(char post_fix[], float x1);

void infix_post_fix(char infix[]);

main()

{

float x0, xn, h, s,e1,e2, e3;

char exp[80], arr[80];

int i,n,l=0;

clrscr();

printf("nEnter an expression: nn");

gets(exp);

puts("nnEnter x0, xn and number of sub-intervals: nn");

scanf("%f%f%d", &x0, &xn, &n);

h=(xn-x0)/n;

if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')

{

l=strlen(exp);

for(i=0;i<l-3; i++)

arr[0]=exp[i+3];

arr[i]=";

infix_post_fix(arr);

e1=eval(post_fix,x0);

e2=eval(post_fix,xn);

e3=4*eval(post_fix, x0+h);

s=log(e1)+log(e2)+log(e3);

for (i=3;i<=n-1;i+=2)

s+=4*eval(post_fix,x0+i*h)+2*eval(post_fix, x0+(i-1)*h);

}

else

{

infix_post_fix(exp);

s=eval(post_fix,x0)+eval(post_fix,xn)+4*eval(post_fix, x0+h);

for (i=3;i<=n-1;i+=2)

s+=4*eval(post_fix,x0+i*h)+2*eval(post_fix, x0+(i-1)*h);

}

printf("nnThe value of integral is %6.3fn",(h/3)*s);

return(0);

}

/*Inserting the operands in a stack. */

void push(float item)

{

if(top==99)

{

printf("ntThe stack is full");

getch();

exit(0);

}

else

{

top++;

stack[top]=item;

}

return;

}

/*Removing the operands from a stack. */

float pop()

{

float item;

if(top==-1)

{

printf("ntThe stack is emptynt");

getch();

}

item=stack[top];

top–;

return (item);

}

void push1(char item)

{

if(top1==79)

{

printf("ntThe stack is full");

getch();

exit(0);

}

else

{

top1++;

stack1[top1]=item;

}

return;

}

/*Removing the operands from a stack. */

char pop1()

{

char item;

if(top1==-1)

{

printf("ntThe stack1 is emptynt");

getch();

}

item=stack1[top1];

top1–-;

return (item);

}

/*Converting an infix expression to a postfix expression. */

void infix_post_fix(char infix[])

{

int i=0,j=0,k;

char ch;

char token;

for(i=0;i<79;i++)

post_fix[i]=' ';

push1('?');

i=0;

token=infix[i];

while(token!=")

{

if(isalnum(token))

{

post_fix[j]=token;

j++;

}

else if(token=='(')

{

push1('(');

}

else if(token==')')

{

while(stack1[top1]!='(')

{

ch=pop1();

post_fix[j]=ch;

j++;

}

ch=pop1();

}

else

{

while(ISPriority(stack1[top1])>=ICP(token))

{

ch=pop1();

post_fix[j]=ch;

j++;

}

push1(token);

}

i++;

token=infix[i];

}

while(top1!=0)

{

ch=pop1();

post_fix[j]=ch;

j++;

}

post_fix[j]=";

}

/*Determining the priority of elements that are placed inside the stack. */

int ISPriority(char token)

{

switch(token)

{

case '(':return (0);

case ')':return (9);

case '+':return (7);

case '-':return (7);

case '*':return (8);

case '/':return (8);

case '?':return (0);

default: printf("nnInvalid expression");

}

return 0;

}

/*Determining the priority of elements that are approaching towards the stack. */

int ICP(char token)

{

switch(token)

{

case '(':return (10);

case ')':return (9);

case '+':return (7);

case '-':return (7);

case '*':return (8);

case '/':return (8);

case ":return (0);

default: printf("nnInvalid expression");

}

return 0;

}

/*Calculating the result of expression, which is converted in postfix notation. */

float eval(char p[], float x1)

{

float t1,t2,k,r;

int i=0,l;

l=strlen(p);

while(i<l)

{

if(p[i]==’x')

push(x1);

else

if(isdigit(p[i]))

{

k=p[i]-'0′;

push(k);

}

else

{

t1=pop();

t2=pop();

switch(p[i])

{

case '+':k=t2+t1;

break;

case '-':k=t2-t1;

break;

case '*':k=t2*t1;

break;

case '/':k=t2/t1;

break;

default: printf("ntInvalid expression");

}

push(k);

}

i++;

}

if(top>0)

{

printf("You have entered the operands more than the operatorsnn");

exit(0);

}

else

{

r=pop();

return (r);

}

return 0;

}

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 Chapter 2 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 2 of the book for easy reference.

Chapter 2: Types, Operators and Expressions

  1. Exercise 2-1.Write a 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. Harder if you compute them: determine the ranges of the various floating-point types.
    Solution to Exercise 2-1.
  2. Exercise 2-2.Write a loop equivalent to the for loop above without using && or || .
    Solution to Exercise 2-2.
  3. Exercise 2-3.Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F .
    Solution to Exercise 2-3.
  4. Exercise 2-4.Write an alternate version of squeeze(s1,s2) that deletes each character in the string s1 that matches any character in the string s2 .
    Solution to Exercise 2-4.
  5. Exercise 2-5.Write the function any(s1,s2) , which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2 . (The standard library function strpbrk does the same job but returns a pointer to the location.)
    Solution to Exercise 2-5.
  6. Exercise 2-6.Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.
    Solution to Exercise 2-6.
  7. Exercise 2-7.Write a 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.
    Solution to Exercise 2-7.
  8. Exercise 2-8.Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions.
    Solution to Exercise 2-8.
  9. Exercise 2-9.In a two’s complement number system, x &= (x-1) deletes the rightmost 1-bit in x . Explain why. Use this observation to write a faster version of bitcount .
    Solution to Exercise 2-9.
  10. Exercise 2-10.Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else .
    Solution to Exercise 2-10.
You can purchase the book from here or here.

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!

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 demonstrate how the escape characters work.

C Program to demonstrate how the escape characters work.Escape character is the single character, and it is compiled as the character constant.
Escape characters are used to solve the c format problems.
An escape sequence is denoted by using the backslash () as an escape character, followed by a single character indicating the nonprintable character desired.
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>
main()
{
clrscr();
printf("Escape operators");

printf("Example program to show the escape characters:nn");
printf("t here is tab");
printf("n Here starts new line");
getch();
}

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 find the position of a sub string.

C Strings:
C Program to find the position of a sub string in a given string.
In this program, We find the position of a sub string where it starts  in the main string. 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>
main()
{
char firststr[30], substr[10];
char *secondstr, *thirdstr;
clrscr();
printf("Enter any string");
scanf("%s",firststr);
printf("Enter sub string");
scanf("%s",substr);
secondstr=firststr;
thirdstr=substr;
i=substr(secondstr, thirdstr);
printf("nn");
if(i!=-1)
printf("The starting location/position of substring in main string: %d", i;
else
printf("String not found");
getch();
}


int substr(secondstr, thirdstr)

{
char *secondstr, *thirdstr;

int j=0,l=0;
static int k;
if(*second==*third)
{
while(i<strlen(third))
{
if(*second==*third)
k=j+1;
else
k+=1;
l++;
}
third++;
second++;
}
second++;
j++;

if(k!=0)
return(k);
else
return(-1);
}
Read more Similar C Programs
Searching in C

C Strings

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 Demonstrate the Recursive function.

Write a C program to demonstrate the recursive function.
Recursion  is the programming technique that a process invoking itself again and again. In this program, We reverse the given number and checks it is a palindrome or not. 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>

int main(){
clrscr();
int num,num1,rev;
printf("nEnter a number :n");
scanf("%d",&num);
num1=num;
//call recursive function
rev=reverse(num);

printf("nAfter reverse the number is :n%d",rev);
if(num1==rev){
printf("nnNumber %d is Palindromen",num1);
}else
{
printf("nnNumber %d is NOT a Palindromen",num1);
}

return 0;
}
int sum=0,r;
reverse(int num){
if(num){
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
return sum;
}

Read more Similar C Programs
Learn C Programming
Recursion
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 count the leaves of the binary tree.

Write a C program to count the leaves of the binary tree.
Binary tree is the ordered directed tree data structure, in which each node has at most two nodes.
A node is called as a leaf node,  if it does not contains any child elements. In this program, We used the structures to create the binary tree.
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>
#include <conio.h>

struct node
{
int data;
struct node* leftnode;
struct node* rightnode;
};

//get the leaves count
unsigned int getLeafCount(struct node* node)
{
if(node == NULL)
return 0;
if(node->leftnode == NULL && node->rightnode==NULL)
return 1;
else
return getLeafCount(node->leftnode)+
getLeafCount(node->rightnode);
}


struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->leftnode = NULL;
node->rightnode = NULL;

return(node);
}


int main()
{

clrcsr();
struct node *root = newNode(1);
root->leftnode = newNode(2);
root->rightnode = newNode(3);
root->leftnode->leftnode = newNode(4);
root->leftnode->rightnode = newNode(5);


printf("nnLeaf count of the binary tree is %d", getLeafCount(root));

getch();
return 0;
}


Read more Similar C Programs
Data Structures
Breadth First Search(BFS)
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 implement hashing.

Write a C Program to implement hashing.
Hashing is the function or routine used to assign the key values to the each entity in the database. Using hashing, We can easily access or search the values from database.
In this program we used the open addressing hashing, also called as closed hashing.
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>
void main() {
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n, value;
int temp, hash;
clrscr();
printf("nEnter the value of n(table size):");
scanf("%d", &n);
do {
printf("nEnter the hash value");
scanf("%d", &value);
hash = value % n;
if (a[hash] == 0) {
a[hash] = value;
printf("na[%d]the value %d is stored", hash, value);
} else {
for (hash++; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
}
}

for (hash = 0; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
}
}
printf("nnERRORn");
printf("nEnter '0' and press 'Enter key' twice to exit");

}

menu:

printf("n Do u want enter more");

scanf("%d", &temp);

}

while (temp == 1);

getch();

}

Read more Similar C Programs
C Basic
C Data Structure
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

K & R C Chapter 1 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 1 of the book for easy reference.

Chapter 1: A Tutorial Introduction

  1. Exercise 1-1. Run the “hello, world” program on your system. Experiment with leaving out parts of the program to see what error messages you get.
    Solution to Exercise 1-1.
  2. Exercise 1-2. Experiment to find what happens when printf’s argument string contains c, where c is some character not listed above.
    Solution to Exercise 1-2.
  3. Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
    Solution to Exercise 1-3.
  4. Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
    Solution to Exercise 1-4.
  5. Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
    Solution to Exercise 1-5.
  6. Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
    Solution to Exercise 1-6.
  7. Exercise 1-7. Write a program to print the value of EOF.
    Solution to  Exercise 1-7.
  8. Exercise 1-8.Write a program to count blanks, tabs, and newlines.
    Solution to  Exercise 1-8.
  9. Exercise 1-9.Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
    Solution to  Exercise 1-9.
  10. Exercise 1-10. Write a program to copy its input to its output, replacing each tab by t , each backspace by b , and each backslash by \ . This makes tabs and backspaces visible in an unambiguous way.
    Solution to  Exercise 1-10.
  11. Exercise 1-11.How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?
    Solution to  Exercise 1-11.
  12. Exercise 1-12.Write a program that prints its input one word per line.
    Solution to  Exercise 1-12.
  13. Exercise 1-13.Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
    Solution to  Exercise 1-13.
  14. Exercise 1-14.Write a program to print a histogram of the frequencies of different characters in its input.
    Solution to  Exercise 1-14.
  15. Exercise 1-15.Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
    Solution to  Exercise 1-15.
  16. Exercise 1-16.Revise the the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.
    Solution to  Exercise 1-16.
  17. Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
    Solution to  Exercise 1-17.
  18. Exercise 1-18. Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.
    Solution to  Exercise 1-18.
  19. Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write program that reverse thes its input a line at a time.
    Solution to  Exercise 1-19.
  20. Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?
    Solution to  Exercise 1-20.
  21. Exercise 1-21. Write a program entab that replaces strings of blanks with the minimum number of tabs and blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?
    Solution to  Exercise 1-21.
  22. Exercise 1-22. Write a program to “fold” long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
    Solution to  Exercise 1-22.
  23. Exercise 1-23. Write a program to remove all comments from a C program. Don’t forget to handle quoted strings and character constants properly. C comments do not nest.
    Solution to  Exercise 1-23.
  24. Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don’t forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.)
    Solution to  Exercise 1-24.
You can purchase the book from here or here.

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!

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