Thursday, January 27, 2011

C Program to find day when a date is given

#include<stdio.h>
main(){
int d,m,y,i;
int r=0,r1=0,r2=0,s=0;
input:
printf("Enter the DATE MONTH and YEAR");
scanf("%d %d %d",&d,&m,&y);
 if(m==2){
   if((d>28)&&(y%4!=0)){
   printf("Fool,Invalid date in February of non leap year.The maximum value is 28");
     goto input;
     }
     if((d>29)&&(y%4==0)){
     printf(" Fool,Invalid date in February of leap year.The maximum value is 29");
     goto input;
     }
     }
 else if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12)){
    if(d>31){
    printf("Fool,Invalid date in %d th month.The maximum value is 31",m);
    goto input;
    }
    }
 else if(((m==4)||(m==6)||(m==9)||(m==11))&&(d>30)){
      printf("Fool,Invalid date in the %d th month.The maximum value is 30",m);
      goto input;
      }
 else if((d>31)){
printf(" Enter valid date");
goto input;
}
if((d<1)||(m<1)){
printf(" Fool,Enter positive valid data ");
goto input;
}
 else  {

switch(m){
case 1:
case 10:
r1=d+2;
break;
case 2:
case 3:
case 11:
r1=d+5;
break;
case 4:
case 7:
r1=d+1;
break;
case 9:
case 12:
r1=d;
break;
case 5:
r1=d+3;
break;
case 6:
r1=d+6;
break;
case 8:
r1=d+4;
break;
default:
{
printf(" Don't U know that there is no month greater than 12.Enter valid month Once again enter the Date Month Year ");

goto input;
}
}
}
if(y<2003){
  for(i=2002;i>=y;i--){
    if((i==y)&&(i%4==0)){
      if(m>2) s=s+1;
      else if(m<=2) s=s+2;
       break;
       }

       else if(i%4==0){
       if(i%100==0){
  if(i%400==0)
  s=s+2;
  else s=s+1;
  }
       else s=s+2;
 }
       else s=s+1;
       }
       r2=7-s%7;
       }
else if(y>2003){
 for(i=2004;i<=y;i++){
  if((i==y)&&(i%4==0)){
    if(m>2) s=s+2;
    else if(m<=2) s=s+1;
    break;
    }
     else if(i%4==0){
      if(i%100==0){
      if(i%400==0)
      s=s+2;
      else s=s+1;
      }
      else s=s+2;
      }
     else s=s+1;
     }
     r2=s%7;
     }
else r2=0;
r=(r1+r2)%7;
printf(" ");
switch(r){
case 0:
printf("The Day is SUNDAY");
break;
case 1:
printf("The Day is MONDAY");
break;
case 2:
printf("The Day is TUESDAY");
break;
case 3:
printf("The Day is WEDNESDAY");
break;
case 4:
printf("The Day is THURSDAY");
break;
case 5:
printf("The Day is FRIDAY");
break;
case 6:
printf("The Day is SATURDAY");
break;
}
printf("Thanks for using my program  ");
}

Wednesday, January 12, 2011

C Program to demonstrate Airport Simulation

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<time.h>
#include<limits.h>
#define MAX 3
#define ARRIVE 0
#define DEPART 1
struct plane
{
  int id;    /* Identification no. of the plane */
  int tm;
};


struct queue
{
   int count;
   int front;
   int rear;
   struct plane p[MAX];
};


void initqueue(struct queue *);
void addqueue(struct queue *,struct plane);
struct plane delqueue(struct queue *);
int size(struct queue);
int empty(struct queue);
int full(struct queue);




void initqueue(struct queue *pq)
{
   pq->count=0;
   pq->front=0;
   pq->rear=-1;
}


void addqueue(struct queue *pq,struct plane item)
{
   if(pq->count >= MAX)
   {
     printf("\n QUEUE is full .\n");
     return;
   }
   (pq->count)++;
  pq->rear=(pq->rear+1)%MAX;
  pq->p[pq->rear]=item;
}


struct plane delqueue(struct queue *pq)
{
   struct plane pl;
   if(pq->count <= 0)
   {
      printf("\n QUEUE is Empty.\n ");
      pl.id=0;
      pl.tm=0;
   }
   else
   {
     (pq->count)--;
     pl=pq->p[pq->front];
     pq->front=(pq->front+1)%MAX;
   }
   return pl;
}




int size(struct queue q)
{
   return q.count;
}


int empty(struct queue q)
{
   return (q.count <= 0);
}


int full(struct queue q)
{
   return (q.count>=MAX);
}




