Thursday 21 March 2013

rajnikanth amithabh bachchan common letters


#include<stdio.h>
#include<conio.h>
#define MAX 100
main()
{
      char a[]="AMITABH BACHCHAN";
      char b[]="RAJNIKANTH";
      char c[100];
   
     
      int i,j,k=0,flag=0;
     
      for(i=0;i<sizeof(a)-1;i++)
      {
             flag=0;
             for(j=0;j<k;j++)
             {
                         if(a[i]==c[j])
                                       flag=1;  
             }                
             if(flag==1)
                        continue;
             else
             {
                 for(j=0;j<sizeof(b)-1;j++)
                 {
                         if(a[i]==b[j])
                         {
                                       c[k]=a[i];
                                       k++;break;
                         }  
                 }                  
             }
      }
     
      printf("%s",c);
      getch();
         
}

a star shortest path heuristic algorithm



#include <iostream>
#include <iomanip>
#include <queue>
#include <string>
#include <math.h>
#include <ctime>

using namespace std;

const int n=15; // horizontal size of the map
const int m=15; // vertical size size of the map
static int map[n][m];
static int closed_nodes_map[n][m]; // map of closed (tried-out) nodes
static int open_nodes_map[n][m]; // map of open (not-yet-tried) nodes
static int dir_map[n][m]; // map of directions
const int dir=8; // number of possible directions to go at any position
// if dir==4
//static int dx[dir]={1, 0, -1, 0};
//static int dy[dir]={0, 1, 0, -1};
// if dir==8
static int dx[dir]={1, 1, 0, -1, -1, -1, 0, 1};
static int dy[dir]={0, 1, 1, 1, 0, -1, -1, -1};

class node
{
    // current position
    int xPos;
    int yPos;
    // total distance already travelled to reach the node
    int level;
    // priority=level+remaining distance estimate
    int priority;  // smaller: higher priority

    public:
        node(int xp, int yp, int d, int p)
            {xPos=xp; yPos=yp; level=d; priority=p;}
   
        int getxPos() const {return xPos;}
        int getyPos() const {return yPos;}
        int getLevel() const {return level;}
        int getPriority() const {return priority;}

        void updatePriority(const int & xDest, const int & yDest)
        {
             priority=level+estimate(xDest, yDest)*10; //A*
        }

        // give better priority to going strait instead of diagonally
        void nextLevel(const int & i) // i: direction
        {
             level+=(dir==8?(i%2==0?10:14):10);
        }
       
        // Estimation function for the remaining distance to the goal.
        const int & estimate(const int & xDest, const int & yDest) const
        {
            static int xd, yd, d;
            xd=xDest-xPos;
            yd=yDest-yPos;        

            // Euclidian Distance
            d=static_cast<int>(sqrt(xd*xd+yd*yd));

            // Manhattan distance
            //d=abs(xd)+abs(yd);
           
            // Chebyshev distance
            //d=max(abs(xd), abs(yd));

            return(d);
        }
};

// Determine priority (in the priority queue)
bool operator<(const node & a, const node & b)
{
  return a.getPriority() > b.getPriority();
}

// A-star algorithm.
// The route returned is a string of direction digits.
string pathFind( const int & xStart, const int & yStart,
                 const int & xFinish, const int & yFinish )
{
    static priority_queue<node> pq[2]; // list of open (not-yet-tried) nodes
    static int pqi; // pq index
    static node* n0;
    static node* m0;
    static int i, j, x, y, xdx, ydy;
    static char c;
    pqi=0;

    // reset the node maps
    for(y=0;y<m;y++)
    {
        for(x=0;x<n;x++)
        {
            closed_nodes_map[x][y]=0;
            open_nodes_map[x][y]=0;
        }
    }

    // create the start node and push into list of open nodes
    n0=new node(xStart, yStart, 0, 0);
    n0->updatePriority(xFinish, yFinish);
    pq[pqi].push(*n0);
    open_nodes_map[x][y]=n0->getPriority(); // mark it on the open nodes map

    // A* search
    while(!pq[pqi].empty())
    {
        // get the current node w/ the highest priority
        // from the list of open nodes
        n0=new node( pq[pqi].top().getxPos(), pq[pqi].top().getyPos(),
                     pq[pqi].top().getLevel(), pq[pqi].top().getPriority());

        x=n0->getxPos(); y=n0->getyPos();

        pq[pqi].pop(); // remove the node from the open list
        open_nodes_map[x][y]=0;
        // mark it on the closed nodes map
        closed_nodes_map[x][y]=1;

        // quit searching when the goal state is reached
        //if((*n0).estimate(xFinish, yFinish) == 0)
        if(x==xFinish && y==yFinish)
        {
            // generate the path from finish to start
            // by following the directions
            string path="";
            while(!(x==xStart && y==yStart))
            {
                j=dir_map[x][y];
                c='0'+(j+dir/2)%dir;
                path=c+path;
                x+=dx[j];
                y+=dy[j];
            }

            // garbage collection
            delete n0;
            // empty the leftover nodes
            while(!pq[pqi].empty()) pq[pqi].pop();          
            return path;
        }

        // generate moves (child nodes) in all possible directions
        for(i=0;i<dir;i++)
        {
            xdx=x+dx[i]; ydy=y+dy[i];

            if(!(xdx<0 || xdx>n-1 || ydy<0 || ydy>m-1 || map[xdx][ydy]==1
                || closed_nodes_map[xdx][ydy]==1))
            {
                // generate a child node
                m0=new node( xdx, ydy, n0->getLevel(),
                             n0->getPriority());
                m0->nextLevel(i);
                m0->updatePriority(xFinish, yFinish);

                // if it is not in the open list then add into that
                if(open_nodes_map[xdx][ydy]==0)
                {
                    open_nodes_map[xdx][ydy]=m0->getPriority();
                    pq[pqi].push(*m0);
                    // mark its parent node direction
                    dir_map[xdx][ydy]=(i+dir/2)%dir;
                }
                else if(open_nodes_map[xdx][ydy]>m0->getPriority())
                {
                    // update the priority info
                    open_nodes_map[xdx][ydy]=m0->getPriority();
                    // update the parent direction info
                    dir_map[xdx][ydy]=(i+dir/2)%dir;

                    // replace the node
                    // by emptying one pq to the other one
                    // except the node to be replaced will be ignored
                    // and the new node will be pushed in instead
                    while(!(pq[pqi].top().getxPos()==xdx &&
                           pq[pqi].top().getyPos()==ydy))
                    {              
                        pq[1-pqi].push(pq[pqi].top());
                        pq[pqi].pop();      
                    }
                    pq[pqi].pop(); // remove the wanted node
                   
                    // empty the larger size pq to the smaller one
                    if(pq[pqi].size()>pq[1-pqi].size()) pqi=1-pqi;
                    while(!pq[pqi].empty())
                    {              
                        pq[1-pqi].push(pq[pqi].top());
                        pq[pqi].pop();      
                    }
                    pqi=1-pqi;
                    pq[pqi].push(*m0); // add the better node instead
                }
                else delete m0; // garbage collection
            }
        }
        delete n0; // garbage collection
    }
    return ""; // no route found
}

