# 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 ProgramsData StructuresLearn C Programming`

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

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

our page!

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

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

# K & R C Programs Exercise 4-4.

K and R C, Solution to Exercise 4-4:
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 the following:
Show the top item of the stack without permanently popping it.
Swap the top two items on the stack.
Duplicate the top item on the stack.
Clear the stack, with the previous cases i. e K and R C Programs Exercise 4-3. 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 "stdlib.h"#include"stdio.h"#include "ctype.h"#include "math.h"#define MAXOP 100#define NUMBER  0#define TRUE 1#define FALSE 0int Getop(char s[]);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStack();int main(void){ int type; double op2; char s[MAXOP]; int flag = TRUE; while((type = Getop(s)) != EOF) {  switch(type)  {  case NUMBER:   push(atof(s));   break;  case '+':   push(pop() + pop());   break;  case '*':   push(pop() * pop());   break;  case '-':   op2 = pop();   push(pop()- op2);   break;  case '/':   op2 = pop();   if(op2)    push(pop() / op2);   else    printf("nError: division by zero!");   break;  case '%':   op2 = pop();   if(op2)    push(fmod(pop(), op2));   else    printf("nError: division by zero!");   break;  case '?':   showTop();   break;  case '#':   duplicate();   break;  case '~':   swapItems();   break;  case '!':   clearStack();  case 'n':   printf("nt%.8gn", pop());   break;  default:   printf("nError: unknown command %s.n", s);   break;  } } return EXIT_SUCCESS;}#define MAXVAL 100int sp = 0;          /* Next free stack position. */double val[MAXVAL];  /* value stack. *//* push: push f onto stack. */void push(double f){ if(sp < MAXVAL)  val[sp++] = f; else  printf("nError: stack full can't push %gn", f);}/*pop: pop and return top value from stack.*/double pop(void){ if(sp > 0)  return val[--sp]; else {  printf("nError: stack emptyn");  return 0.0; }}void showTop(void){ if(sp > 0)  printf("Top of stack contains: %8gn", val[sp-1]); else  printf("The stack is empty!n");}void duplicate(void){ double temp = pop(); push(temp); push(temp);}void swapItems(void){ double item1 = pop(); double item2 = pop(); push(item1); push(item2);}/* pop only returns a value if sp is greater than zero. So setting thestack pointer to zero will cause pop to return its error */void clearStack(void){ sp = 0;}int getch(void);void unGetch(int);/* Getop: get next operator or numeric operand. */int Getop(char s[]){ int i = 0; int c; int next; /* Skip whitespace */ while((s[0] = c = getch()) == ' ' || c == 't')  ; s[1] = ''; /* Not a number but may contain a unary minus. */ if(!isdigit(c) && c != '.' && c != '-')  return c;                if(c == '-') {  next = getch();  if(!isdigit(next) && next != '.')  {   return c;  }  c = next; } else  c = getch(); while(isdigit(s[++i] = c))  c = getch(); if(c == '.')                        /* Collect fraction part. */  while(isdigit(s[++i] = c = getch()))   ; s[i] = ''; if(c != EOF)  unGetch(c); return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE];int bufp = 0;/* Getch: get a ( possibly pushed back) character. */int getch(void){ return (bufp > 0) ? buf[--bufp]: getchar();}/* unGetch: push character back on input. */void unGetch(int c){ if(bufp >= BUFSIZE)  printf("nUnGetch: too many charactersn"); else  buf[bufp++] = c;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

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

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

our page!

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

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 ProgramsData StructuresLearn C Programming`

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

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

our page!

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

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

# C Program to implement STACK operations using Linked Lists

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 stack operations using linked list. 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 <stdlib.h>typedef struct node { int data; struct node *link;} NODE;void Push(int);int pop();void Display();NODE *top = NULL; /* Global Declarations */main() { /* Main Program */ int opn, elem; do {  clrscr();  printf("n ### Linked List Implementation of STACK Operations ### nn");  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 tobe pushed ?");   scanf("%d", &elem);   Push(elem);   break;  case 2:   elem = Pop();   if (elem != -1)    printf(" Deleted Node(From Top)with the Data: %dn", elem);   break;  case 3:   printf("Linked List Implementation of Stack: Status:n");   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);}void Push(int info) { NODE *temp; temp = (NODE *) malloc(sizeof(NODE)); if (temp == NULL)  printf(" Out of Memory !! Overflow !!!"); else {  temp->data = info;  temp->link = top;  top = temp;  printf(" Node has been inserted at Top(Front) Successfully !!"); }}int Pop() { int info; NODE *t; if (top == NULL) {  printf(" Underflow!!!");  return -1; } else {  t = top;  info = top->data;  top = top->link;  t->link = NULL;  free(t);  return (info); }}void Display() { NODE *t; if (top == NULL)  printf("Empty Stackn"); else {  t = top;  printf("Top->");  while (t) {   printf("[%d]->", t->data);   t = t->link;  }  printf("Nulln"); }}`
`Read more Similar C ProgramsData StructuresLearn C Programming`

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

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

our page!

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

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