struct airport
{
   struct queue landing;
   struct queue takeoff;
   struct queue *pl;
   struct queue *pt;
   int idletime;              // Numberv of units when runway is idle
   int landwait;              // Total waiting time for planes landed
   int takeoffwait;           // Total waiting time for take off;
   int nland;                 // No. of planes landed
   int nplanes;               // No. of planes processed so far
   int nrefuse;               // No. of planes refused of airport
   int ntakeoff;              // No. of planes taken off
   struct plane pln;
};


void initairport(struct airport *);
void start(int *,double *,double *);
void newplane(struct airport *,int ,int );
void refuse(struct airport *,int);
void land(struct airport * ,struct plane,int);
void fly(struct airport*,struct plane,int);
void idle(struct airport *,int);
void conclude(struct airport *,int);
int randomnumber(double);
void apaddqueue(struct airport *,char);
struct plane apdelqueue(struct airport *,char);
int apsize(struct airport,char);
int apfull(struct airport,char);
int apempty(struct airport,char);
void myrandomize();




void initairport(struct airport *ap)
{
   initqueue(&(ap->landing));
   initqueue(&(ap->takeoff));
  ap->pl=&(ap->landing);
  ap->pt=&(ap->takeoff);
  ap->nplanes=ap->nland=ap->ntakeoff=ap->nrefuse=0;
  ap->landwait=ap->takeoffwait=ap->idletime=0;
}


void start(int *endtime,double *expectarrive,double *expectdepart)
{
  int flag=0;
  char wish;
  printf("\n Program that stimulates an airport with only one runway.\n");


  /* Instruct User */
  printf(" One plane can land or depart in each unit of time . \n");
  printf(" Up to %d planes can be waiting to land or takeoff at any time .\n",MAX);
  printf(" How many units of time will the stimulation run ?");


  /* Input parameter */
  scanf("%d",endtime);
  myrandomize();
  do
  {
     printf("\n Expected number of arrivals per unit time ? ");
     /* Error checking */
     scanf("%lf",expectarrive);
     printf("\n Expected nu,ber of departures per unit time ? ");
     scanf("%lf",expectdepart);
     if(*expectarrive < 0.0 || *expectarrive < 0.0)
     {
       printf(" These numbers must be nonnegative .\n");
       flag=0;
     }
     else
     {
       if(*expectarrive + *expectdepart > 1.0)
       {
 printf(" The airport will become saturated. Read new numbers ? ");


 fflush(stdin);
   scanf("%c",&wish);
   if(tolower(wish)=='y')
      flag=0;
   else
      flag=1;
       }
       else
  flag=1;
     }
   }while(flag==0);
}




void newplane(struct airport *ap,int curtime,int action)
/* Newplane: make a new record for a plane , update nplanes */
{
  (ap->nplanes)++;
 ap->pln.id=ap->nplanes;
 ap->pln.tm=curtime;


 switch(action)
 {
    case ARRIVE:
       printf("\n");
       printf(" Plane %d ready to land . \n",ap->nplanes);
       break;
    case DEPART:
       printf("\nPlane %d ready to take iff .\n",ap->nplanes);
       break;
 }
}




void refuse(struct airport *ap,int action)
/* refuses : processes the plane when queue is full */
{
   switch(action)
   {
      case ARRIVE:
      printf("\tplane %d directed to another airport .\n",ap->pln.id);
      break;
      case DEPART:
      printf("\tplane %d told to try later .\n",ap->pln.id);
      break;
   }
   (ap->nrefuse)++;
}


void land(struct airport *ap,struct plane pl,int curtime)
/* Land :processa plane p that is actually landing */
{
   int wait;
   wait=curtime-pl.tm;
   printf("%d : Plane %d landed",curtime,pl.id);
   printf(" in queue %d units \n",wait);
   (ap->nland)++;
   (ap->landwait)+=wait;
}






void fly(struct airport *ap,struct plane pl,int curtime)
/* Fly : process a plane p that i actually landing */
{
   int wait;
   wait=curtime-pl.tm;
   printf("%d : Plane %d took off ",curtime,pl.id);
   printf(" in queue %d units \n",wait);
   (ap->ntakeoff)++;
   (ap->takeoffwait)+=wait;
}


void idle(struct airport *ap,int curtime)
/* Idle : updates variables for idle runway */
{
   printf("%d : Runway is idle \n",curtime);
   ap->idletime++;
}