int main()
{
    srand(time(NULL));

    // create empty map
    for(int y=0;y<m;y++)
    {
        for(int x=0;x<n;x++) map[x][y]=0;
    }

    // fillout the map matrix with a '+' pattern
    for(int x=n/8;x<n*7/8;x++)
    {
        map[x][m/2]=1;
    }
    for(int y=m/8;y<m*7/8;y++)
    {
        map[n/2][y]=1;
    }
   
    // randomly select start and finish locations
    int xA, yA, xB, yB;
    switch(rand()%8)
    {
        case 0: xA=0;yA=0;xB=n-1;yB=m-1; break;
        case 1: xA=0;yA=m-1;xB=n-1;yB=0; break;
        case 2: xA=n/2-1;yA=m/2-1;xB=n/2+1;yB=m/2+1; break;
        case 3: xA=n/2-1;yA=m/2+1;xB=n/2+1;yB=m/2-1; break;
        case 4: xA=n/2-1;yA=0;xB=n/2+1;yB=m-1; break;
        case 5: xA=n/2+1;yA=m-1;xB=n/2-1;yB=0; break;
        case 6: xA=0;yA=m/2-1;xB=n-1;yB=m/2+1; break;
        case 7: xA=n-1;yA=m/2+1;xB=0;yB=m/2-1; break;
    }
    cout<<"\n";
    cout<<"\t\tMap Size (X,Y): "<<n<<","<<m<<endl;
    cout<<"\t\tStart: "<<xA<<","<<yA<<endl;
    cout<<"\t\tFinish: "<<xB<<","<<yB<<endl;
    // get the route
    clock_t start = clock();
    string route=pathFind(xA, yA, xB, yB);
    if(route=="") cout<<"An empty route generated!"<<endl;
    clock_t end = clock();
    double time_elapsed = double(end - start);
    cout<<"\t\tTime to calculate the route (ms): "<<time_elapsed<<endl;
    cout<<"\t\tRoute:"<<endl;
    cout<<"\t\t";
    cout<<route<<endl<<endl;

    // follow the route on the map and display it
    if(route.length()>0)
    {
        int j; char c;
        int x=xA;
        int y=yA;
        map[x][y]=2;
        for(int i=0;i<route.length();i++)
        {
            c =route.at(i);
            j=atoi(&c);
            x=x+dx[j];
            y=y+dy[j];
            map[x][y]=3;
        }
        map[x][y]=4;
   
        // display the map with the route
        for(int y=0;y<m;y++)
        {
            for(int x=0;x<n;x++)
                if(map[x][y]==0)
                    cout<<".";
                else if(map[x][y]==1)
                    cout<<"O"; //obstacle
                else if(map[x][y]==2)
                    cout<<"S"; //start
                else if(map[x][y]==3)
                    cout<<"R"; //route
                else if(map[x][y]==4)
                    cout<<"F"; //finish
            cout<<endl;
        }
    }
    getchar(); // wait for a (Enter) keypress
    return(0);
}

scanner lexical compiler first phase


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 32
int h[MAX]={0};
int keyw[MAX]={0};
char ident[20][30];


char key[MAX][30]={{'s','t','r','u','c','t'}
,{'v','o','i','d'}
,{'a','u','t','o'}
,{'b','r','e','a','k'}
,{'c','h','a','r'}
,{'c','o','n','s','t'}
,{'c','o','n','t','i','n','u','e'}
,{'d','e','f','a','u','l','t'}
,{'d','o','u','b','l','e'}
,{'e','n','u','m'}
,{'e','x','t','e','r','n'}
,{'f','l','o','a','t'}
,{'f','o','r'}
,{'g','o','t','o'}
,{'l','o','n','g'}
,{'r','e','g','i','s','t','e','r'}
,{'s','h','o','r','t'}
,{'s','i','g','n','e','d'}
,{'s','t','a','t','i','c'}
,{'u','n','i','o','n'}
,{'v','o','l','a','t','i','l','e'}
,{'u','n','s','i','g','n','e','d'}
,{'i','n','t'}
,{'t','y','p','e','d','e','f'}
,{'e','l','s','e'}
,{'i','f'}
,{'d','o'}
,{'c','a','s','e'}
,{'s','w','i','t','c','h'}
,{'w','h','i','l','e'}
,{'s','i','z','e','o','f'}
,{'r','e','t','u','r','n'}};
void display(int x)
{
int i=0;
char c;
// printf("  Key:");
   printf("\n");
do
{
printf("%c",key[x][i]);
i++;
}while(i!=20);
}