# C Program for Evaluation of Postfix Expression

C Program for Evaluation of Postfix ExpressionIn this program we evaluate the Postfix Expression, using the stack. For example, 456*+7- is the postfix expression, from left one by one it is inserted into the stack, and after evaluation the answer is 27. 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 50            /* Size of Stack */#include <ctype.h>int s[SIZE];int top=-1;       /* Global declarations */push(int elem){                       /* Function for PUSH operation */ s[++top]=elem;}int pop(){                      /* Function for POP operation */ return(s[top--]);}main(){                         /* Main Program */ char pofx[50],ch; int i=0,op1,op2; printf("nnRead the Postfix Expression ? "); scanf("%s",pofx); while( (ch=pofx[i++]) != '') {  if(isdigit(ch)) push(ch-'0'); /* Push the operand */  else  {        /* Operator,pop two  operands */   op2=pop();   op1=pop();   switch(ch)   {   case '+':push(op1+op2);break;   case '-':push(op1-op2);break;   case '*':push(op1*op2);break;   case '/':push(op1/op2);break;   }  } } printf("n Given Postfix Expn: %sn",pofx); printf("n Result after Evaluation: %dn",s[top]);} `
`Read more Similar C ProgramsData StructuresLearn C Programming`

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

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

our page!

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

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

# C Program for Stack Operations using arrays.

Data structures using C,
Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are added or deleted from only one end, i.e. top of the stack. Here we implement the PUSH, POP, DISPLAY stack operations using the 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]xample.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                                  Happy Coding***********************************************************/#include<stdio.h>#define SIZE 5            /* Size of Stack */int s[SIZE], top = -1; /* Global declarations */push(int elem) { /* Function for PUSH operation */ if (Sfull())  printf("nn Overflow!!!!nn"); else {  ++top;  s[top] = elem; }}int pop() { /* Function for POP operation */ int elem; if (Sempty()) {  printf("nnUnderflow!!!!nn");  return (-1); } else {  elem = s[top];  top--;  return (elem); }}int Sfull() { /* Function to Check Stack Full */ if (top == SIZE - 1)  return 1; return 0;}int Sempty() { /* Function to Check Stack Empty */ if (top == -1)  return 1; return 0;}display() { /* Function to display status of Stack */ int i; if (Sempty())  printf(" n Empty Stackn"); else {  for (i = 0; i <= top; i++)   printf("%dn", s[i]);  printf("^Top"); }}main() { /* Main Program */ int opn, elem; do {  clrscr();  printf("n ### Stack Operations ### nn");  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);   break;  case 2:   elem = pop();   if (elem != -1)    printf("nnPopped Element is %d n", elem);   break;  case 3:   printf("nnStatus of Stacknn");   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 ProgramsData StructuresLearn C Programming`

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

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

our page!

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

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

# C program to implement stack.

Data structures using C,
Write a C program to implement stack. Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are added or deleted from only one end, i.e. top of the stack. Stack is a LIFO data structure. LIFO – Last in First Out Perform PUSH(insert operation), POP(Delete operation) and Display stack . 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].com* To find more C programs, do visit www.c-program-example.com* and browse!* *                                  Happy Coding***********************************************************/#include <stdio.h>#include <conio.h>#define MAXSIZE 5struct stack            /* Structure definition for stack */{ int stk[MAXSIZE]; int top;};typedef struct stack STACK;STACK s;/* Function declaration/Prototype*/void push (void);int  pop(void);void display (void);void main (){ int choice; int option = 1; clrscr (); s.top = -1; printf ("STACK OPERATIONn"); while (option) {  printf ("------------------------------------------n");  printf ("   1 --> PUSH            n");  printf ("   2 --> POP            n");  printf ("   3 --> DISPLAY            n");  printf ("   4 --> EXIT     n");  printf ("------------------------------------------n");  printf ("Enter your choicen");  scanf ("%d", &choice);  switch (choice)  {  case 1: push();  break;  case 2: pop();  break;  case 3: display();  break;  case 4: return;  }  fflush (stdin);  printf ("Do you want to continue(Type 0 or 1)?n");  scanf ("%d", &option); }}/*Function to add an element to the stack*/void push (){ int num; if (s.top == (MAXSIZE - 1)) {  printf ("Stack is Fulln");  return; } else {  printf ("Enter the element to be pushedn");  scanf ("%d", &num);  s.top = s.top + 1;  s.stk[s.top] = num; } return;}/*Function to delete an element from the stack*/int pop (){ int num; if (s.top == - 1) {  printf ("Stack is Emptyn");  return (s.top); } else {  num = s.stk[s.top];  printf ("poped element is = %dn", s.stk[s.top]);  s.top = s.top - 1; } return(num);}/*Function to display the status of the stack*/void display (){ int i; if (s.top == -1) {  printf ("Stack is emptyn");  return; } else {  printf ("nThe status of the stack isn");  for (i = s.top; i >= 0; i--)  {   printf ("%dn", s.stk[i]);  } } printf ("n");}`
`Read more Similar C ProgramsData StructuresLearn C Programming`

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