Sunday, November 27, 2011

Implementation of Midpoint Circle Algorithm

 The Algorithm
 
function line(x0, y0, x1, y1)
   dx := abs(x1-x0)
   dy := abs(y1-y0) 
   if x0 < x1 then sx := 1 else sx := -1
   if y0 < y1 then sy := 1 else sy := -1
   err := dx-dy
 
   loop
     setPixel(x0,y0)
     if x0 = x1 and y0 = y1 exit loop
     e2 := 2*err
     if e2 > -dy then 
       err := err - dy
       x0 := x0 + sx
     end if
     if e2 <  dx then 
       err := err + dx
       y0 := y0 + sy 
     end if
   end loop
 
 
 The Program 
 
 #include<iostream.h>
     #include<conio.h>
     #include<graphics.h>
     #include<math.h>
     #include<dos.h>
     void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c;
      cout<<"enter the coordinates of the line in the form x1 x2 y1 y2\n";
      cin>>x1>>y1>>x2>>y2;
      int gdriver=DETECT,gmode;
      initgraph(&gdriver,&gmode,"");
      x=x1;y=y1;
      dx=abs(x2-x1);
      dy=abs(y2-y1);
      if(x2>x1)
 s1=1;
 else s1=-1;
      if(y2>y1)
      s2=1;
      else s2=-1;
  if(dy>dx)
  { temp=dx;
  dx=dy;
  dy=temp;
  c=1;
  }
  else c=0;

  e=2*dy-dx;
  for(i=1;i<=dx;i++)
  {  delay(40);
   putpixel(x,y,10);
    while(e>0)
     { if(c==1)
 { x+=s1;}
       else y=y+s2;
 e-=2*dx;
     }
    if(c==1)
     y+=s2;
    else
     x=x+s1;
  e+=2*dy;
  }
  getch();
}

Wednesday, November 23, 2011

Implementation of DDA Line Drawing Algorithm

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void main()
{
clrscr();
float x,y;int x1,x2,y1,y2,dx,dy,l,i=0;
cout<<"Enter the coordinates in the form of x1,y1,x2,y2";
cin>>x1>>y1>>x2>>y2;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
if(abs(x2-x1)>abs(y2-y1))
l=abs(x2-x1);
else
l=abs(y2-y1);
dx=(x2-x1)/l;
dy=(y2-y1)/l;
x=x+0.5;
y=y+0.5;
while(i<=l)
{
putpixel(int(x),int(y),9);
x=x+dx;
y=y+dy;
i++;
}
getch();
}

Implementation of Bresenham's Algorithm For Line Drawing

 The Algorithm
 
function line(x0, y0, x1, y1)
   dx := abs(x1-x0)
   dy := abs(y1-y0) 
   if x0 < x1 then sx := 1 else sx := -1
   if y0 < y1 then sy := 1 else sy := -1
   err := dx-dy
 
   loop
     setPixel(x0,y0)
     if x0 = x1 and y0 = y1 exit loop
     e2 := 2*err
     if e2 > -dy then 
       err := err - dy
       x0 := x0 + sx
     end if
     if e2 <  dx then 
       err := err + dx
       y0 := y0 + sy 
     end if
   end loop
 
 
 The Program 
 
 #include<iostream.h>
     #include<conio.h>
     #include<graphics.h>
     #include<math.h>
     #include<dos.h>
     void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c;
      cout<<"enter the coordinates of the line in the form x1 x2 y1 y2\n";
      cin>>x1>>y1>>x2>>y2;
      int gdriver=DETECT,gmode;
      initgraph(&gdriver,&gmode,"");
      x=x1;y=y1;
      dx=abs(x2-x1);
      dy=abs(y2-y1);
      if(x2>x1)
 s1=1;
 else s1=-1;
      if(y2>y1)
      s2=1;
      else s2=-1;
  if(dy>dx)
  { temp=dx;
  dx=dy;
  dy=temp;
  c=1;
  }
  else c=0;

  e=2*dy-dx;
  for(i=1;i<=dx;i++)
  {  delay(40);
   putpixel(x,y,10);
    while(e>0)
     { if(c==1)
 { x+=s1;}
       else y=y+s2;
 e-=2*dx;
     }
    if(c==1)
     y+=s2;
    else
     x=x+s1;
  e+=2*dy;
  }
  getch();
}

Monday, November 21, 2011

Implementation of First Come First Serve Sceduling in c++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
 struct process
 {  float t ;
    int i;
 };
 int main()
 { process p[4];
  int n=4,temp;
  float t[4],w[4],ta=0,wa=0;
  clrscr();
  for( int j=0;j<n;j++)
  { cout<<"\n enter the the burst time of process p"<<j+1<<" : ";
  cin>>p[j].t;  }
  t[0]=p[0].t;
  for(j=1;j<4;j++)
  { t[j]=t[j-1]+p[j].t;
  }
   for (j=0;j<4;j++)
   { w[j]=t[j]-p[j].t;
   ta+=t[j];
   wa+=w[j];
   p[j].i=j;
   }
   // tubular display
   cout<<"\n\n        FCFS ALGO OF 4 PROCESSES \n    (\"All times are in milliseconds)\n\n\n -: RESULT :-\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n\n     PROCESS     |     BURSTS    |    TAT     |    WT      |  \n\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n";
   for(j=0;j<4;j++)
   {  cout<<"        "<<p[j].i+1<<"              "<<p[j].t<<"            "<<t[j]<<"            "<<w[j]<<endl;
     for(int m=0;m<60;m++)
     cout<<"-";
     cout<<endl;
   }
        float taa=ta/4,waa=wa/4;
   cout<<"\n\r      AVERAGE    :|                   "<<taa<<"           "<<waa;;
   cout<<endl;
   for(int m=0;m<60;m++)
   cout<<"-";
   getch();
   return 0;
   }