int main()
{
int keys=0;  
    FILE *f;
    f=fopen("tree.txt","r");
   
    if(f==NULL)
    {
               printf("\nFAIL");
               getch();
             
    }
   
    char c,id[20];
    int i,resulti=0,resultk=0,flag,x,z,count=0,l=1,no_of_identifiers=0;
    int brackets=0,string=0;
    printf("Tokens\n");
    while((c=getc(f))!=EOF)
    {
        if(c=='"' && string==0)
        {
                  printf("\n\"");
                  string=1;
        }
        else if(c=='"' && string==1)
         {
                           string=0;
                           printf("\n\"");
         }            
        if(c=='(' && brackets==0)
                  brackets=1;
        else if(c==')' && brackets==1)
                       brackets=0;
       
       
        if(string==0)
        {                  
        if(count==0 && (isalnum(c)-isalpha(c)))
        {
                 
                    resultk=-1;
                    resulti=-1;
        }
       
         flag=0;                
         if(isalpha(c))
{
            if(resultk!=-1)
            {        
          for(i=0;i<MAX;i++)
          {
            if(h[i]==count && c==key[i][count])
           {
                                h[i]+=1;
                                    flag=1;                            
                            }  
                       }
                       if(flag==0)
                                  resultk=-1;      
            }
id[count]=c;
++count;      
        }
        else if(c=='\n')
        {
            for(i=0;i<MAX;i++)
                    h[i]=0;
           
           resultk=0;
           count=0;
           l++;
      }
      else if(c==' ')
{
                   if(count!=0)
                   {
                  for(x=0;x<MAX;x++)
       {
      if(h[x]==count)
      {
                                 
                                   keyw[keys++]=x;
                                   //no_of_keys+=1;
                  // printf("\nline:%d",l);
           display(x);
                     }            
                  }
                  for(i=0;i<MAX;i++)
                    h[i]=0;
                  resultk=0;
                  resulti=0;
     count=0;
                           
                }
        }
     
        else if(c==',')
        {
             if(count!=0)
             {
             //printf("\nline:%d  identifier:",l);
             printf("\n");
             for(i=0;i<count;i++)
             {
                        printf("%c",id[i]);        
                        ident[no_of_identifiers][i]=id[i];
             }
             no_of_identifiers++;
                        //printf("%c",id[i]);
              }
             count=0;
             resulti=0;              
                 
        }
        else if(c=='=')
        {
           
             if(count!=0)
             {
             //printf("\nline:%d  identifier:",l);
             printf("\n");
             for(i=0;i<count;i++)
             {
                        printf("%c",id[i]);        
                        ident[no_of_identifiers][i]=id[i];
             }
             no_of_identifiers++;
              }
               printf("\n=");
             count=0;
             resulti=0;              
                 
        }
        else if(c=='(' || c==')')
        {
             if(count>1)
             {
             //printf("\nline:%d  identifier:",l);
             printf("\n");
             for(i=0;i<count;i++)
             {
                        printf("%c",id[i]);        
                        ident[no_of_identifiers][i]=id[i];
             }
             no_of_identifiers++;
              }
               if(c=='(')
           {
                printf("\n(");
                count=0;
           }
           else if(c==')')
           {
                printf("\n)");
                count=0;
           }
             count=0;
             resulti=0;              
                 
        }
        else if(c==';' && brackets==0)
        {
         
             if(resulti!=-1)
             {
                if(count!=0)
                {          
             //printf("\nline:%d identifier:",l);
             printf("\n");
            for(i=0;i<count;i++)
             {
                        printf("%c",id[i]);        
                        ident[no_of_identifiers][i]=id[i];
             }
             no_of_identifiers++;
                }
             }
             printf("\n");
              for(i=0;i<count;i++)
                        printf("%c",id[i]);
                printf("\n;");
             count=0;
             resulti=0;              
           
        }
        else if(isalnum(c)-isalpha(c))
        {
             id[count]=c;
             count++;
        }
       else
       {
         
          count++;
          resultk=-1;
          resulti=-1;
          if(c=='#')
            {         printf("\n#");
                      count=0;
            }
           else if(c=='<')
           {
                printf("\ninclude\n<\n");
                count=0;
           }
           else if(c=='.')
           {
                for(i=0;i<count-1;i++)
                        printf("%c",id[i]);
                printf("\n.\nh\n>");
                count=0;
           }
           else if(c=='{')
           {
                printf("\n{");
                count=0;
           }
           else if(c=='}')
           {
                printf("\n}");
                count=0;
           }
             else if(c=='&')
           {
                printf("\n&");
                count=0;
           }
      }
                 
      }
}



    //  printf("\n\nSYMBOL TABLE\n\n\n\n");

      printf("\n\nKey\t\tidentifiers\n");
     
      int j,max=no_of_identifiers;
      if(max<keys)
                        max=keys;
                       
     
      for(i=0;i<max;i++)
      {
   
                        if(i<keys-1)
                        {
                                  display(keyw[i]);
                         }
                                           printf("\n\t\t");
                                         
                        if(i<=no_of_identifiers)
                        {
                                    for(j=0;j<10;j++)
                                                  printf("%c",ident[i][j]);                
                        }
      }





     getch();
}

heap sort


#include<stdio.h>
#include<conio.h>
int heapsize=10,ar[10];
void swap(int largest,int i)
{    int temp;
     temp=ar[largest];
     ar[largest]=ar[i];
     ar[i]=temp;
}
void heapify(int i)
{    int l,r,largest,test=0;
     l=i*2;
     r=l+1;
     if(l<=heapsize&&ar[i-1]<ar[l-1])
     {largest=l;}
     else
     largest=i;
     if(r<=heapsize&&ar[largest-1]<ar[r-1])
     {largest=r;}
   
     if(largest!=i)
     {swap(largest-1,i-1);
     heapify(largest);}
     }
void build_heap()
{    int i;
     for(i=5;i>0;i--)
     heapify(i);
}

main()
{     printf("enter the values");
      int i,j;
      for(i=0;i<10;i++)
      scanf("%d",&ar[i]);
      build_heap();
      //heapsort
      //heapify(2);
      for(i=10;i>1;i--)
      {              

                      swap(0,i-1);
                     
                      heapsize--;
                      //j=heapsize>>2;
                      heapify(1);
                     
                      }
      for(i=0;i<10;i++)
      printf("%d \n",ar[i]);
      getch();
}

