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 info@c-program-example.com
* 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