Implementation of Shortest Job First Scheduling in c++

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct process
{int t;
 int i;
};
int main()
{  process p[4];
   int n=4,temp ,tempi;
   float w[4],t[4],ta=0,wa=0;
   clrscr();
   for(int j=0;j<4;j++)
   { cout<<"Enter the Burst Time of the process"<<j+1<<"  :";
     cin>>p[j].t;
     p[j].i=j;
   }
   for(j=n-1;j>0;j--)
   { for(int k=0;k<j;k++)
      { if(p[k].t>p[k+1].t)
      { temp=p[k+1].t;
        tempi=p[k+1].i;

        p[k+1].t=p[k].t;
        p[k+1].i=p[k].i;

        p[k].t=temp;
        p[k].i=tempi;
      }
       }
   }

   t[0]=p[0].t;
   for(j=1;j<4;j++)
    t[j]=t[j-1]+p[j].t;
   for(j=0;j<4;j++)
   {w[j]=t[j]-p[j].t;
    ta+=t[j];
    wa+=w[j];
   }

   /* for(j=0;j<4;j++)
   {cout<<"\n THE TAT OF THE PROCESS P"<<p[j].i<<"is  :- "<<t[j]<<"ms";
   }
   cout<<"\n\n\n\n\n\n";
   for(j=0;j<4;j++)
      {cout<<"\n THE WT OF PROCESS P"<<p[j].i<<"is  :-"<<w[j]<<"ms";
      }
   cout<<"\n\n\n\n\n\n\n";
   for(j=0;j<4;j++)
   { cout<<"The Given BURST Of the process P"<<p[j].i<<"is  :-"<<p[j].t<<"ms";
   }
   getch();

   // tabular display
   clrscr();*/

   cout<<"\n\n       SJF ALGO of 4 processes \n    (All times are in milliseconds)\n\n  -: RESULT  :-\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n\n PROCESS    | BURSTS   |  WT  |    TAT    |  \n\n";
   for(j=0;j<60;j++)
      cout<<"-";
      cout<<"\n";

   for(j=0;j<4;j++)
   { cout<<"     "<<p[j].i+1<<"          "<<p[j].t<<"          "<<w[j]<<"        "<<t[j]<<"\n";
     for(int m=0;m<60;m++)
     cout<<"-";
     cout<<"\n";
   }float atat=ta/4,awt=wa/4;
   cout<<"\r  AVERAGE  :  |            "<<awt<<"        "<<atat<<endl;
   for(j=0;j<60;j++)
       cout<<"-";
   getch();
   return 0;
}

Implementation of Priority Sceduling in c++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct process
{int pr,i;
float t;};

void main()
{ process p[4];
  int n=4,m,temp,tempi,temppr;
  float T[4],w[4],ta=0,wa=0;
  clrscr();
  for(int j=0;j<4;j++)
     {  cout<<"\n Enter the burst time of process P"<<j+1<<" :";
         cin>>p[j].t;
         p[j].i=j;
        cout<<"\nEnter the priority of process P"<<j+1<<" :";
        cin>>p[j].pr;
     }
  clrscr();
  for(j=n-1;j>0;j--)
    { for(int k=0;k<j;k++)
      { if(p[k].pr>p[k+1].pr)
      {
    temp=p[k+1].t;
    tempi=p[k+1].i;
    temppr=p[k+1].pr;
    p[k+1].t=p[k].t;
    p[k+1].i=p[k].i;
    p[k+1].pr=p[k].pr;
    p[k].t=temp;
    p[k].i=tempi;
    p[k].pr=temppr;
      }
    }
    }

    T[0]=p[0].t;
    for(j=1;j<=3;j++)
       T[j]=T[j-1]+p[j].t;
    for(j=0;j<4;j++)
      {w[j]=T[j]-p[j].t;
       ta=ta+T[j];
       wa=wa+w[j];
      }

cout<<"\n\n\n\n\n\n\n";
for(j=0;j<4;j++)
    {cout<<"\nThe BURSTS of process P"<<p[j].i+1<<"is :-"<<p[j].t<<"ms with PRIORITY="<<p[j].pr;
    }
getch();
clrscr();
cout<<"\n\n                    PRIORITY ALGO OF 4 PROCESS \n                (\"All times are in milliseconds\")\n\n\n\n\n      -:RESULT:-\n";
for(j=0;j<76;j++)
cout<<"-";
cout<<"\n";
cout<<"|  Process |  Burst Time    |   Priority    |   TAT     |     WT   |";
for(j=0;j<4;j++)
{
cout<<"\n    "<<p[j].i+1<<"          "<<p[j].t<<"               "<<p[j].pr<<"         "<<T[j]<<"            "<<w[j]<<endl;
for(m=0;m<76;m++)
cout<<"-";
cout<<endl;
}
ta=ta/4;
wa=wa/4;
cout<<"\n";
cout<<"\rAVERAGE TAT & WT ARE GIVEN AS :   |              "<<ta<<"            "<<wa;
cout<<endl;
for(m=0;m<76;m++)
cout<<"-";
getch();
}