insertion sort

file open is used.you can change it accordingly.assuming you know little bit of c coding.



#include<stdio.h>
#include<conio.h>
#include<time.h>
int main()
{
FILE *f;
f=fopen("INPUT.TXT","r");

    int i,a[10],n;
   
    printf("Enter no of Elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
                  scanf("%d",&a[i]);
   
    int j,temp;
    time_t stime=time(NULL);
    for(i=1;i<=n-1;i++)
    {
     for(j=0;j<i;j++)
     {
      if(a[i]>a[j])
      {
       a[i]=(a[i]+a[j])-(a[j]=a[i]);
      }
     }
    }
    time_t ftime=time(NULL);
    printf("\nSorted time:%ld\n",difftime(ftime,stime));
    for(i=0;i<n;i++)
    printf("%d\n",a[i]);
    getch();
}

two stack queue c code


#include<stdio.h>
#include<conio.h>
#define MAX 10
int i=0;

void push(int *a,int v)
{
     if(i==MAX-1)
     {
         printf("\nOverflow\n");
     }
     else
     {
         i++;
         a[i]=v;
     }
   
}

int pop(int a[])
{
    int v;
    if(i==0)
            printf("\nUnderflow\n");
    else
    {
        v=a[i];
        i--;
    }
    return v;
}

int popq(int a[],int b[])
{
     int j;
     for(j=0;j<i;j++)
     {
            push(b,pop(a));
     }
     return pop(b);
     for(j=0;j<i;j++)
     {
            push(a,pop(b));
     }
   
}

void display(int a[])
{
     int j;
     for(j=0;j<i;j++)
     {
          printf("%d\n",a[j]);
     }
}

int main()
{
    int a[MAX],b[MAX];
    printf("\n2s Queue\n");
    int choice,v;
    do
    {
        printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\nEnter choice:");
        scanf("%d",&choice);
        switch(choice)
        {
                      case 1:printf("\nenter value:");
                           scanf("%d",&v);                
                           push(a,v);
                           break;
                      case 2:v=popq(a,b);
                           printf("\n%d dequeued",v);
                           break;
                      case 3:display(a);
                           break;
                      case 4:break;
                      default:printf("\nEnter correct option..!");
        }      
    }while(choice!=4);
    getch();
}

nilu.c computer graphics


# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
int first_end_point_region(int x,int y);

int xmin,ymin,xmax,ymax;
void main()
{
    int x1,y1,x2,y2;
    int gdriver = DETECT, gmode, errorcode;
    int findRegionP1(int,int);
    void clipline1(int,int,int,int);
    void clipline2(int,int,int,int);
    void clipline3(int,int,int,int);
    int ch;
    float m;
    clrscr();
    printf("\nEnter the xmin:->");
    scanf("%d",&xmin);
    printf("\nEnter the ymin:->");
    scanf("%d",&ymin);
    printf("\nEnter the xmax:->");
    scanf("%d",&xmax);
    printf("\nEnter the ymax:->");
    scanf("%d",&ymax);

    initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
    setcolor(15);
    rectangle(xmin,ymin,xmax,ymax);
    printf("Enter the x1:->");
    scanf("%d",&x1);
    printf("Enter the y1:->");
    scanf("%d",&y1);
    printf("Enter the x2:->");
    scanf("%d",&x2);
    printf("Enter the y2:->");
    scanf("%d",&y2);
    setcolor(12);
    line(x1,y1,xmin,ymin);
    line(x1,y1,xmax,ymin);
    line(x1,y1,xmax,ymax);
    line(x1,y1,xmin,ymax);
    getch();
    setcolor(3);
    line(x1,y1,x2,y2);
    getch();
    ch=first_end_point_region(x1,y1);
    switch(ch)
    {
case 1:
   clipline1(x1,y1,x2,y2);
   break;
case 2:
   clipline2(x1,y1,x2,y2);
   break;
case 3:
   clipline3(x1,y1,x2,y2);
   break;
default:
   printf("\nInvalid select of the option: ");
    }
    getch();
    getch();
    getch();
}

int first_end_point_region(int x,int y)
{
    /*    u have two equations:- xmin <= x <= xmax; ymin <= y <= ymax; */
    if(x>=xmin && x<=xmax && y>=ymin && y<=ymax)
return 1;
    else if(x<xmin && y>=ymin && y<=ymax)
return 2;
    else if(x<=xmin && y<=ymin)
return 3;
    else return 0;
}

/* point p1 is inside the clip window */
void clipline1(int x1,int y1,int x2,int y2)
{
    int draw=1;
    float m,m1,m2,m3,m4;
    int nx1,ny1,nx2,ny2;

    /* calculate slopes for all the lines passing thru vertices and including the input line :- */

    m=((float)(y2-y1))/(x2-x1);
    m1=((float)(ymin-y1))/(xmin-x1);
    m2=((float)(ymin-y1))/(xmax-x1);
    m3=((float)(ymax-y1))/(xmax-x1);
    m4=((float)(ymax-y1))/(xmin-x1);
    nx1=x1;
    ny1=y1;

    // point p2 is on top
    if(((abs(m)>=m1 && x2<x1) || (abs(m)>abs(m2) && x2>x1)) && y1>y2)
    {
 // point p2 is also inside clip window
 if(y2>ymin)
 {
     nx2=x2;
     ny2=y2;
 }
 // point p2 is outside clip window
 else
 {
     ny2=ymin;
     nx2=x1+(ymin-y1)/m;
 }
    }
    // point p2 is on right side of clip window
    else if(m>m2 && m<m3 && x2>=xmax)
    {
// point p2 is inside clip window
if(x2<xmax)
{
   nx2=x2;
   ny2=y2;
}
// point p2 is outside clip window
else
{
   nx2=xmax;
   ny2=y1+(xmax-x1)*m;
}
    }
    // point p2 is on bottom side of clip window
    else if((abs(m)>=m3 && x2>x1) || (abs(m)>abs(m4) && x2<x1))
    {
// point p2 is inside clip window
if(y2<ymax)
 {
   nx2=x2;
   ny2=y2;
 }
// point p2 is outside clip window
else
 {
   ny2=ymax;
   nx2=x1+(ymax-y1)/m;
 }
    }
    // point p2 is on left side of clip window
    else if(m>m4 && m<m1)
    {
// point p2 is inside the clip window
if(x2>xmin)
{
   nx2=x2;
   ny2=y2;
}
// point p2 is outside the clip window
else
{
   nx2=xmin;
   ny2=y1+(xmin-x1)*m;
}
    }
    else
draw=0;
    getch();
    cleardevice();
    setcolor(18);
    rectangle(xmin,ymin,xmax,ymax);
    if(draw)
    {
setcolor(12);
line(x1,y1,xmin,ymin);
line(x1,y1,xmax,ymin);
line(x1,y1,xmax,ymax);
line(x1,y1,xmin,ymax);
setcolor(5);
line(nx1,ny1,nx2,ny2);
    }
}

/* Point p1 is in the edge region */
void clipline2(int x1,int y1,int x2,int y2)
{
    int draw=1;
    float m,m1,m2,m3,m4;
    int nx1,ny1,nx2,ny2;

    m=((float)(y2-y1))/(x2-x1);
    m1=((float)(ymin-y1))/(xmin-x1);
    m2=((float)(ymin-y1))/(xmax-x1);
    m3=((float)(ymax-y1))/(xmax-x1);
    m4=((float)(ymax-y1))/(xmin-x1);

    // Point p2 is in Left-Top region
    if(m>m1 && m<m2)
    {
// Point p2 is inside the clip window
if(y2>ymin)
{
   nx1=xmin;
   ny1=y1+m*(xmin-x1);
   nx2=x2;
   ny2=y2;
}
// Point p2 is outside the clip window
else
{
   nx1=xmin;
   ny1=y1+m*(xmin-x1);
   ny2=ymin;
   nx2=x1+(ymin-y1)/m;
}
    }
    // Point p2 is in Left-Right region
    else if(m>m2 && m<m3)
    {
// Point p2 is inside the clip window
if(x2<xmax)
{
   nx1=xmin;
   ny1=y1+m*(xmin-x1);
   nx2=x2;
   ny2=y2;
}
// Point p2 is outside the clip window
else
{
            nx1=xmin;
            ny1=y1+m*(xmin-x1);
            nx2=xmax;
            ny2=y1+(xmax-x1)*m;
        }
    }
    // Point p2 is in Left-Bottom region
    else if(m>m3 && m<m4)
    {
// Point p2 is inside the clip window
if(y2<ymax)
{
   nx1=xmin;
   ny1=y1+m*(xmin-x1);
   nx2=x2;
   ny2=y2;
}
// Point p2 is outside the clip window
else
{
   nx1=xmin;
   ny1=y1+m*(xmin-x1);
   ny2=ymax;
   nx2=x1+(ymax-y1)/m;
}
    }
    else
draw=0;
    getch();
    cleardevice();
    setcolor(18);
    rectangle(xmin,ymin,xmax,ymax);
    if(draw)
    {
setcolor(12);
line(x1,y1,xmin,ymin);
line(x1,y1,xmax,ymin);
line(x1,y1,xmax,ymax);
line(x1,y1,xmin,ymax);
setcolor(5);
line(nx1,ny1,nx2,ny2);
    }
}

/* Point p1 is in the Corner Region */
void clipline3(int x1,int y1,int x2,int y2)
{
    int draw=1;
    float m,m1,m2,m3,m4,tm1,tm2;
    int nx1,ny1,nx2,ny2;
    int flag,t;
    tm1=((float)(ymin-y1))/(xmin-x1);
    tm2=((float)(ymax-ymin))/(xmax-xmin); //diagonal slope

    m=((float)(y2-y1))/(x2-x1);
    m1=((float)(ymin-y1))/(xmax-x1);
    m2=((float)(ymax-y1))/(xmax-x1);
    m3=((float)(ymin-y1))/(xmin-x1);
    m4=((float)(ymax-y1))/(xmin-x1);

    // Point p1 is towards the left side of the clip window (case2)
    if(tm1<tm2)
    {
flag=2;
t=m2;
m2=m3;
m3=t;
    }
    // Point p1 is towards the top side of the clip window (case1)
    else
flag=1;

    // Point p2 is in the Top-Right region
    if(m>m1 && m<m2)
    {
// Point p2 is outside the clip window
if(x2>xmax && y2>ymin)
{
   ny1=ymin;
   nx1=x1+(ymin-y1)/m;
   nx2=xmax;
   ny2=y1+m*(xmax-x1);
}
// Point p2 is inside the clip window
else if(y2>ymin && x2<xmax)
{
   ny1=ymin;
   nx1=x1+(ymin-y1)/m;
   ny2=y2;
   nx2=x2;
}
    }
    // Point p2 is Left-Right or Top-Bottom region
     else if(m>m2 && m<m3)
    {
// Point p2 is in Top-Bottom region (case1)
if(flag==1)
{
   // Point p2 is outside the clip window
   if(y2>=ymax)
      {
  ny1=ymin;
  nx1=x1+(ymin-y1)/m;
  nx2=x1+(ymax-y1)/m;
  ny2=ymax;
      }
      // Point p2 is inside the clip window
      else if(y2>=ymin)
      {
  ny1=ymin;
  nx1=x1+(ymin-y1)/m;
  nx2=x2;
  ny2=y2;
      }
}
// Point p2 is in Left-Right region (case2)
else
{
   // Point p2 is outside the clip window
   if(x2>=xmax)
      {
  nx1=xmin;
  ny1=y1+m*(xmin-x1);
  nx2=xmax;
  ny2=y1+m*(xmax-x1);
      }
      // Point p2 is inside the clip window
      else if(x2>=xmin)
      {
  nx1=xmin;
  ny1=y1+m*(xmin-x1);
  nx2=x2;
  ny2=y2;
      }
}
    }
    // Point p2 is in Left-Bottom region
    else if(m>m3 && m<m4)
    {
// Point p2 is outside the clip window
if(y2>=ymax)
{
   nx1=xmin;
   ny1=y1+m*(xmin-x1);
   nx2=x1+(ymax-y1)/m;
   ny2=ymax;
}
// Point p2 is inside the clip window
else if(y2>=ymin)
{
   nx1=xmin;
   ny1=y1+m*(xmin-x1);
   ny2=y2;
   nx2=x2;
}
    }
    else
draw=0;
    getch();
    cleardevice();
    setcolor(18);
    rectangle(xmin,ymin,xmax,ymax);
    if(draw)
    {
setcolor(12);
line(x1,y1,xmin,ymin);
line(x1,y1,xmax,ymin);
line(x1,y1,xmax,ymax);
line(x1,y1,xmin,ymax);
setcolor(5);
line(nx1,ny1,nx2,ny2);
    }
}

cyrus.c computer graphics


#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void clip(double*,double*,double*,double*,int *);
int clipt(double denom,double num,double *te,double *tl);

double xmin,ymin,xmax,ymax;
int main()
{
double x0,y0,x1,y1;
int vis;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter coordinates of window:\n");
scanf("%lf%lf%lf%lf",&xmin,&ymin,&xmax,&ymax);
printf("Enter coordinates of line:\n");
scanf("%lf%lf%lf%lf",&x0,&y0,&x1,&y1);
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x0,y0,x1,y1);
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
clip(&x0,&y0,&x1,&y1,&vis);
if(vis)
{
line(x0,y0,x1,y1);
}
getch();
getch();
closegraph();

return 0;
}