void conclude(struct airport *ap,int endtime)
/* Conclude : write out statistics and conclude simulation */
{
   printf("\t Simulation has concluded after %d units . \n",endtime);
   printf("\t Total number of planes processed : %d\n",ap->nplanes);
   printf("\t Number if planes landed : %d\n",ap->nland);
   printf("\t Number of planes taken off : %d\n",ap->ntakeoff);
   printf("\t Number of planes refused use  %d  \n",ap->nrefuse);
   printf("\t Number left ready to land : %d \n",apsize(*ap,'l'));
   printf("\t Number left ready to take off : %d\n",apsize(*ap,'t'));


   if(endtime > 0)
      printf("\t Percentage of time runway idle : %lf \n",((double)ap->idletime/endtime)*100.0);
   if(ap->nland > 0)
      printf("\t Average wait time to land : %lf \n",((double)ap->landwait/ap->nland));
   if(ap->ntakeoff > 0)
      printf("\t Average wait time to takeoff : %lf \n",((double)ap->takeoffwait/ap->ntakeoff));
}






int randomnumber(double expectedvalue)
/* Randomnumber : generates a pseudorandom integer according to the Poisson Distribution */
{
   int n=0;   /* Counter of iterations*/
   double em;
   double x;  /* Pseudorandom number */
   em=exp(-expectedvalue);
   x=rand()/(double)INT_MAX;
   while(x > em)
   {
     n++;
     x*=rand()/(double)INT_MAX;
   }
     return n;
}




void apaddqueue(struct airport *ap,char type)
{
   switch(tolower(type))
   {
      case 'l' :
addqueue(ap->pl,ap->pln);
break;
      case 't' :
addqueue(ap->pt,ap->pln);
break;
   }
}






struct plane apdelqueue(struct airport *ap,char type)
{
   struct plane pl;
   switch(tolower(type))
   {
      case 'l':
     pl=delqueue(ap->pl);
     break;
      case 't':
     pl=delqueue(ap->pl);
     break;
   }
   return pl;
}


int apsize(struct airport ap,char type)
{
   switch(tolower(type))
   {
      case 'l':
      return(size(*(ap.pl)));
      case 't':
      return(size(*(ap.pt)));
   }
   return 0;
}




int apfull(struct airport ap,char type)
{
   switch(tolower(type))
   {
       case 'l':   return(full(*(ap.pl)));
       case 't':   return(full(*(ap.pt)));
   }
   return 0;
}


int apempty(struct airport ap,char type)
{
   switch(tolower(type))
   {
      case 'l': return(empty(*(ap.pl)));
      case 't': return(empty(*(ap.pt)));
   }
   return 0;
}




void myrandomize()
/* Sets starting point for pseudorandom numbers */
{
   srand((unsigned int) (time(NULL)%10000));
}


void main()
{
   struct airport a;
   int i;
   int pri;                            /* Pseudorandom integer */
   int curtime;                        /* Current time ; one unit = time for takeoff and landing */
   int endtime;                        /* Total number of time units to run */
   double expectarrive;                /* Number of planes arriving in one unt */
   double expectdepart;                /* Number of planes newly ready to take off */
   struct plane temp;
     clrscr();
     initairport(&a);
    start(&endtime,&expectarrive,&expectdepart);
    for(curtime=1;curtime<=endtime;curtime++)
    {
       pri=randomnumber(expectarrive);
       for(i=1;i<=pri;i++)                    /* Add to landing queue */
       {
  newplane(&a,curtime,ARRIVE);
  if(apfull(a,'l'))
     refuse(&a,ARRIVE);
  else
     apaddqueue(&a,'l');
       }


       pri=randomnumber(expectdepart);
       for(i=1;i<=pri;i++)                /* Add to take off queue*/
       {
 newplane(&a,curtime,DEPART);
 if(apfull(a,'t'))
    refuse(&a,DEPART);
 else
    apaddqueue(&a,'t');
       }
       if(!(apempty(a,'l')))           /* Bring plane to land */
       {
temp=apdelqueue(&a,'l');
land(&a,temp,curtime);
       }
       else
       {
if(!(apempty(a,'t')))        /* Allow  plane to takeoff */
{
   temp=apdelqueue(&a,'t');
   fly(&a,temp,curtime);
}
else
     idle(&a,curtime);      /* Runway Idle */
       }
    }




    conclude(&a,endtime);                         /* Finish simulation */
    getch();
}

C Program to demnstrate insert and delete operations in a DEQUE


