Wednesday, October 1, 2014

Implement Newton’s forward interpolation polynomial (interpolation with equidistance points)

#include
#include
void main()
{
            clrscr();
            int i,j,n,k;
            float f[30],x[30],df[30][30],xx,a,b,mul,ad;
            printf ("\t\tNewton's Forward Interpolation\n");
            printf ("\t\t------------------------------\n\n");
            printf("How many points???");
            scanf("%d",&n);
            for(i=0;i
            {
                        printf("\nEnter the value of x[%d]:",i);
                        scanf("%f",&x[i]);
                        printf("Enter the value of f[x%d]:",i);
                        scanf("%f",&f[i]);
                        df[0][i]=f[i];
            }
            for(j=1;j
            {
                        for(i=0;i
                        {
                                    df[j][i]=df[j-1][i+1]-df[j-1][i];
                        }
            }
            printf("\n Enter the value x:");
            scanf("%f",&xx);
            a=x[1]-x[0];
            b=(xx-x[0])/a;
            mul=1.0;
            ad=f[0];
            for(i=1;i
            {
                        j=i-1;
                        mul=mul*(b-j)/(float)i;
                        ad=ad+(mul*df[i][0]);
            }
            printf("\n The required value = %4f",ad);
            getch();

}

Implement Newton’s backward interpolation polynomial using C programming.

#include
#include
void main()
{
            clrscr();
            int i,j,n,k;
            float f[30],x[30],df[30][30],xx,a,b,mul,ad,x0,f0;
            printf ("\t\tNewton's Backward Interpolation\n");
            printf ("\t\t------------------------------\n\n");
            printf("How many points???");
            scanf("%d",&n);
            for(i=0;i
            {
                        printf("\nEnter the value of x[%d]:",i);
                        scanf("%f",&x[i]);
                        printf("Enter the value of f[x%d]:",i);
                        scanf("%f",&f[i]);
                        df[0][i]=f[i];
            }
            printf("\n Enter the value x:");
            scanf("%f",&xx);
            a=x[1]-x[0];
            for(i=0;i
            {
                        df[i][1]=f[i+1]-f[i];

            }
            for(j=2;j
            {
                        for(i=0;i
                        {
                                    df[i][j]=df[i+1][j-1]-df[i][j-1];
                        }
            }
            i=0;
            while (!x[i]>xx)
            {
            i++;
            }
            x0=x[0];
            f0=f[0];
            b=(xx-x0)/a;
            mul=1;
            ad=f0;
            for(k=1;k<=n;k++)
            {
                        mul=(mul*(b-(k-1)))/k;
                        ad=ad+(mul*df[i][k]);
            }
            printf("\n The required value = %4f",ad);
            getch();

}

Implement the Newton’s interpolation using divided difference table.

#include
#include
#include
void main ()
{
  float x [10], y [10] [10], sum, p, u, temp;
  int i,n,j,k=0,f,m;
  float fact(int);
  clrscr();
  printf("\n How many points?? ");
  scanf("%d",&n);
  for(i=0; i
  {
   printf("\n\nenter the value of x%d: ",i);
   scanf("%f",&x[i]);
   printf("\n\nenter the value of f(x%d): ",i);
   scanf("%f",&y[k][i]);
  }
  printf("\n\nEnter X for finding f(x): ");
  scanf("%f",&p);

  for(i=1;i
  {
    k=i;
    for(j=0;j
    {
     y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
     k++;
    }
  }
  printf("\n_____________________________________________________\n");
  printf("\n  x(i)\t   y(i)\t    y1(i)    y2(i)    y3(i)    y4(i)");
  printf("\n_____________________________________________________\n");
  for(i=0;i
  {
    printf("\n %.3f",x[i]);
    for(j=0;j
    {
     printf("   ");
     printf(" %.3f",y[j][i]);
    }
   printf("\n");
  }

  i=0;
  do
  {
   if(x[i]
    k=1;
   else
    i++;
  }while(k != 1);
  f=i;

  sum=0;
  for(i=0;i
  {
   k=f;
   temp=1;
   for(j=0;j
   {
    temp = temp * (p - x[k]);
    k++;
   }
    sum = sum + temp*(y[i][f]);
  }
  printf("\n\n f(%.2f) = %f ",p,sum);
  getch();

}

Write a program in high level language to demonstrate the operation of the following CPU scheduling algorithms.

First Come First Serve
ALGORITHM:
1.      Start
2.      Get the number of processes
3.       
CODE:
#include
#include
void main()
{
     clrscr();
     int Bt[10],At[10],start[10],fin [10],TAT[10],wt[10], n,p[10],i;
     float avgwt,avgtat,totwt,tottat;
     printf("\n Enter number of Processes:");
     scanf("%d",&n);
     for(i=0;i
     {
                 printf("\n Input processes name,Arrival Time &Burst Time ");
                 scanf("%d %d %d",&p[i],&at[i],&bt[i] );
     }
     start[0]=at[0];
     fin [0]=start[0]+bt[0];
     wt[0]=0;
     TAT[0]=fin [0]-at[0];
     tottat=TAT[0];
     for(i=1;i
     {
         start[i]=fin [i-1];
         fin [i]=start[i]+bt[i];
         wt [i]=fin [i-1]-at[i];
         TAT[i]=fin [i]-at[i];
         totwt+= wt[i];
         tottat+=+TAT[i];
     }
     avgwt=totwt/n;
     avgtat=tottat/n;
     printf("Pname\tArrive time\tBurst time\tStart\tTAT\tFinish");
     for(i=0;i
     {
                 printf("\n%d\t%d\t\t%d\t\t%d\t%d\t%d",p[i],at[i],bt[i],start[i],TAT[i],fin [i]);
      }
      printf("\nAverage Waiting=%f",avgwt);
      printf("\nAverage Turn Over Around=%f",avgtat);
     getch();
}
Shortest Job First
ALGORITHM:

CODE:
#include
#include
void main()
{
     clrscr();
     int bt[10],at[10],start[10],fin [10],TAT[10],wt[10], n,p[10],i,j,temp;
     float avgwt,avgtat,totwt,tottat;
     printf("\n Enter number of Processes:");
     scanf("%d",&n);
     for(i=0;i
     {
                 printf("\n Input processes name,Arrival Time &Burst Time ");
                 scanf("%d %d %d",&p[i],&at[i],&bt[i] );
     }
     for(i=0;i
     {
                 for(j=i+1;j
                 {
                             if(bt[i]>bt[j])
                             {
                                         temp=bt[i];
                                         bt[i]=bt[j];
                                         bt[j]=temp;
                                         temp=p[i];
                                         p[i]=p[j];
                                         p[j]=temp;
                                         temp=at[i];
                                         at[i]=at[j];
                                         at[j]=temp;
                             }
                 }
     }

     start[0]=at[0];
     fin [0]=start[0]+bt[0];
     wt[0]=0;
     TAT[0]=fin [0]-at[0];
     tottat=TAT[0];
     for(i=1;i
     {
               start[i]=fin [i-1];
               fin [i]=start[i]+bt[i];
               wt [i]=fin [i-1]-at[i];
               TAT[i]=fin [i]-at[i];
               totwt+= wt[i];
               tottat+=+TAT[i];
     }
     avgwt=totwt/n;
     avgtat=tottat/n;
     printf("Pname\tArrive time\tBurst time\tStart\tTAT\tFinish");
     for(i=0;i
     {
                 printf("\n%d\t%d\t\t%d\t\t%d\t%d\t%d",p[i],at[i],bt[i],start[i],TAT[i],fin [i]);
      }
      printf("\nAverage Waiting=%f",avgwt);
      printf("\nAverage Turn Over Around=%f",avgtat);
     getch();
}

Round Robin
ALGORITHM:

CODE:
#include
#include
#include
#define m 10
void main()
{
         clrscr();
         int bt[m],start=0,est,c, n,p[m],i,q;
         printf("\nEnter the number of Processes:");
         scanf("%d",&n);
         printf("Enter Quantum");
         scanf("%d",&q);
         for(i=0;i
         {
                     printf("Enter Process name,Burst Time ");
                     scanf("%d %d",&p[i],&bt[i] );
         }
         do{
                     for(i=0;i
                     {
                                 if(bt[i]!=0){
                                             est=start+q;
                                             start=est;
                                             printf("\n%d -> %d",i+1,q);
                                             if(bt[i]>0&&bt[i]>=q)
                                                         bt[i]=bt[i]-q;
                                             else
                                                         bt[i]=0;
                     }
                     for(i=0;i
                     {
                                 if(bt[i]==0)
                                             c++;
                     }



         }while(c!=n);
        printf("\n Total Time Estimated=%d",start);
       getch();
}

Priority
ALGORITHM:

CODE:
#include
#include
#define m 10
void main()
{
         clrscr();
         int bt[m],at[m],start[m],fin [m],TAT[m],wt[m],p[m],pr[m],n,i,j,temp;
         float avgwt,avgtat,totwt,tottat;
         printf("\n Enter number of Processes:");
         scanf("%d",&n);
         for(i=0;i
         {
                     printf("\n Enter Process name,Arrival Time,Burst Time and Priority ");
                     scanf("%d %d %d %d",&p[i],&at[i],&bt[i],&pr[i]);
         }
         for(i=0;i
         {
                     for(j=i+1;j
                     {
                                 if(pr[i]>pr[j])
                                 {
                                             temp=pr[i];
                                             pr[i]=pr[j];
                                             pr[j]=temp;
                                             temp=bt[i];
                                             bt[i]=bt[j];
                                             bt[j]=temp;
                                             temp=p[i];
                                             p[i]=p[j];
                                             p[j]=temp;
                                             temp=at[i];
                                             at[i]=at[j];
                                             at[j]=temp;
                                 }
                     }
         }

         start[0]=at[0];
         fin [0]=start[0]+bt[0];
         wt[0]=0;
         TAT[0]=fin [0]-at[0];
         tottat=TAT[0];
         for(i=1;i
         {
      start[i]=fin [i-1];
      fin [i]=start[i]+bt[i];
      wt [i]=fin [i-1]-at[i];
      TAT[i]=fin [i]-at[i];
      totwt+= wt[i];
      tottat+=+TAT[i];
         }
         avgwt=totwt/n;
         avgtat=tottat/n;
         printf("Pname\tArrive time\tBurst time\tPriority\tWaiting time\tTAT");
         for(i=0;i
         {
                     printf("\n%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d",p[i],at[i],bt[i],pr[i],wt[i],TAT[i]);
          }
          printf("\nAverage Waiting=%f",avgwt);
          printf("\nAverage Turn Over Around=%f",avgtat);
         getch();
}