void clip(double *x0,double *y0,double *x1,double *y1,int *vis)
{
double dx=*x1-*x0;
double dy=*y1-*y0;
double te=0.0,tl=1.0;
*vis=0;
if(clipt(dx,xmin-*x0,&te,&tl))
if(clipt(-dx,*x0-xmax,&te,&tl))
if(clipt(dy,ymin-*y0,&te,&tl))
if(clipt(-dy,*y0-ymax,&te,&tl))
{
*vis=1;
if(tl<1)
{
*x1=*x0+tl*dx;
*y1=*y0+tl*dy;
}

if(te>0)
{
*x0=*x0+te*dx;
*y0=*y0+te*dy;
}

}
 return ;
}

int clipt(double denom,double num,double *te,double *tl)
{
double t;
if(denom>0)//PE
{
t=num/denom;
if(t>*tl)
return 0;
else if(t>*te)
*te=t;
}
else if(denom<0)//PL
{
t=num/denom;
if(t<*te)
return 0;
else
*tl=t;
}
else if(num>0)
return 0;
return 1;
}

liba.c computer graphics


#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void clip(double*,double*,double*,double*,int *);
int clipt(double denom,double num,double *te,double *tl);

double xmin,ymin,xmax,ymax;
int main()
{
double x0,y0,x1,y1;
int vis;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter coordinates of window:\n");
scanf("%lf%lf%lf%lf",&xmin,&ymin,&xmax,&ymax);
printf("Enter coordinates of line:\n");
scanf("%lf%lf%lf%lf",&x0,&y0,&x1,&y1);
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x0,y0,x1,y1);
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
clip(&x0,&y0,&x1,&y1,&vis);
if(vis)
{
line(x0,y0,x1,y1);
}
getch();
getch();
closegraph();

