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