#include<stdio.h>
#include<conio.h>
#define MAX 10
void dequeaddatbeg(int *,int,int*,int *);
void dequeaddatend(int *,int,int *,int *);
int dequedelatbeg(int *,int *,int *);
int dequedelatend(int *,int *,int *);
void display(int *);
int count(int *);
void main()
{
   int arr[MAX];
   int front,rear,i,n;
   clrscr();
   /* Initiates data members */
   front=rear=-1;
   for(i=0;i<MAX;i++)
     arr[i]=0;
   dequeaddatend(arr,1,&front,&rear);
   dequeaddatbeg(arr,2,&front,&rear);
   dequeaddatend(arr,3,&front,&rear);
   dequeaddatbeg(arr,4,&front,&rear);
   dequeaddatend(arr,5,&front,&rear);
   dequeaddatbeg(arr,6,&front,&rear);
   dequeaddatend(arr,7,&front,&rear);
   dequeaddatbeg(arr,8,&front,&rear);
   dequeaddatend(arr,9,&front,&rear);
   dequeaddatbeg(arr,10,&front,&rear);
   dequeaddatend(arr,11,&front,&rear);
   dequeaddatbeg(arr,12,&front,&rear);
  // dequeaddatend(arr,13,&front,&rear);
  // dequeaddatend(arr,14,&front,&rear);
   print("\nElements in a deque: ");
   display(arr);
   n=count(arr);
   printf("\nTotal number of elements in deque : %d",n);
   i=dequedelatbeg(arr,&front,&rear);
   printf("\n Item Extracted : %d",i);
   i=dequedelatbeg(arr,&front,&rear);
   printf("\n Item Extracted : %d",i);
   i=dequedelatbeg(arr,&front,&rear);
   printf("\n Item Extracted : %d",i);
   i=dequedelatbeg(arr,&front,&rear);
   printf("\n Item Extracted : %d",i);
   printf("\nElements in a deque after deletion :");
   display(arr);
   dequeaddatend(arr,13,&front,&rear);
   dequeaddatend(arr,14,&front,&rear);
   printf(" Elemnts in the deque after deletion :");
   display(arr);
   i=dequedelatend(arr,&front,&rear);
   printf("\n Item Extracted : %d",i);
   i=dequedelatend(arr,&front,&rear);
   printf("\n Item Extracted : %d",i);
   printf(" \n Elemnts in a deque after deletion :");
   display(arr);
   n=count(arr);
   printf("\n Total number of elements in deque : %d",n);
   getch();
}


/* Adds an element at the beginning of a deque */
void dequeaddatbeg(int *arr,int item,int *pfront,int *prear)
{
   int i,k,c;
   if(*pfront==0 && *prear==MAX-1)
   {
      printf("\n Deque is FULL.\n");
      return;
   }
   if(*pfront == -1)
   {
      *pfront=*prear=0;
      arr[*pfront]=item;
      return;
   }
   if(*prear != MAX-1)
   {
     c=count(arr);
     k=(*prear)+1;
     for(i=1;i<=c;i++)
     {
arr[k]=arr[k-1];
k--;
     }
     arr[k]=item;
     *pfront=k;
     (*prear)++;
   }
   else
   {
     (*pfront)--;
     arr[*pfront]=item;
   }
 }


/* Adds an element at the end of a deque */
void dequeaddatend(int *arr,int item,int *pfront,int *prear)
{
   int i,k;
   if(*pfront==0 && *prear==MAX-1)
   {
      printf("\nDeque is FULL.\n");
      return;
   }
   if(*pfront==-1)
   {
     *prear=*pfront=0;
     arr[*prear]=item;
     return;
   }
   if(*prear==MAX-1)
   {
     k=*pfront-1;
     for(i=*pfront-1;i<(*prear);i++)
     {
       k=i;
       if(k == MAX-1)
 arr[k]=0;
       else
 arr[k]=arr[i+1];
     }
     (*prear)--;
     (*pfront)--;
   }
   (*prear)++;
   arr[*prear]=item;
}


/* Removes an elemnt from *pfront end of deque */
int dequedelatbeg(int *arr,int *pfront,int *prear)
{
   int item;
   if(*pfront==-1)
   {
      printf("\n Deque is empty ");


      return 0;
   }
   item=arr[*pfront];
   arr[*pfront]=0;
   if(*pfront==*prear)
      *pfront=*prear=-1;
   else
      (*pfront)++;
   return item;
}


/* Removes an element from the *prear end of the deque */
int dequedelatend(int *arr,int *pfront,int *prear)
{
   int item;
   if(*pfront==-1)
   {
     printf("\n Deque is empty .\n");
     return 0;
   }
   item=arr[*prear];
   arr[*prear]=0;
   (*prear)--;
   if(*prear==-1)
     *pfront=-1;
   return item;
}


/* Displays elements of a deque */
void display(int *arr)
{
   int i;
   printf("\n front ->");
   for(i=0;i<MAX;i++)
     printf("\t%d",arr[i]);
   printf(" <-rear");
}


/* Counts the total number of elements in deque */
int count(int *arr)
{
  int c=0,i;
  for(i=0;i<MAX;i++)
  {
    if(arr[i]!=0)
      c++;
  }
  return c;
}