EXPLORING CYBERSPACE…

Entries tagged as ‘C Programs’

Queue implementation

January 25, 2009 · Leave a Comment

//Insertion and deletion  in a Queue
#include<stdio.h>
#include<conio.h>
int front=0,rear=0,A[15],n;

void enq()
{
int val;
printf(“n Enter the no to be enqueued===> “);
scanf(“%d”,&val);
if(((front==1)&&(rear==n))||(front==(rear+1)))
printf(“n “Queue overflow” n”);
else
{
if(front==0)
{
front=1;
rear=1;
}
else if(rear==n)
rear=1;
else
rear=rear+1;
A[rear]=val;
}
}

void deq()
{
if(front==0)
{
printf(“n “Queue underflow”n”);
}
else if(front==rear)
{
front=0;
rear=0;
}
else if(front==n)
front=1;
else
front=front+1;
}

void display()
{
printf(“nn The elements in Queue are::n”);
if(front<=rear)
{
for(int i=front;i<=rear;i++)
printf(” %d “,A[i]);
}
else
{
for(int i=front;i<=n;i++)
printf(” %d “,A[i]);
for(i=1;i<=rear;i++)
printf(” %d “,A[i]);
}
}

main()
{
clrscr();
int ch;
printf(“nn INSERTION AND DELETION IN QUEUESnn”);
printf(“n Enter the maximum size of Queue::”);
scanf(“%d”,&n);
printf(“nn Enter your choice::n 1==>Enqueue,n 2==>Dequeue,n 3==>Display,and,n 4==>Exit::   “);
scanf(“%d”,&ch);
do
{
if(ch==1)
enq();
else if(ch==2)
deq();
else if(ch==3)
display();
else if(ch==4)
return 0;
else
printf(“Oops!! Wrong choice entered”);
printf(“nn Enter your choice again::”);
scanf(“%d”,&ch);
}
while(ch!=4);
}

Categories: Data Structures · Programming in C
Tagged:

Regula Falsi Method

January 25, 2009 · 1 Comment

/*
Program to find out the roots of equation using regula-falsi method.
*/

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
float f(float x)
{
return(x*x*x-2*x-5);
}
void regular(float *m, float x0, float x1, float fx0,float fx1, int *itr)
{
*m=x0-((x1-x0)/(fx1-fx0))*fx0;
++(*itr);
printf(“Iteration no %d x= %7.5fn”,*itr,*m) ;
}

int main()
{
clrscr();
int itr=0,maxitr;
float x0,x1,x2,aerr,x3;
printf(“nnEnter the values of x0, x1, allowed error and maximum iterations==>n”);
scanf(“%f %f %f %d”,&x0,&x1,&aerr,&maxitr);
regular(&x2,x0,x1,f(x0),f(x1),&itr);
do
{
if(f(x0)*f(x1)<0)
x1=x2;
else
x0=x2;
regular(&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2)<aerr)
{
printf(“After%d Iterations, root=%6.4fn”,itr,x3);
return 0;
}
x2=x3;
}
while(itr<maxitr);
printf(“nSolution does not converge, Iterations not Sufficient”);
return 1;
}

Categories: Numerical Techniques · Programming in C
Tagged:

Bisection Method

January 25, 2009 · Leave a Comment

// Program to find out the roots of equation, using Bisection Method.

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>

float f(float x)  //Returning the value of f(x)
{
return(x*x*x-4*x-9);
}
void bisect(float *m, float a, float b, int *itr)
{
*m=(a+b)/2;
++(*itr);
printf(“Iteration no %3dx= %7.5fn”,*itr,*m) ;
}

main() //Main Program
{
clrscr();
int itr=0,maxitr;
float x,a,b,aerr,x1;
printf(“ntBISECTION METHOD TO CALCULATE ROOTS OF EQN.x*x*x-4*x-9nn”);
printf(“nnEnter the values of a, b, allowed error and maximum iterations==>n”);
scanf(“%f%f%f%d”,&a,&b,&aerr,&maxitr);

bisect(&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
b=x;
else
a=x;
bisect(&x1,a,b,&itr);
if(fabs(x1-x)<aerr)
{
printf(“After%d Iterations, root=%6.4fn”,itr,x1);
delay(4000);
return 0;
}
x=x1;
}
while(itr<maxitr);
printf(“nSolution does not converge, Iterations not Sufficient”);
delay(4000);
return 1;
}

Categories: Numerical Techniques · Programming in C
Tagged:

Stack implementation

January 25, 2009 · Leave a Comment

/*
Program to implement STACKS as an array clearly depicting the ‘Push’ and ‘Pop’ operations
*/

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
void main()
{
clrscr();
int top=0,choice,n,item,i,A[25];
printf(“nnn Enter the choice::nnn 1==>’Push the element in stack’nn”);
printf(” 2==>’Pop the element off stack’nn 3==> Exitnnn Your Choice::”);
scanf(“%d”,&choice);
printf(“n Enter the maximum size of STACK::”);
scanf(“%d”,&n);
while((choice==1)||(choice==2)||(choice==3))
{
switch(choice)
{
case 1: //Push the elements in Stack .
{
printf(“n Enter the element to be pushed::”);
scanf(“%d”,&item);
if(top==n)
{
printf(“nn STACK OVERFLOW!!”);
break;
}
else
{
top=top+1;
A[top]=item;
}
break;
}
case 2: //Pop the elements out of Stack .
{
if(top==0)
{
printf(“nn STACK UNDERFLOW!!”);
break;
}
else
top=top-1;
break;
}
case 3: //Exit the operations.
{
if(top>0)
{
printf(“n The elements in stacks are::”);
for(i=1;i<=top;i++)
printf(“%d “,A[i]);
delay(4000);
exit(0);
}
break;
}
}
printf(“nnn Enter your choice::”);
scanf(“%d”,&choice);
}
}

Categories: Data Structures · Programming in C
Tagged: