Sunday, November 27, 2011

Implementation of Cohen-Sutherland Line clipping Algorithm

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
const int t=1, b=2, r=4, l=8 ;
float xmin,ymin,xmax,ymax;
int calcode (float x,float y)
{ int code =0;
  if(y> ymax) code |=t;
  else if(y<ymin) code |= b;
  else if(x>xmax) code |= r;
  else if(x<xmin) code |= l;
  return(code);
}

void lineclip(float x1,float y1,float x2,float y2)
{ unsigned int code1,code2,codeout;
  int accept = 0, done=0;
  code1 = calcode(x1,y1);
  code2 = calcode(x2,y2);
  do{ if(!(code1 | code2))
    { accept =1 ; done =1; }
    else if(code1 & code2) done = 1;
    else
    { float x,y;
       codeout = code1 ? code1 : code2;
       if(codeout & t)
    { x = x1 + (x2-x1)*(ymax-y1)/(y2-y1);y = ymax;}
       else if(codeout & b)
    {x=x1+(x2-x1)*(ymin-y1)/(y2-y1);y=ymin;}
       else if (codeout & r)
      {y=y1+(y2-y1)*(xmax-x1)/(x2-x1);x=xmax;}
       else
     {y=y1+(y2-y1)*(xmin-x1)/(x2-x1);x=xmin;}
       if(codeout == code1)
      {x1 = x; y1 = y;
      code1=calcode(x1,y1);}
       else
    {x2 = x; y2 = y;
     code2 = calcode(x2,y2);}
   }
  } while( done == 0);
  if(accept)
    line(x1,y1,x2,y2);
    rectangle(xmin,ymin,xmax,ymax);
}

main()
{ float x1,y1,x2,y2;
  int gd=DETECT,gm;
  clrscr();
  initgraph(&gd,&gm,"");
  cout<<"\n\n\t:::Enter the co-ordinates of Line::::\n\tx1 :";cin>>x1;
  cout<<"\n\ty1 :";cin>>y1;
  cout<<"\n\tx2 :";cin>>x2;
  cout<<"\n\ty2 :";cin>>y2;
  cout<<"\n\t:::Enter the co_ordinates of window:::\n ";
  cout<<"\n\txmin :";cin>>xmin;
  cout<<"\n\tymin :";cin>>ymin;
  cout<<"\n\txmax :";cin>>xmax;
  cout<<"\n\tymax :";cin>>ymax;
  clrscr();
  line(x1,y1,x2,y2);
  rectangle(xmin,ymin,xmax,ymax);
  getch();
  clrscr();
  lineclip(x1,y1,x2,y2);
  getch();
  closegraph();
  return 0;
}

Implementation of Sutherland–Hodgman Polygon Clipping Algorithm

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum  {  TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode code1,code2,codeout;
int accept = FALSE,done = FALSE;
code1 = CompOutCode(x0,y0);
code2 = CompOutCode(x1,y1);
do
{
if(!(code1|code2))
{
accept = TRUE;
done = TRUE;
}
else
if(code1 & code2)
done = TRUE;
else
{
float x,y;
 
codeout = code1?code1:code2;
if(codeout & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(codeout & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(codeout & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(codeout==code1)
{
x0 = x;
y0 = y;
code1 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
code2 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
 
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}
void main( )
{float x1,y1,x2,y2;
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{scanf("%d",&poly[i]);}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
initgraph(&gdriver, &gmode, "");
 
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
}

program to show movement of circle along a line

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
// function for drawing circle of radius r having centre at m,n
void draw(int m,int n,int r)
{int x,y,p;
x=0;
y=r;
putpixel(m+y,n+x,1);
putpixel(m-y,n+x,1);
putpixel(m+x,n+y,1);
putpixel(m+x,n-y,1);
p=3-(2*r);
while(y>x)
{  if (p<0)
   p=(p+(4*x)+6);
   else
   { y=y-1;
     p=p+((4*(x-y)+10));
   }
   x++;
   putpixel(m+x,n+y,1);
   putpixel(m+x,n-y,1);
   putpixel(m-x,n-y,1);
   putpixel(m-x,n+y,1);
   putpixel(m-y,n-x,1);
   putpixel(m-y,n+x,1);
   putpixel(m+y,n+x,1);
   putpixel(m+y,n-x,1);
}
}

  void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c,r;
       cout<<"Enter the radius ";
       cin>>r;
      cout<<"enter the end points of line to move the circle along ( 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);
      clrscr();
      // calling function draw the circle
      draw(x,y,r);
      line(x1,y1,x,y);
    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();
}


implementation of concept of rotation in computer graphics


 #include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
double object[3][3],translate[3][3],output[3][3], rotate[3][3],output2[3][3],output3[3][3];
int n;
void input(int n)
{for(int i=0;i<n;i++)
     {cout<<"\nenter x and y for cordinate #"<<i+1<<endl;
      cin>>object[i][0]>>object[i][1];
     }
}
void matmulti(double a[10][3],double b[3][3],double c[10][3])
{for(int i=0;i<3;i++)
  for(int j=0;j<3;j++)
   for(int k=0;k<3;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(double a[10][3],int n)
{ for(int i=0;i<n-1;i++)
      line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
       line(a[0][0],a[0][1],a[n-1][0],a[n-1][1]);
}


void main()
{int i,j,ch,gd=DETECT,gm;
double theta;
translate[0][0]=1;
translate[1][1]=1;
translate[2][2]=1;
object[0][2]=1;
object[1][2]=1;
object[2][2]=1;


 cout<<"\n 1.rotate a line";
 cout<<"\n 2.rotate a triangle";
 cout<<"\n enter your choice : ";
 cin>>ch;
 if(ch==1)
   n=2;
 if(ch==2)
   n=3;
 input(n);
 initgraph(&gd,&gm,"");
 display(object,n);
 getch();
 closegraph();
 cout<<"\nenter the fix points tx and ty\n";
 cin>>translate[2][0]>>translate[2][1];
 cout<<"\nenter the angle to rotate";
 cin>>theta;theta=-theta;
 theta=(3.14)*theta/180;
 rotate[0][0]=cos(theta);
 rotate[0][1]=sin(theta);
 rotate[1][0]=-sin(theta);
 rotate[1][1]=cos(theta);
 rotate[2][2]=1;

 translate[2][0]=(-translate[2][0]);
 translate[2][1]=(-translate[2][1]);
 matmulti(translate,rotate,output);
 translate[2][0]=(-translate[2][0]);
 translate[2][1]=(-translate[2][1]);
 matmulti(output,translate,output2);
 matmulti(object,output2,output3);

  initgraph(&gd,&gm,"");
 display(object,n);
 setcolor(12);
 display(output3,n);
 getch();

}

implementation of concept of translation in computer graphics

Program in c for 2-D Translation of line and Triangle

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
int object[3][3],translate[3][3],output[3][3],n;
void input(int n)
{for(int i=0;i<n;i++)
     {cout<<"\nenter x and y for cordinate #"<<i+1<<endl;
      cin>>object[i][0]>>object[i][1];
     }
}    
void matmulti(int a[10][3],int b[3][3],int c[10][3])
{for(int i=0;i<3;i++)
  for(int j=0;j<3;j++)
   for(int k=0;k<3;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(int a[10][3],int n)
{ for(int i=0;i<n-1;i++)
      line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
       line(a[0][0],a[0][1],a[n-1][0],a[n-1][1]);
}     
     

void main()
{int i,j,ch,gd=DETECT,gm;
translate[0][0]=1;
translate[1][1]=1;
translate[2][2]=1;
object[0][2]=1;
object[1][2]=1;
object[2][2]=1;


 cout<<"\n 1.translate a line";
 cout<<"\n 2.translate a triangle";
 cout<<"\n enter your choice : ";
 cin>>ch;
 if(ch==1)
   n=2;
 if(ch==2)
   n=3;
 input(n);
 initgraph(&gd,&gm,"");
 display(object,n);
 getch();
 closegraph();
 cout<<"\nenter the translation factors tx and ty\n";
 cin>>translate[2][0]>>translate[2][1];
 matmulti(object,translate,output);
  initgraph(&gd,&gm,"");
 display(object,n);
 setcolor(12);
 display(output,n);
 getch();

}

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();
}

Monday, October 17, 2011

Implementation of 2-D transformations

C program for translations-scaling -rotation-reflection

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
long double object[10][10],translate[10][10],translate2[10][10],output[10][10],theta;
long double scale[10][10], mirror[10][10],rotate[10][10],output2[10][10],output3[10][10];
int n ,i,j,k;
void init(long double a[10][10])
{ for(i=0;i<10;i++)
     for(j=0;j<10;j++)
     a[i][j]=0;
}
void input(long double a[10][10],int b)
{for( i=0;i<b;i++)
     {cout<<"\nenter row  #"<<i+1<<endl;
      cin>>a[i][0]>>a[i][1]>>a[i][2];
     }
}
void matmulti(long double a[10][10],long double b[10][10],long double c[10][10])
{for(i=0;i<n;i++)
  for(j=0;j<3;j++)
   for(k=0;k<n;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(long double a[10][10])
{ for(i=0;i<n-1;i++)
      line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
       line(a[0][0],a[0][1],a[n-1][0],a[n-1][1]);
}

void matdisp(long double a[10][10],int b)
{  for(i=0;i<b;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"  ";
   cout<<endl;
  }
}

void matdisp(long double a[10][10],long double b[10][10],long double c[10][10])
{  for(i=0;i<3;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"   ";
   cout<<"           ";
   for(j=0;j<3;j++)
   cout<<b[i][j]<<"   ";
   cout<<"           ";
   for(j=0;j<3;j++)
   cout<<c[i][j]<<"   ";
   cout<<endl;
  }
}
void matdisp(long double a[10][10],long double b[10][10])
{  for(i=0;i<n;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"  ";
   cout<<"              ";
   for(j=0;j<3;j++)
   cout<<b[i][j]<<"  ";
   cout<<endl;
  }
}

int main()
{clrscr();
 int i,j,ch,gd=DETECT,gm;
long double theta;
char choice='n';
 do{
 if(choice=='n')
 {cout<<"\n enter the no of verices in the figure";
  cin>>n;
  cout<<"\ enter the object matrix";
  input(object,n);
 }
  initgraph(&gd,&gm,"");
  display(object);
 getch();
 closegraph();
 cout<<"\n choose an operation";
 cout<<"\n 0.translate ";
 cout<<"\n 1.simple scale";
 cout<<"\n 2.fix point scale";
 cout<<"\n 3.simple rotate";
 cout<<"\n 4.fix point rotate";
 cout<<"\n 5.reflection about x-axis";
  cout<<"\n 6.reflection about y- axis";
 cout<<"\n Enter CHOICE : ";

 cin>>ch;
 if(ch==0)
{ cout<<"\n enter the translation matrix";
  input(translate,3);
  matmulti(object,translate,output3);
  cout<<endl;
  cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
  cout<<"\n translation matrix \n";
  matdisp(translate,3);
  cout<<"\n output matrix \n";
  matdisp(output3,n);
  getch();
}
 if(ch==1)
{ cout<<"\n enter the scaling matrix";
  input(scale,3);
  matmulti(object,scale,output3);
  cout<<endl;
  cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
  cout<<"\n scaling matrix \n";
  matdisp(scale,3);            
  cout<<"\noutput matrix \n";
  matdisp(output3,n);
  getch();

  getch();
}
 if(ch==2)
{ cout<<"\n enter the translation matrix";
  input(translate,3);
  cout<<"\n enter the scaling matrix";
  input(scale,3);
  cout<<"\n enter the inverse translation matrix";
  input(translate2,3);
  cout<<"\n object matrix\n";
  matdisp(object,n);
  cout<<"\n transformation matrixes       \n";
  cout<<"    TRANSLATION            SCALING            DE TRANSLATION  \n";
  matdisp(translate,scale,translate2);
  getch();
  matmulti(translate,scale,output);
  matmulti(output,translate2,output2);
  matmulti(object,output2,output3);
  cout<<"\n output matrix is \n";
  matdisp(output3,n);
  cout<<endl;
  getch();
}
 if(ch==3)
{  cout<<"\n enter the rotation matrix";
   input(rotate,3);
   matmulti(object,rotate,output3);
   cout<<endl;
   cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
    cout<<"\n rotation matrix \n";
  matdisp(rotate,3);
  cout<<"\n output matrix \n";
  matdisp(output3,n);
   getch();
}

 if(ch==4)
{  cout<<"\n enter the translation matrix";
   input(translate,3);
   cout<<"\n enter the rotation matrix";
   input(rotate,3);
   cout<<"\n enter the inverse translation matrix";
   input(translate2,3);
   cout<<"\n object matrix\n";
   matdisp(object,n);
   cout<<"\n transformation matrixes       \n";
   cout<<"    TRANSLATION            ROTATION            DE TRANSLATION  \n";
   matdisp(translate,rotate,translate2);
   getch();
   matmulti(translate,rotate,output);
   matmulti(output,translate2,output2);
   matmulti(object,output2,output3);
   cout<<"\n output matrix is \n";
   matdisp(output3,n);
   cout<<endl;
   getch();
}
if(ch==5)
{ translate[2][1]=-240;
 translate[2][0]=0;
  translate[0][0]=1;
  translate[1][1]=1;
  translate[2][2]=1;
  cout<<"\nenter reflection matrix\n";
  input(mirror,3);
  matmulti(translate,mirror,output);
  translate[2][1]=240;
  matmulti(output,translate,output2);
  matmulti(object,output2,output3);
    cout<<"\n input matrix\n";
  matdisp(object,n);
  getch();
  cout<<"\n mirror matrix\n";
  matdisp(mirror,3);
  getch();
  cout<<"\n output matric\n";
  matdisp(output3,n);
  getch();
}
if(ch==6)
{ translate[2][0]=-320;
 translate[2][1]=0;
  translate[0][0]=1;
  translate[1][1]=1;
  translate[2][2]=1;
  cout<<"\nenter reflection matrix\n";
  input(mirror,3);
  matmulti(translate,mirror,output);
  translate[2][0]=320;
  matmulti(output,translate,output2);
  matmulti(object,output2,output3);
  cout<<"\n input matrix\n";
  matdisp(object,n);
  getch();
  cout<<"\n mirror matrix\n";
  matdisp(mirror,3);
  getch();
  cout<<"\n output matric\n";
  matdisp(output3,n);
  getch();
}
 initgraph(&gd,&gm,"");
 display(object);
 setcolor(12);
 display(output3);
 if(ch==5||ch==6)
 {setcolor(10);
  line(0,240,640,240);
  setcolor(11);
  line(320,0,320,480);
 }
 getch();
 closegraph();
 init(output);
 init(output2);
 init(output3);
 cout<<"\n want to go to main menu  (y/n)\n";
 choice=getche();
}while(choice!='n');
  return 0;
}

Thursday, June 16, 2011

Algorithm and Program for Strassen's matrix multiplication


Algorithm

If the sizes of A and B are less than the threshold
Compute C = AB using the traditional matrix multiplication algorithm.
Else use Strassen's algorithm
Split matrices A and B
For each of

Mi i = 1 to 7
Create a new thread to compute

Mi = A'i B'i
If the sizes of the matrices are less than the threshold
Compute C using the traditional matrix multiplication algorithm.
Else use Strassen's algorithm
Split matrices A'

i and B'i
For each of M

ij j = 1 to 7

If i=7 and j=7 go to step 1 with A = A'77

and
B = B'77
Get a thread from the thread pool to compute

M= A'ij B'ij
Execute the recursive version of Strassen's algorithm in this thread
Wait for the Mij
threads to complete execution
Compute MiWait for the Mi

threads to complete execution
Compute C

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
int p1,p2,p3,p4,p5,p6,p7;
clrscr();
cout<<"enter the first matrix";
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
cin>>a[i][j];
cout<<"enter the second matrix";
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
cin>>b[i][j];
p1=a[1][1]*(b[1][2]-b[2][2]);
p2=(a[1][1]+a[1][2])*b[2][2];
p3=(a[2][1]+a[2][2])*b[1][1];
p4=a[2][2]*(b[2][1]-b[1][1]);
p5=(a[1][1]+a[2][2])*(b[1][1]+b[2][2]) ;
p6=(a[1][2]-a[2][2])*(b[2][1]+b[2][2]) ;
p7=(a[1][1]-a[2][1])*(b[1][1]+b[1][2]) ;
c[1][1]=p5+p4-p2+p6;
c[1][2]=p1+p2;
c[2][1]=p3+p4;
c[2][2]=p5+p1-p3-p7;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n" ;
}
getch();
}

Algorithm and Program for Depth First Search(DFS)

Algorithm
procedure DFS(G,v):
2      label v as explored
3      for all edges e in G.incidentEdges(v) do
4          if edge e is unexplored then
5              w ← G.opposite(v,e)
6              if vertex w is unexplored then
7                  label e as a discovery edge
8                  recursively call DFS(G,w)
9          else
10             label e as a back edge
Program
#include<stdio.h>
#include<conio.h>
#define MAX 5
int dfs(int adj[][MAX],int visited[],int start)
{
    int stack[MAX];
    int top=-1,i;
   
    printf("%c-",start+65);
    visited[start]=1;
    stack[++top]=start;
    while(top!=-1)
    {
    start=stack[top];
    for(i=0;i<MAX;i++)
    {  if(adj[start][i]&&visited[i]==0)
    {stack[++top]=i;
      printf("%c-",i+65);
        visited[i]=1;
        break;                                                            
        }}
      if(i==MAX)
      top--;
    }
    return 0;
}
int main()
{
    int adj[MAX][MAX]={{0,0,1,1,0},{0,0,0,0,0},{0,1,0,1,1},{0,0,0,0,1},{0,0,0,1,0}};
    int visited[MAX]={0};
     printf("DFS Traversal : ");
    dfs(adj,visited,0);
 printf("\n");
    getch();
    return 0;
}

Algorithm and Program for Merge Short

Algorithm

function merge(left,right)
    var list result
    while length(left) > 0 or length(right) > 0
        if length(left) > 0 and length(right) > 0
            if first(left) ≤ first(right)
                append first(left) to result
                left = rest(left)
            else
                append first(right) to result
                right = rest(right)
        else if length(left) > 0
            append first(left) to result
            left = rest(left)
        else if length(right) > 0
            append first(right) to result
            right = rest(right)
    end while
    return result

Program

#include <stdio.h>
#include<conio.h>
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{ int mid;
  if(low<high)
 {mid=(low+high)/2;
  merge_sort(low,mid);
  merge_sort(mid+1,high);
  merge(low,mid,high);
 }
}
void merge(int low,int mid,int high)
{
 int h,i,j,b[50],k;
 h=low;
 i=low;
 j=mid+1;
 while((h<=mid)&&(j<=high))
 {if(a[h]<=a[j])
  {b[i]=a[h];
   h++;
  }
  else
  {b[i]=a[j];
   j++;
  }
  i++;
 }
 if(h>mid)
 {
  for(k=j;k<=high;k++)
  {b[i]=a[k];
   i++;
  }
 }
 else
 {  for(k=h;k<=mid;k++)
       {b[i]=a[k];
        i++;
       }
 }
 for(k=low;k<=high;k++) a[k]=b[k];
}
int main()
{int num,i;
 printf("Please Enter THE NUMBER OF ELEMENTS you want to sort :\n");
 scanf("%d",&num);
 printf("\n");
 printf("Now, Please Enter the ( %d ) numbers :\n",num);
 for(i=1;i<=num;i++)
 {  scanf("%d",&a[i]); }
 merge_sort(1,num);
 printf("\n");
 printf("So, the sorted list (using MERGE SORT) will be :\n");
 printf("\n\n");
 for(i=1;i<=num;i++)
 printf("%d  ",a[i]);
 printf("\n\n\n\n");
getch();
return 0;
}

Algorithm and Program for Quick Short

Algorithm

// left is the index of the leftmost element of the array
  // right is the index of the rightmost element of the array (inclusive)
  // number of elements in subarray: right-left+1
  function partition(array, left, right, pivotIndex)
     pivotValue := array[pivotIndex]
     swap array[pivotIndex] and array[right]  // Move pivot to end
     storeIndex := left
     for i from left to right - 1 // left ≤ i < right
         if array[i] < pivotValue
             swap array[i] and array[storeIndex]
             storeIndex := storeIndex + 1
     swap array[storeIndex] and array[right]  // Move pivot to its final place
     return storeIndex

Program
#include<conio.h>
#include<stdio.h>
void quickshort(int ,int,int);
int partition(int arr[], int left, int right)
{  int i = left, j = right;
   int tmp;
   int pivot = arr[i];
   while (i <= j)
         { while (arr[i] < pivot)
                  i++;
            while (arr[j] > pivot)
                   j--;
            if (i <= j)
               { tmp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = tmp;
                  i++;
                  j--;
               }
         };
   return i;
}
void quickSort(int arr[], int left, int right)
{  int index = partition(arr, left, right);
      if (left < index - 1)
      quickSort(arr, left, index - 1);
      if (index < right)
            quickSort(arr, index, right);
}
int main()
{printf("enter the no of elements in the list\n");
int n,i;
scanf("%d",&n);
int arr[n];
printf("now enter the %d elements of the list ",n);
for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
quickSort(arr,0,7);
printf("\n The shorted list is ");
for(i=0;i<n;i++)
printf("  %d  ",arr[i]);
getch();
    return 0;}

Algorithm and Program for Binary Search


implementation of the algorithm

limitations :
entered list must be pre shorted;
there must be only one element in the list which is to be searched;

#include<stdio.h>
#include<conio.h>
int mid;
int BinarySearch(int A[], int key, int low,int high)
{
       if (high < low)
        return -1;
      mid =low + (high - low)/2;
       if (A[mid] > key)
          return BinarySearch(A,key, low, mid-1);
       else if (A[mid] < key)
          return BinarySearch(A, key, mid+1, high);
       else
           return mid ;
   };
  
   int main()
   {
     
      int j,low=0,high=9,key,k=0;
      int A[9];
       printf("enter the 12 elements of array");
      for(j=0;j<9;j++)
      scanf("%d",&A[j]);
       printf("\n enter the element to be searched for");
      scanf("%d",&key);
      j=BinarySearch(A,key,low,high);
      if (j>=0)
      printf("\element %d is found at position %d",key,j+1); 
      else
      printf("\n element not found");
      getch();
      return 0;
   }
BinarySearch(A[0..N-1], value, low, high) {
       if (high < low)
           return -1 // not found
       mid = low + (high - low) / 2
       if (A[mid] > value)
           return BinarySearch(A, value, low, mid-1)
       else if (A[mid] < value)
           return BinarySearch(A, value, mid+1, high)
       else
           return mid // found
   }

Program to saving data to a database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{

  public partial class Form1 : Form
{
  public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn =new SqlConnection("PASTE CONNECTION STRING HERE");

cn.Open();
SqlCommand cmd = new SqlCommand("insert into emp values ('"+textBox1.Text+"', '"+textBox2.Text+"', "+textBox3.Text+")",cn); 

cmd.ExecuteNonQuery();
MessageBox.Show(" your entries are saved into the database");
}
}

connection string is found in database properties

example of a connection string


("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\manish\\Documents\\niist.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

Program to add two numbers in c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using System.Drawing;
using System.Linq;using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{int a,b,c;
a=Convert.ToInt32(textBox1.Text);
b=Convert.ToInt32(textBox2.Text);
c=a+b;
textBox3.Text = (c.ToString());

}
}
}

Program to retrieve data from a table

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1

   public partial class Form1 : Form
{
   public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

 SqlCommand cmd = new SqlCommand(" select * from EMP where ENAME='" + textBox1.Text + "'",cn);
 SqlDataReader dr = cmd.ExecuteReader();
cn.Open();
dr.Read();
if (dr.HasRows)
{
label2.Text = dr["DESIG"].ToString();
label3.Text = dr["SAL"].ToString();
}
if (!dr.HasRows)
{
MessageBox.Show("ERROR IN RETRIEVING","Error Message"MessageBoxButtons.OKCancel ,  MessageBoxIcon.Warning,  MessageBoxDefaultButton.Button1);
}
}
}
}

connection string can be found in database properties

example of a connection string

("Data Source=.\\SQLEXPRESS;AttachDbFilename= C:\\Users\\manish\\Documents\\niist.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True")SqlConnection cn = new SqlConnection("PASTE CONNECTION STRING HERE");

Thursday, April 21, 2011

Job Sequencing With Deadline

Algorithm:

Step 1: Sort p[i] into non-increasing order. After sorting p[1]>= p[2]>=p[3]>=... p[i.]
Step 2: Add the next job i to the solution set if i can be completed by its deadline.
Assign i to time slot [r-1, r], where r is the largest integer such that 1 <= r <= d[i]
and [r-1, r] is free.
Step 3: Stop if all jobs are examined. Otherwise, go to step 2.

Time complexity: O(n^2)

Implementation:

Program In C

#include<conio.h>
#include<stdio.h>
int n,i,j,k,t;
int check(int s[],int p)
       {  int ptr=0;
           for(int i=0;i<n;i++)
           {if(s[i]==p)
               ptr++;
      }           
            if(ptr==0)
            return 1;
            else
            return 0;
        }    
                     
int main()
{  
     printf("enter the no of jobs      : ");
     scanf("%d",&n);
     int slot[n],profit,p[n],d[n];
     for(i=0;i<n;i++)
       {printf("\n enter the profit of job #%d      :",i+1);
       scanf("%d",&p[i]);                 
       printf("\n enter the deadline of job #%d    :",i+1);
       scanf("%d",&d[i]);
       }
      
     for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
         if(p[i]<p[j])
            { t=p[i];
              p[i]=p[j];
              p[j]=t;
              t=d[i];
              d[i]=d[j];
              d[j]=t;             
            }
              
       for(i=0;i<n;i++)
           slot[i]=0;         
                 
     for(i=0;i<n;i++)
         for(j=d[i];j>0;j--)
             {if(check(slot,j)==1)
                {slot[i]=j;
                break;} 
             }
           
     printf("\n\n INDEX   PROFIT  DEADLINE  SLOT ALLOTTED ");      
     for(i=0;i<n;i++)
     {if(slot[i]>0)
     printf("\n\n   %d       %d        %d        [%d - %d]", i+1,p[i],d[i],(slot[i]-1),slot[i]);
     else
     printf("\n\n   %d       %d        %d       REJECTED", i+1,p[i],d[i]);
     }
              
       getch();
}