return 0;
}

void clip(double *x0,double *y0,double *x1,double *y1,int *vis)
{
double dx=*x1-*x0;
double dy=*y1-*y0;
double te=0.0,tl=1.0;
*vis=0;
if(clipt(dx,xmin-*x0,&te,&tl))
if(clipt(-dx,*x0-xmax,&te,&tl))
if(clipt(dy,ymin-*y0,&te,&tl))
if(clipt(-dy,*y0-ymax,&te,&tl))
{
*vis=1;
if(tl<1)
{
*x1=*x0+tl*dx;
*y1=*y0+tl*dy;
}

if(te>0)
{
*x0=*x0+te*dx;
*y0=*y0+te*dy;
}

}
 return ;
}

int clipt(double denom,double num,double *te,double *tl)
{
double t;
if(denom>0)//PE
{
t=num/denom;
if(t>*tl)
return 0;
else if(t>*te)
*te=t;
}
else if(denom<0)//PL
{
t=num/denom;
if(t<*te)
return 0;
else
*tl=t;
}
else if(num>0)
return 0;
return 1;
}

look disk scheduling


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int max,min,disk,mark[100],q[100],rear=0,size,head,seek;
void ins();
void dis();
void look();
void fmaxmin();
main()
{
int i,ch;
printf("\nEnter the size of the disk (in tracks) : ");
scanf("%d",&disk);
printf("\nEnter the current position of the disk head : ");
scanf("%d",&head);
printf("\nEnter the number of requests : ");
scanf("%d",&size);
if(size!= 0)
printf("\nEnter the requests - : \n\n");
else
{
printf("No requests to be made ");
return;
}
for(i=1;rear<=size;i++)
{
ins();
mark[i] = 0;
}
fmaxmin();
q[0] = head;
printf("\n\n\t\t\t\tDISK SCHEDULING\n\n");
head = q[0];
seek = 0;
look();
getch();
}
void ins()
{
int i;
rear++;
if(rear<=size)
{
printf("Enter the request %d to track no. : ",rear);
scanf("%d",&i);
}
if(i>disk)
{
printf("\nEnter valid track request\n\n");
rear--;
}
else
q[rear] = i;
}
void dis()
{
int i;
for(i=1;i<=size;i++)
printf("%d\t",q[i]);
}
void look()
{
int i,j;
printf("\n\n\t\t\t\tLOOK SCHEDULING\n\n");
printf("\nWORK QUEUE : \n");
dis();
printf("\n\n--------------------------------------------------------------------------------\n");
printf("\nThe sequence is. . .\n\n");
for(j=q[0];j<=max;j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
if(head>min)
{
for(j=q[0];j>=0;j--)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
}
printf("\n\nTotal number of head movements is : %d",seek);
}
void fmaxmin()
{
int i;
for(i = 1;i<=size;i++)
{
if(q[i]>max)
max = q[i];
if(q[i]<min)
min = q[i];
}
}

scan disk scheduling


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int max,min,disk,mark[100],q[100],rear=0,size,head,seek;
void ins();
void dis();
void clook();
void fmaxmin();
main()
{
int i,ch;
printf("\nEnter the size of the disk (in tracks) : ");
scanf("%d",&disk);
printf("\nEnter the current position of the disk head : ");
scanf("%d",&head);
printf("\nEnter the number of requests : ");
scanf("%d",&size);
if(size!= 0)
printf("\nEnter the requests - : \n\n");
else
{
printf("No requests to be made ");
return;
}
for(i=1;rear<=size;i++)
{
ins();
mark[i] = 0;
}
fmaxmin();
q[0] = head;
printf("\n\n\t\t\t\tDISK SCHEDULING\n\n");
head = q[0];
seek = 0;
clook();
getch();
}
void ins()
{
int i;
rear++;
if(rear<=size)
{
printf("Enter the request %d to track no. : ",rear);
scanf("%d",&i);
}
if(i>disk)
{
printf("\nEnter valid track request\n\n");
rear--;
}
else
q[rear] = i;
}
void dis()
{
int i;
for(i=1;i<=size;i++)
printf("%d\t",q[i]);
}
void clook()
{
int i,j;
printf("\n\n\t\t\t\tCLOOK SCHEDULING\n\n");
printf("\nWORK QUEUE : \n");
dis();
printf("\n\n--------------------------------------------------------------------------------\n");
printf("\nThe sequence is. . .\n\n");
for(j=q[0];j<=max;j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
if(head>min)
{
for(j=0;j<=q[0];j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
}
printf("\n\nTotal number of head movements is : %d",seek);
}
void fmaxmin()
{
int i;
for(i = 1;i<=size;i++)
{
if(q[i]>max)
max = q[i];
if(q[i]<min)
min = q[i];
}
}

sstf disk scheduling


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int max,min,disk,mark[100],q[100],rear=0,size,head,seek;
void ins();
void dis();
void sstf();
void fmaxmin();
main()
{
int i,ch;
printf("\nEnter the size of the disk (in tracks) : ");
scanf("%d",&disk);
printf("\nEnter the current position of the disk head : ");
scanf("%d",&head);
printf("\nEnter the number of requests : ");
scanf("%d",&size);
if(size!= 0)
printf("\nEnter the requests - : \n\n");
else
{
printf("No requests to be made ");
return;
}
for(i=1;rear<=size;i++)
{
ins();
mark[i] = 0;
}
fmaxmin();
q[0] = head;
printf("\n\n\t\t\t\tDISK SCHEDULING\n\n");
head = q[0];
seek = 0;
sstf();
getch();
}
void ins()
{
int i;
rear++;
if(rear<=size)
{
printf("Enter the request %d to track no. : ",rear);
scanf("%d",&i);
}
if(i>disk)
{
printf("\nEnter valid track request\n\n");
rear--;
}
else
q[rear] = i;
}
void dis()
{
int i;
for(i=1;i<=size;i++)
printf("%d\t",q[i]);
}
void sstf()
{
int low = disk,j,flag;
for(j=1;j<=size;j++)
{
if(abs(head-q[j]) < low && mark[j] == 0)
{
low = abs(head-q[j]);
flag = j;
}
}
mark[flag] = 1;
printf("\nHead moves from %d to %d...",head,q[flag]);
head = q[flag];
seek += low;
}
void fmaxmin()
{
int i;
for(i = 1;i<=size;i++)
{
if(q[i]>max)
max = q[i];
if(q[i]<min)
min = q[i];
}
}

dinning problem


#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
 
#define N 5 /*Number of philosphers*/
#define THINKING 0 /*Philospher is thinking*/
#define HUNGRY 1 /*Philospher is trying to get forks*/
#define EATING 2 /*Philospher is eating*/
#define LEFT (ph_num+4)%N /*Number of left neighbor*/
#define RIGHT (ph_num+1)%N /*Number of right neighbor*/
 
sem_t mutex;
sem_t S[N]; /*Array to keep track of everyone's state*/
 
void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);
 
int state[N];
int phil_num[N]={0,1,2,3,4};
 
int main()
{
    int i;
    pthread_t thread_id[N];
    sem_init(&mutex,0,1);
    for(i=0;i<N;i++)
        sem_init(&S[i],0,0);
    for(i=0;i<N;i++)
    {
        pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
        printf("Philosopher %d is thinking\n",i+1);
    }
    for(i=0;i<N;i++)
        pthread_join(thread_id[i],NULL);
}
 
void *philospher(void *num) /* num: philospher's number from 0 to N-1*/
{
    while(1) /*repeat forever*/
    {
        int *i = num;
        sleep(1); /* philospher is thinking*/
        take_fork(*i); /*aquire forks or block*/
        sleep(0); /*Philospher is eating*/
        put_fork(*i); /*put both forks back on table*/
    }
}
 
void take_fork(int ph_num) /* ph_num: philospher's number from 0 to N-1*/
{
    sem_wait(&mutex); /*enter critical section*/
    state[ph_num] = HUNGRY; /* State is philospher is hungry*/
    printf("Philosopher %d is Hungry\n",ph_num+1);
    test(ph_num); /*try to aquire two forks*/
    sem_post(&mutex); /* exit crtical section*/
    sem_wait(&S[ph_num]); /*block if forks were not aquired*/
    sleep(1);
}
 
void test(int ph_num) /* ph_num: philospher's number from 0 to N-1*/
{
    if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
    {
        state[ph_num] = EATING;
        sleep(2);
        printf("Philosopher %d takes fork %d and %d\n",ph_num+1,LEFT+1,ph_num+1);
        printf("Philosopher %d is Eating\n",ph_num+1);
        sem_post(&S[ph_num]);
    }
}
 
void put_fork(int ph_num) /* ph_num: philospher's number from 0 to N-1*/
{
    sem_wait(&mutex); /*enter critical section*/
    state[ph_num] = THINKING; /*philospher has finished eating*/
    printf("Philosopher %d putting fork %d and %d down\n",ph_num+1,LEFT+1,ph_num+1);
    printf("Philosopher %d is thinking\n",ph_num+1);
    test(LEFT); /*see if left neighbor can now eat*/
    test(RIGHT); /*see if right neighbor can now eat*/
    sem_post(&mutex); /* exit crtical section*/
}

c look disk scheduling


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int max,min,disk,mark[100],q[100],rear=0,size,head,seek;
void ins();
void dis();
void clook();
void fmaxmin();
main()
{
int i,ch;
printf("\nEnter the size of the disk (in tracks) : ");
scanf("%d",&disk);
printf("\nEnter the current position of the disk head : ");
scanf("%d",&head);
printf("\nEnter the number of requests : ");
scanf("%d",&size);
if(size!= 0)
printf("\nEnter the requests - : \n\n");
else
{
printf("No requests to be made ");
return;
}
for(i=1;rear<=size;i++)
{
ins();
mark[i] = 0;
}
fmaxmin();
q[0] = head;
printf("\n\n\t\t\t\tDISK SCHEDULING\n\n");
head = q[0];
seek = 0;
clook();
getch();
}
void ins()
{
int i;
rear++;
if(rear<=size)
{
printf("Enter the request %d to track no. : ",rear);
scanf("%d",&i);
}
if(i>disk)
{
printf("\nEnter valid track request\n\n");
rear--;
}
else
q[rear] = i;
}
void dis()
{
int i;
for(i=1;i<=size;i++)
printf("%d\t",q[i]);
}
void clook()
{
int i,j;
printf("\n\n\t\t\t\tCLOOK SCHEDULING\n\n");
printf("\nWORK QUEUE : \n");
dis();
printf("\n\n--------------------------------------------------------------------------------\n");
printf("\nThe sequence is. . .\n\n");
for(j=q[0];j<=max;j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
if(head>min)
{
for(j=0;j<=q[0];j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
}
printf("\n\nTotal number of head movements is : %d",seek);
}
void fmaxmin()
{
int i;
for(i = 1;i<=size;i++)
{
if(q[i]>max)
max = q[i];
if(q[i]<min)
min = q[i];
}
}

non simple mutex


#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>

pthread_t tid[2];
int counter;

void* doSomeThing(void *arg)
{
    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);

    for(i=0; i<(0xFFFFFFFF);i++); //this is just wait
    printf("\n Job %d finished\n", counter);

    return NULL;
}

int main(void)
{
    int i = 0;
    int err;

    while(i < 2)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

    return 0;
}

mutex simple c code


#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>

pthread_t tid[2];
int counter;
pthread_mutex_t lock;

void* doSomeThing(void *arg)
{
    pthread_mutex_lock(&lock);

    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);

    for(i=0; i<(0xFFFFFFFF);i++); //this is just wait

    printf("\n Job %d finished\n", counter);

    pthread_mutex_unlock(&lock);

    return NULL;
}

int main(void)
{
    int i = 0;
    int err;

    if (pthread_mutex_init(&lock, NULL) != 0)
    {
        printf("\n mutex init failed\n");
        return 1;
    }

    while(i < 2)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    pthread_mutex_destroy(&lock);

    return 0;
}

preempt priority scheduling


#include<stdio.h>

struct process
{
int arr_time;
int burst_time;
int no;
int rem_time;
int priority;
};


struct process read(int i)
{
struct process p;
printf("\n\n The process no.:%d.\n",i);
p.no=i;
printf("Enter the arrival time:");
scanf("%d",&p.arr_time);
printf("Enter the burst time:");
scanf("%d",&p.burst_time);
p.rem_time=p.burst_time;
return p;
}



struct process readp(int i)
{
struct process p;
printf("\n\n The process no.:%d.\n",i);
p.no=i;
printf("Enter the arrival time:");
scanf("%d",&p.arr_time);
printf("Enter the burst time:");
scanf("%d",&p.burst_time);
p.rem_time=p.burst_time;
printf("Enter the priority:");
scanf("%d",&p.priority);
return p;
}



void swap(struct process *i, struct process *j)
{
struct process *t;
i=t;
i=j;
j=t;
}


//PRIORITY BASED PREEEMPTIVE  SCHEDULING ALGO.
int main()
{
int  n; //To hold the no. of processes.
struct process p[10],tmp; //To hold the details of the processes.
int i,j;
int ready[10]; //List of ready processes  index
int running; //Running process index
int t; //Time variable
int last,min;
int time;
printf("LOWER NUMBER INDIACTES HIGHER PRIORITY\n");
printf("Enter the number if processes you want to enter:"); //Get the total number of processes from the user

scanf("%d",&n);


for(i=0;i<n;i++)
p[i]=readp(i); //Read the details of the processes


t=0;
last=-1;
min=0;
time=0;

do
{
last=-1;
for(i=0;i<n;i++)
{
if(p[i].arr_time<=time && p[i].rem_time>0)
{
ready[++last]=i; //update the ready queue

}

}

min = ready[0];
if(last<0)
continue;
for(i=0;i<=last;i++)
{
if(p[ready[i]].priority<p[min].priority) //Schedule the process with lowest priority for execution
min=ready[i];
}
running = min;
printf("\n\nTime:%-2d to time: %-2d Running Process: %d",time,time+1,running);
//Print the time for which the particuler process was running
time = time + 1;
p[running].rem_time--; //Update the remaining time




}while(last>=0);
printf("\n");
return 0;
}