Absolute Loader

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
         FILE *fp;
         int i,addr1,l,j,staddr1;
         char name[10],line[50],name1[10],addr[10],rec[10],ch,staddr[10];

            printf("enter program name:" );
           scanf("%s",name);
            fp=fopen("objectcode.txt","r");
             fscanf(fp,"%s",line);
            for(i=2,j=0;i<8,j<6;i++,j++)
    name1[j]=line[i];
    name1[j]='\0';
    printf("name from obj. %s\n",name1);
   if(strcmp(name,name1)==0)
   {
         fscanf(fp,"%s",line);
        do
       {

                  if(line[0]=='T')
                {
                         for(i=2,j=0;i<8,j<6;i++,j++)
                      staddr[j]=line[i];
                     staddr[j]='\0';
                    staddr1=atoi(staddr);
                     i=12;
                 while(line[i]!='$')
              {
                     if(line[i]!='^')
                    {
                           printf("00%d \t %c%c\n", staddr1,line[i],line[i+1]);
                           staddr1++;
                            i=i+2;
                 }
              else i++;
              }
     }
    else if(line[0]='E')
              printf("jump to execution address:%s",&line[2]);
         fscanf(fp,"%s",line);
    }while(!feof(fp) );

   }
    fclose(fp);
}

objectcode.txt

H^SAMPLE^001000^0035
T^001000^0C^001003^071009$
T^002000^03^111111$
E^001000

Output
enter program name:SAMPLE
name from obj. SAMPLE
001000   00
001001   10
001002   03
001003   07
001004   10
001005   09
002000   11
002001   11
002002   11
jump to execution address:001000

One Pass Assembler [ SIC Load and Go Assembler Simulation]


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct node
{
int addr;
struct node *link;
};
struct symbol
{
char label[10];
int addr;
struct node *ptr;
}symtab[100];
void inserttosymtab(char *,int,int);
void resolve(int,int);
void modifyobjectcode(int,int);
int checkresolved(void) ;
int searchsymbol(char *,int*);
int searchopcode(char *);
int current=0,start;
char objectcode[500]={'\0'} ;
void main()
{
FILE *fin;
int i,locctr,found,det,x,nloc,len,addr,loadaddr;
char s[10],operand[10],opcode[10],label[10];
fin=fopen("input.txt","r");
fscanf(fin,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{

locctr=atoi(operand);
start=locctr;
}
else
locctr=0;

fscanf(fin,"%s%s%s",label,opcode,operand);

while(strcmp(opcode,"END")!=0)
{
if(strcmp(label,"-")!=0)
{
      found=searchsymbol(label,&addr);
      if(found==1)
      {
printf("Duplicate label found\n");
exit(0);
      }
      else if(found==0)
      {
inserttosymtab(label,locctr,0);
       }
      else
resolve(locctr,addr);

}
det=searchopcode(opcode );
if(det==1)
{
strcat(objectcode,opcode);
locctr=locctr+1;
found=searchsymbol(operand,&addr);
if(found==0|| found==2)
{
strcat(objectcode,"0000");
if(strcmp(operand,"-")!=0)
inserttosymtab(operand,locctr,1);
locctr=locctr+2;
}
else if(found==1)
{

sprintf(operand,"%d",addr);
strcat(objectcode,operand);
locctr=locctr+2;
}
  }
else if((strcmp(opcode,"BYTE")==0)||(strcmp(opcode,"WORD")==0))
{
if(strcmp(opcode,"WORD")==0)
{
    len=strlen(operand);
    for(i=0;i<(6-len);i++)
    strcat(objectcode,"0");
    strcat(objectcode,operand);
    locctr=locctr+3;

}
else
{
len=strlen(operand);
for(i=2;i<len-1;i++)
{
x=toascii(operand[i]);
sprintf(s,"%d",x);
strcat(objectcode,s);
}
locctr=locctr+len-3;

}
}
else if(strcmp(opcode,"RESW")==0)
{
nloc=3*atoi(operand);
locctr=locctr+3*atoi(operand);
for(i=0;i<nloc;i++)
strcat(objectcode,"X");
}
else if(strcmp(opcode,"RESB")==0)
{
nloc=atoi(operand);
locctr=locctr+atoi(operand);
for(i=0;i<nloc;i++)
strcat(objectcode,"XX");
}


    fscanf(fin,"%s%s%s",label,opcode,operand);

}
fclose(fin);
if(checkresolved())
{
i=0;
loadaddr=start;
printf("\n......Load and Go One Pass assembler loaded memory map....\n");

while(objectcode[i]!='\0')
{
if(i%32==0)
{
printf("\n%d",loadaddr);
loadaddr=loadaddr+16;
}
if(i%8==0 )
printf("  ");
printf("%c",objectcode[i]);
i++;
}
}

}

void inserttosymtab(char *label,int locctr,int flag)
{
struct node *temp;
int det,pos;

if(flag==0)
{
strcpy(symtab[current].label,label);
symtab[current].addr=locctr;
symtab[current].ptr=NULL;
current++;
}
else
{
det=searchsymbol(label,&pos);
if(det==0)
{
pos=current;
current++;
}
strcpy(symtab[pos].label,label);
temp=(struct node *)malloc(sizeof(struct node));
temp->addr=locctr;
temp->link=symtab[pos].ptr;
symtab[pos].ptr=temp;
}

}
int searchsymbol(char *label,int *addr)
{
int i=0;
for(i=0;i<current;i++)
{
if(strcmp(symtab[i].label ,label)==0)
{
if(symtab[i].ptr==NULL)
{
*addr=symtab[i].addr;
return 1;
}
else
{ *addr=i;
return 2;
}
}
}
return 0;
}
void resolve(int locctr,int loc)
{
struct node *temp;
temp=symtab[loc].ptr;
while(temp!=NULL)
{
modifyobjectcode(temp->addr,locctr);
temp=temp->link;
}
symtab[loc].ptr=NULL;
symtab[loc].addr=locctr;
}

void modifyobjectcode(int addr,int locctr)
{

char operand[10];
int i=0;
sprintf(operand,"%d",locctr);
addr=(addr-start)*2;
while(operand[i]!='\0')
{
objectcode[addr]=operand[i];
addr++;
i++;
}
}

int searchopcode(char *opcode)
{
char opmne[10],op[10];
int flag=0;
FILE *optab;
optab=fopen("optab.txt","r");

fscanf(optab,"%s%s",opmne,op);

while(!feof(optab))
{
if(strcmp(opcode,opmne)==0)
{
flag=1;
strcpy(opcode,op);
break;


}
else
fscanf(optab,"%s%s",opmne,op);
}
fclose(optab);
return flag;
}

int checkresolved(void)
{
int i=0,flag=1;
for(i=0;i<current;i++)
{
if(symtab[i].ptr!=NULL)
{
printf("%s is not resoved,",symtab[i].label);
flag=0;
}
}
return(flag);
}
INPUT FILES
input.txt
optab.txt
Output

C Program for Number System Conversion




#include<stdio.h>
#include<string.h>
#include<ctype.h>

#include<math.h>
#include<stdlib.h>
char *strrev(char *);
char *convert(int , int );
int con(char *,int);
char lookuptable[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char result[100];

int  main()
{

 char str[100];
 char *p=str;
 int choice;

 while(1)
 {     

  printf("\n 1 decimal----binary\t 2 decimal----octa\n");
  printf("\n 3 decimal----hexa  \t 4 octa----binary\n");
  printf("\n 5 octa----decimal  \t 6 octa----hexa\n");
  printf("\n 7 hexa----binary   \t 8 hexa----decimal\n");
  printf("\n 9 hexa----octa     \t 10 BINARY----Decimal\n");
  printf("\n 11 binary----octa  \t 12 binary----hexa\n");
  printf("\b 13.exit\n");
  printf("\n enter your choice\n");

  scanf("%d",&choice);

  if(choice<=3)
   printf("\n enter a decimal number\n");
  if(choice<=6 && choice >3)
   printf("\n enter a octal number\n");
  if(choice <=9 && choice >6)
   printf("\n enter a hexa\n");
  if(choice<=12 && choice>9)
   printf("\n enter a binary no\n");
  if(choice==13)
   printf("exiting....");  
  else 
   scanf("%s",str);
   switch(choice)
   {
    case 1:
     printf(" BINARY: %s" ,convert(con(p,10),2));
     break;
    case 2:
     printf(" octal: %s" ,convert(con(p,10),8));
     break;
    case 3:
     printf(" octal: %s" ,convert(con(p,10),16));
     break;
    case 4:
     printf(" BINRY: %s" ,convert(con(p,8),2));
     break;
     case 5:
     printf(" BINARY: %s" ,convert(con(p,8),10));
     break;
     case 6:
     printf(" BINARY: %s" ,convert(con(p,8),16));
     break;
     case 7:
     printf(" BINARY: %s" ,convert(con(p,16),2));
     break;
     case 8:
     printf(" BINARY: %s" ,convert(con(p,16),10));
     break;
     case 9:
     printf(" BINARY: %s" ,convert(con(p,16),8));
     break;
    case 10:
     printf(" DECIMAAL: %s" ,convert(con(p,2),10));
     break;
    case 11:
     printf(" OCTAL: %s" ,convert(con(p,2),8));
     break;
    case 12:
     printf(" hexa: %s" ,convert(con(p,2),16));
     break;
    case 13:
     exit(0);
    default:
    printf("your choice is not currect\n");


   }
  
 

 }
 
 }


   char *convert(int num, int base)
   {
 char *r;
 int i=0;
 do
 {
  result[i++]=lookuptable[(num%base)];
  num=num/base;
 }
 while(num>=base);

 result[i]=lookuptable[num];
 result[++i]='\0';
r=strrev(result);

     return(r);

   }

int    con(char *p,int base)
    {
 char *temp=p;
 int i=0 ,j=0,dec=0;
 strrev(p);
 while(*temp!='\0')
 {
  if(islower(*temp))
  *temp=toupper(*temp);
  temp++;
 }

 while(*p!='\0')
 {
  while(lookuptable[i]!=(*p))
  i++;
  dec=dec+i* pow(base,j);
  j++;
  i=0;
  p++;
 }

 return(dec);
   }

char *strrev(char *str)
{
    int i = strlen(str) - 1, j = 0;

    char ch;
    while (i > j)
    {
        ch = str[i];
        str[i] = str[j];
        str[j] = ch;
        i--;
        j++;
    }
    return str;
}

Output

Grammar- First function in Compiler

              C program for First Function in Compiler

Check the sample output..Non terminal must be capital letter, epsilon is represented by # symbol
#include<stdio.h>
#include<ctype.h>

void firstof(char[], char);
void arrangearray(char[], char);
int visited(char );

int noproduction;
char production[100][100];

char visit[100];
int nTerm;
int main()
{
      char option;
      char ch;
      char array[100];
      int c,k;
      printf("\nEnter Total Number of Productions:");
      scanf("%d", &noproduction);
      for(c = 0; c < noproduction; c++)
      {
     printf("\nEnter Production%d:", c + 1);
     scanf("%s", production[c]);
      }
      k=0;
      while(k<noproduction)
      {

     ch=production[k][0];
     if(!visited(ch))
     {
  firstof(array, ch);
  printf("\nFirst(%c):\t{ ", ch);
  for(c = 0; array[c] != '\0'; c++)
  {
     printf("%c,", array[c]);
  }
  printf("}\n");
     }
     k++;

      }
      return 0;
}

void firstof(char* array, char ch)
{
      int c, j, k;
      char tmpresult[100];
      int x;
      tmpresult[0] = '\0';
      array[0] = '\0';
      if(!(isupper(ch)))
      {
     arrangearray(array, ch);
     return ;
      }
      c=0;
      while( c < noproduction)
      {
     if(production[c][0] == ch)
     {
    if(production[c][2] == '#')
    {
   arrangearray(array, '#');
    }
    else
    {
   j = 2;
   while(production[c][j] != '\0')
   {
         x = 0;
         firstof(tmpresult, production[c][j]);
         for(k = 0; tmpresult[k] != '\0'; k++)
         {
        arrangearray(array,tmpresult[k]);
         }
         for(k = 0; tmpresult[k] != '\0'; k++)
         {
        if(tmpresult[k] == '#')
        {
       x = 1;
       break;
        }
         }
         if(x==0)
         {
        break;
         }
         j++;
   }
    }
     }
 c++;
      }
      return;
}

void arrangearray(char array[], char value)
{
      int tmp;
      for(tmp = 0; array[tmp] != '\0'; tmp++)
      {
     if(array[tmp] == value)
     {
    return;
     }
      }
      array[tmp] = value;
      array[tmp + 1] = '\0';
}

int visited(char ch)
{
 int i=0;
 for(i=0;i<nTerm;i++)
 {
  if(visit[i]==ch)
  {
   return 1;
  }
 }
 visit[i]=ch;
 nTerm++;
 return 0;
}

Output

C program for BFS traversal

#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
int FRONT=-1,REAR=-1;
int A[SIZE];
void enq(int);
int deq();
main()
{
 int VISIT[20],adjmat[20][20];
 int n,i,j,v=0;
 printf("Enter the no of vertices\n");
 scanf("%d",&n);
 printf("Enter adjacency matrix\n");
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
   scanf("%d",&adjmat[i][j]);
 }
 if (n<=0)
  printf("The graph is empty\n");
 else
 {
  printf("The BFS traversal is\n");
  for(i=0;i<n;i++)
  {
   VISIT[i]=0;
  }
  enq(v);
  while(FRONT!=-1 && REAR!=-1)
  {
   v=deq();
   if(VISIT[v]==0)
   {
    printf("\tv%d",v);
    VISIT[v]=1;
    for(i=0;i<n;i++)
    {
     if((adjmat[v][i]==1)&&(VISIT[i]==0))
      enq(i);
    }
   }
  }
 }
}
void enq(int n)
{
 if(REAR>=99)
  printf("Queue is full\n");
 else
 {
  if((REAR==-1)&&(FRONT==-1))
  {
   FRONT=0;REAR=0;
   A[REAR]=n;
  }
  else
  {
   REAR=REAR+1;
   A[REAR]=n;
  }
 }
}

int deq()
{
 int i;
 if((FRONT==-1)&&(REAR==-1))
 {
  printf("QUEUE IS EMPTY \n");
  return(-999);
 }
 else
 {
  i=A[FRONT];
  if(FRONT==REAR)
  {
   FRONT=-1;REAR=-1;
  }
  else
  {
   FRONT=FRONT+1;
  }
  return(i);
 }
}

Output



Quick sort

Quick sort

#include<stdio.h>
void quicksort(int,int);
int partition(int,int);
int a[100],n;
main()
{
 int i;
 printf("Enter limit \n");
 scanf("%d",&n);
 printf("Enter %d elements \n",n);
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 printf("Before sorting \n");
 for (i=0;i<n;i++)
  printf("%d\t",a[i]);
 quicksort(0,n-1);
 printf("Afer sorting\n");
 for(i=0;i<n;i++)
  printf("%d\t",a[i]);
}
void quicksort(int lb,int ub)
{
 int newub;
 if (lb<ub)
 {
  newub=partition(lb,ub);
  quicksort(lb,newub-1);
  quicksort(newub+1,ub);
 }
}
int partition(int lb,int ub)
{
 int temp,down=lb,up=ub,pivot=a[lb];
 while (down<up)
 {
  while (pivot>=a[down] && down<=up)
   down=down+1;
  while (pivot<a[up])
   up=up-1;
  if (down<up)
  {
   temp=a[down];
   a[down]=a[up];
   a[up]=temp;
  }
 }
temp=a[lb];
a[lb]=a[up];
a[up]=temp;
return up;
}
 
Output


Dijkstra's Algorithm

Dijkstra's Algorithm

#include<stdio.h>
void dijkstra(int);
int search_min(int [100],int [100]);
int adjmat[100][100],n;
int  main()
{
int s,i,j;
printf("Enter number of vertices?\n");
scanf("%d",&n);
printf("enter adjacensy  matrix?\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&adjmat[i][j]);
}
}
printf("Enter source vertex?\n");
scanf("%d",&s);
dijkstra(s);

}
void dijkstra(int s)
{
int set[100],length[100],path[100];
int i=0,j,complete;
while(i<n)
{
set[i]=0;
i++;
}
i=0;
while(i<n)
{
if(adjmat[s][i]==0)
{
length[i]=999;
path[i]=999;
}
else
{
length[i]=adjmat[s][i];
path[i]=s;
}
i++;
}
set[s]=1;
length[s]=0;
complete=0;
while(complete==0)
{
i=search_min(length,set);
set[i]=1;
j=0;
while(j<n)
{
if(set[j]==1)
j++;
else
{
if(adjmat[i][j]!=0)
{
if((length[i]+adjmat[i][j])<length[j])
{
length[j]=length[i]+adjmat[i][j];
path[j]=i;
}
}
j++;
}
}
complete=1;
i=0;
while(i<n)
{
if(set[i]==0)
{
complete=0;
break;
}
else
{
i++;
}
}
}
printf("source->dest\tpath\tlength\n");
for(i=1;i<n;i++)
{
printf("v%d->v%d\t\t",s,i);
j=i;
while(j!=s)
{
printf("v%d-",j);
j=path[j];
}
       printf("v%d\t\t%d\n",s,length[i]);

}
}

int search_min(int length[100],int set[100])
{
int min=999;
int i=-1,ind=-1;
for(i=0;i<n;i++)
{
if(set[i]==0)
{
if(min>length[i])
{
min=length[i];
ind=i;
}
}
}
return ind;
}


Output


C program for infix expression to postfix expression and postfix expression evaluation


C program for infix expression to postfix expression and postfix expression evaluation
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void push1(char);
char pop1(void);
int operand(char);
int priority(char);
void push2(float);
float pop2(void);
float findval(char);
char expr[100];
char variable[100];
float value[100];
char stack1[100],c;
int top1=-1;
int top2=-1;
char postfix[100];
int nv;
float stack2[100];
float val,val1,val2;
main()
{
 int i,j,l,p1,p2,flag;
 printf("Enter the no. of variables\n");
 scanf("%d",&nv);
 printf("Enter the variable names\n");
 getchar();
 for(i=0;i<nv;i++)
 {
  scanf("%c",&variable[i]);
  getchar();
 }
 printf("enter the values of variables\n");
 for(i=0;i<nv;i++)
 {
  printf("%c=?",variable[i]);
  scanf("%f",&value[i]);
 }
 printf("enter the infix expression\n");
 scanf("%s",expr);
 l=strlen(expr);
 expr[l]=')';
 expr[l+1]='\0';
 push1('(');
 i=0;
 j=0;
 while(top1>=0)
 {
  if(operand(expr[i]))
  {
   postfix[j++]=expr[i];
  }
  else if(expr[i]=='(')
  {
   push1('(');
  }
  else if(expr[i]==')')
  {
   c=pop1();
   while(c!='(')
   {
    postfix[j++]=c;
    c=pop1();
   }
  }
  else
  {
   p2=priority(expr[i]);
   p1=priority(stack1[top1]);
   while(p1>=p2)
   {
    postfix[j++]=pop1();
    p1=priority(stack1[top1]);
   }
   push1(expr[i]);
  }
  i++;
 }
 postfix[j]='\0';
 printf("Postfix expression=%s",postfix);
 printf("\n");
 i=0;
 for(i=0;postfix[i]!='\0';i++)
 {

  if(operand(postfix[i]))
  {
   val=findval(postfix[i]);
   push2(val);
  }
  else
  {
   val1=pop2();
   val2=pop2();
   switch(postfix[i])
   {
    case '+':
     val=val2+val1;
     break;
    case '-':
     val=val2-val1;
     break;
    case '*':
     val=val2*val1;
     break;
    case '/':
     val=val2/val1;
     break;
    case '^':
     val=pow(val2,val1);
     break;
    default:
     {
      printf("Error!\n");
      exit(0);
     }
   }  
   push2(val);
  }
 }
 val=pop2();
 printf("The value of expression=%f",val);
 printf("\n");
}
void push1(char c)
{

 stack1[++top1]=c;
}

char pop1(void)
{
 char c;
 c=stack1[top1];
 top1--;
 return c;
}
void push2(float c)
{
 stack2[++top2]=c;
}
                           
float pop2(void)
{
 float c;
 c=stack2[top2];
 top2--;
 return c;
}
int operand(char c)
{
 int i,flag=0;
 for(i=0;i<nv;i++)
 {
  if(variable[i]==c)
  { 
   flag=1;
  }
 }
 return flag;
}
int priority(char c)
{
 if(c=='(')
 {
  return 0;
 }
 if(c=='+')
 {
  return 1;
 }
 if(c=='-')
 {
  return 1;

 }
 if(c=='*')
 {
  return 2;
 }
 if(c=='/')
 {
  return 2;
 }
 if(c=='^')
 {
  return 3;
 }
 else
 {
  printf("Invalid operation\n");
  exit(0);
 }
}
float findval(char c)
{
 int i;
 for(i=0;i<nv;i++)
 {
  if(c==variable[i])
  {
   return value[i];
  }
  
 }
  return 0;
}

Output

C Program for open hashing



C Program for open hashing


#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *link;
};
void insert_hash(struct node**,int);
void traverse_hash(struct node*);
void delete_hash(struct node**,int);
void search_hash(struct node*,int);
main()
{
 int i,ch,key,pos;
 struct node* hash[10]={NULL};
 while(1)
 {
  printf("1-INSERTION\n2-TRAVERSING\n3-DELETION\n4-SEARCHING\n5-EXIT\n");
  printf("Enter a choice\n");
  scanf("%d",&ch);
  switch (ch)
  {
   case 1:
    printf("Enter the key\n");
    scanf("%d",&key);
    pos=key%10;
    insert_hash(&hash[pos],key);
    break;
   case 2:
    for(i=0;i<10;i++)
    {
     printf("hash[%d]:",i);
     traverse_hash(hash[i]);
    }
    break;
   case 3:
    printf("Enter key\n");
    scanf("%d",&key);
    pos=key%10;
    delete_hash(&hash[pos],key);
    break;
   case 4:
    printf("Enter key\n");
    scanf("%d",&key);
    pos=key%10;
    search_hash(hash[pos],key);
    break;
   case 5:
    exit(0);
  }
 }
}
void insert_hash(struct node**start,int key)
{
 struct node *temp,*temp1,*prev;
 temp=(struct node*) malloc (sizeof(struct node));
 if (temp==NULL)
 {
  printf("Linked list is not created\n");
 }
 else
 {
  temp->data=key;
  temp->link=NULL;
  temp1=*start;
  prev=*start;
  while( temp1!=NULL && key>temp1->data )
  {
   prev=temp1;
   temp1=temp1->link;
  }
  if (temp1==prev)
  {
   temp->link=*start;
   *start=temp;
  }
  else
  {
   temp->link=temp1;
   prev->link=temp;
  }
 }
}
void traverse_hash(struct node*start)
{
 struct node*temp;
 if (temp==NULL)
  printf("linked list is empty\n");
 else
 {
  temp=start;
  while(temp!=NULL)
  {
   printf("%d->",temp->data);
   temp=temp->link;
  } 
  printf("NULL\n");
 }
}
void delete_hash(struct node **start,int key)
{
 struct node *temp,*prev;
 if (*start==NULL)
  printf("linked list is empty\n");
 else
 {
  temp=*start;
  prev=*start;
  while (temp!=NULL && key!=(temp->data))
  {
   prev=temp;
   temp=temp->link;
  }
  if (temp==NULL)
   printf("Key is not found  \n");
  else
  {
   if (temp==prev)
    *start=(*start)->link;
   else
   {
    prev->link=temp->link;
   }
   free(temp);
  }
 } 
}
void search_hash(struct node*start,int key)
{
 struct node *temp;
 int flag;
 if(temp==NULL)
  printf("Linked list is empty\n");
 else
 {
  temp=start;
  flag=0;
  while(temp!=NULL && flag==0)
  {
   if (key==temp->data)
    flag=1;
   temp=temp->link;
  }
 
  if (flag==0)
   printf("Key is not found\n");
  else
   printf("Key is found\n");
 }
}
 

Output
 

Polynomial Addition and Multiplication using Linked List

Polynomial Addition and Multiplication using Linked List


#include<stdio.h>
#include<stdlib.h>
struct node
{
 int coef;
 int exp;
 struct node *link;
};
void insert_term(struct node **,int,int);
void traverse(struct node *);
void poly_add(struct node **,struct node **,struct node **);
void poly_pdt(struct node **,struct node **,struct node **);
void main()
{
 struct node *start_p=NULL,*start_q=NULL,*start_r=NULL;
 int i,n,c,e;
 printf("Enter first polynomial?\n");
 printf("Enter no of terms?\n");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  printf("Enter %d term\n",i+1);
  printf("Enter coefficient\n");
  scanf("%d",&c);
  printf("Enter exponent\n");
  scanf("%d",&e);
  insert_term(&start_p,c,e);
 }
 printf("Enter second polynomial\n");
 printf("Enter no of terms?\n");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  printf("Enter %d term",i+1);
  printf("Enter coefficient\n");
  scanf("%d",&c);
  printf("Enter exponent\n");
  scanf("%d",&e);
  insert_term(&start_q,c,e);
 }
 printf("First polynomial:\n");
 traverse(start_p);
 printf("second polynomial:\n");
 traverse(start_q);
 poly_add(&start_p,&start_q,&start_r);
 printf("sum of two polynomial:\n");
 traverse(start_r);
 start_r=NULL;
 poly_pdt(&start_p,&start_q,&start_r);
 printf("Product of the two polynomial:\n");
 traverse(start_r);
 
}
void insert_term(struct node **start,int c,int e)
{ 
 struct node *temp,*temp1,*prev;
 if (*start==NULL)
 {
  temp=(struct node*)malloc(sizeof(struct node));
  if (temp==NULL)
   printf("Node is not created.Term cannot be inserted\n");
  else
  {
   temp->coef=c;
   temp->exp=e;
   temp->link=NULL;
   *start=temp;
  }
 }
 else
 {
  temp1=*start;
  while (temp1!=NULL && temp1->exp>e)
  {
   prev=temp1;
   temp1=temp1->link;
  }
  if(temp1==NULL)
  {
   temp=(struct node *)malloc(sizeof(struct node));
   if (temp==NULL)
    printf("Node is not created\n");
    
   else
   {
    temp->coef=c;
    temp->exp=e;
    temp->link=NULL;
    prev->link=temp; 
   }
  }
  else
  {
   if(temp1->exp==e)
    temp1->coef=temp1->coef+c;
   else
   {
    if(temp1==*start)
    {
     temp=(struct node *)malloc (sizeof (struct node));
     if(temp==NULL)
      printf("Node is not created\n");
     else
     {
      temp->coef=c;
      temp->exp=e;
      temp->link=*start;
      *start=temp;
     }
    }
    else
    {
     temp=(struct node *)malloc(sizeof(struct node));
     if (temp==NULL)
      printf("node is not created\n");
     else
     {
      temp->coef=c;
      temp->exp=e;
      temp->link=temp1;
      prev->link=temp;
     }
    }
   } 
  }
 }

}
void traverse(struct node *start)
{
 struct node *temp;
 temp=start;
 if (temp==NULL)
  printf("Empty polynomial\n");
 else
 {
  while(temp!=NULL)
  {
   printf("+  %d X ^%d ",temp->coef,temp->exp);
   temp=temp->link;
  }
  printf("\n");
 }
 

}
void poly_add(struct node** start_p,struct node **start_q,struct node** start_r)
{
 int c,e;
 struct node *pptr,*qptr;
 *start_r=NULL;
 pptr=*start_p;
 qptr=*start_q;
 while(pptr!=NULL && qptr!=NULL)
 {
  if (pptr->exp==qptr->exp)
  {
   c=pptr->coef+qptr->coef;
   e=pptr->exp;
   insert_term(start_r,c,e);
   pptr=pptr->link;
   qptr=qptr->link;
  }
  else
  {
   if (pptr->exp>qptr->exp)
   {
    c=pptr->coef;
    e=pptr->exp;
    insert_term(start_r,c,e);
    pptr=pptr->link;
   }
   else
   {
    c=qptr->coef;
    e=qptr->exp;
    insert_term(start_r,c,e);
    qptr=qptr->link;
   }
  }
 }
 while(pptr!=NULL)
 {
  c=pptr->coef;
  e=pptr->exp;
  insert_term(start_r,c,e);
  pptr=pptr->link;
 }
 while (qptr!=NULL)
 {
  c=qptr->coef;
  e=qptr->exp;
  insert_term(start_r,c,e);
  qptr=qptr->link;
 }
 
}
void poly_pdt(struct node ** start_p,struct node **start_q,struct node** start_r)
{
 int c,e;
 struct node *pptr,*qptr;
 *start_r=NULL;
 pptr=*start_p;
 qptr=*start_q;
 if (*start_p==NULL && *start_q==NULL)
  printf("\n Multiplication of polynomial is not possible\n");
 else
 {
  while(pptr!=NULL)
  {
   qptr=*start_q;
   while(qptr!=NULL)
   {
    c=pptr->coef*qptr->coef;
    e=pptr->exp+qptr->exp;
    insert_term(start_r,c,e);
    qptr=qptr->link;
   }
   pptr=pptr->link;
  }
 }
}

Output

C Program for Binary Search Tree

C Program for Binary Search Tree


#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node  *lchild;
 struct node *rchild;
 
};
void ins_bst(struct node**,int);
void del_bst(struct node **,int);
void pre_bst(struct node *);
 void in_bst(struct  node*);
void post_bst(struct node*);
struct node *inos(struct node *);
main()
{
 int i,ch;
 struct node *root=NULL;
 while(1)
 {
  printf("*********\n\tMENU\n********");
  printf("\n1.Insertion\n2.Deletion\nTRAVERSAL\n3.Inorder\n4.Preorder\n5.Postorder\n6.Exit\nEnter ur choice\n");
 scanf("%d",&ch);
 switch(ch)
 {
  case 1:
   printf("Enter the data\n");
   scanf("%d",&i);
   ins_bst(&root,i);
   break;
  case 2:
   printf("Enter the item to be deleted\n");
   scanf("%d",&i);
   del_bst(&root,i);
   break;
  case 3:
   printf("THE INORDER TRAVERSAL\t");
   in_bst(root);
   printf("\n");
   break;
  case 4:
   printf("THE PREORDER TRAVERSAL\t");
   pre_bst(root);
   printf("\n");
   break;
  case 5:
   printf("THE POSTORDER TRAVERSAL\t");
   post_bst(root);
   break;
  case 6:
   exit(0);
   break;
  default:
   printf("SORRY WRONG CHOICE\n");

 }
}
}
void in_bst(struct node *root)
{
 struct node *ptr;
 ptr=root;
 if(ptr!=NULL)
 {
  in_bst(ptr->lchild);
  printf("%d\t",ptr->data);
  in_bst(ptr->rchild);
 }
}
void ins_bst(struct node **root,int item)
{
 int f=0;
 struct node *ptr,*temp,*parent;
 ptr=*root;
 parent=*root;
 while(ptr!=NULL&&f==0)
 {
  if(item==ptr->data)
  {
   printf("ITEM ALREADY EXISTS\n");
   f=1;
  }
  else if(item>ptr->data)
  {
   parent=ptr;
   ptr=ptr->rchild;
  }
  else if(item<ptr->data)
  { parent=ptr;
   ptr=ptr->lchild;
  }
 }
 if (f==0)
 {
  temp=(struct node*)malloc(sizeof(struct node));
  if(temp==NULL)
  {
   printf("The node is not created\n");
  }
  else
  {
   temp->data=item;
   temp->rchild=NULL;
   temp->lchild=NULL;
   if(*root==NULL)  
   {
    *root=temp;
   }
   else
   {
    if(item>parent->data)
     parent->rchild=temp;
    else
    {
     parent->lchild=temp;
    }
   }
  }
 }
}
void del_bst(struct node **root,int item)
{
 int f=0,c,i1;
 struct node *ptr1,*ptr,*parent;
 ptr=*root;
 parent=*root;
 while(ptr!=NULL&&f==0)
 {
  if(ptr->data==item)
   f=1;
  else if (item>ptr->data)
  {
   parent=ptr; 
   ptr=ptr->rchild;
  }
  else /* if(item<ptr->data)*/
  {
   parent=ptr;
   ptr=ptr->lchild;
  }
 }
 if(f==0)
 {
  printf("Item is not present\n");
  /* return(0);*/
 }
 else
 {
  if(ptr->lchild==NULL&&ptr->rchild==NULL)
  {
   c=0;
  }
  else if (ptr->lchild!=NULL && ptr->rchild!=NULL)
  {
   c=2;
  }
  else
  {
   c=1;
  }

  if(c==0)
  {
   if(parent==ptr) 
    *root=NULL;
   else
   {

    if(parent->lchild==ptr)
    { 
     parent->lchild=NULL;
    }
    else
    {
     parent->rchild=NULL;
    }
    free(ptr);
   }
  }
  if(c==1)
  {
   if(parent==ptr)
   {
    if(parent->rchild==NULL)
    {
     *root=parent->lchild;
    }
    else
    {
     *root=parent->rchild;
    }
   }
   else if(parent->lchild==ptr)
   {
    if(ptr->lchild==NULL)
    {
     parent->lchild=ptr->rchild;
    }
    else
    {
     parent->lchild=ptr->lchild;
    }
   }  
   else
   {
    if(ptr->lchild==NULL)
    {
     parent->rchild=ptr->rchild;
    }
    else
    {

     parent->rchild=ptr->lchild;
    }
   }
   free(ptr);
  }
  if(c==2)
  {
   ptr1=inos(ptr);
   i1=ptr1->data;
   del_bst(root,i1);
   ptr->data=i1;
  }
 
 }
}
struct node*inos(struct node *ptr)

{
 ptr=ptr->rchild;
 if(ptr!=NULL)
 {
  while(ptr->lchild!=NULL)
  {
   ptr=ptr->lchild;
  }
 }
 return(ptr);
}
void pre_bst(struct node *root)
{
 struct node *ptr;
 ptr=root;
 if(ptr!=NULL)
 {
  printf("%d\t",ptr->data);
  pre_bst(ptr->lchild);
  pre_bst(ptr->rchild);
 }
 
 
}
void post_bst(struct node*root)
{
 struct node *ptr;
 ptr=root;
 if(ptr!=NULL)

 {
  post_bst(ptr->lchild);
  post_bst(ptr->rchild);
  printf("%d\t",ptr->data);
 }

}

Output

C Program for Linked List Implementation

C Program for Linked List Implementation

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node*link;

};
void insert_begin_sll(struct node**,int);
void traverse_sll(struct node*);
void insert_end_sll(struct node **,int);
void insert_position_sll(struct node**,int,int);
void delete_begin_sll(struct node **);
void delete_end_sll(struct node **);
void delete_position_sll(struct node **,int);
void search_sll(struct node**,int);
int main()

{
 int ch,position,key;
 struct node *start=NULL;
 while(1)
 {
  printf("Select from the menu\n");
  printf("1.Insert node at beginning\n2.Traversing\n3.Insert node at end\n4.Insert node at specific position\n5.Delete node from beginning\n6.Delete node from end\n7.Delete node from specified position\n8.searching\n9.exit\n");
 printf("Enter a choice\n");
 scanf("%d",&ch);

 switch (ch)
 {
  case 1:
   printf("Enter key\n");
   scanf("%d",&key);
   insert_begin_sll(&start,key);
   break;
  case 2:
   traverse_sll(start);
   break;
  case 3:
   printf("Enter key\n");
   scanf("%d",&key);
   insert_end_sll(&start,key);
   break;
  case 4:
   printf("Enter key\n");
   scanf("%d",&key);
   printf("Enter position\n");
   scanf("%d",&position);
   insert_position_sll(&start,key,position);
   break;
  case 5:
   delete_begin_sll(&start);
   break;
  case 6:
   delete_end_sll(&start);
   break;
  case 7:
   printf("Enter position\n");
   scanf("%d",&position);
   delete_position_sll(&start,position);
   break;
  case 8:
   printf("Enter the key to be searched\n");
   scanf("%d",&key);
   search_sll(&start,key);
   break;
  case 9:
   exit(0);
 }
 }
}
void insert_begin_sll(struct node **start,int key)
{
 struct node *temp;
 temp=(struct node*)malloc(sizeof(struct node));
 if (temp==NULL)
  printf("Node is not created \n Insertion is not possible\n");
 else
 { 
  temp->data=key;
  temp->link=NULL;
  temp->link=*start;
  *start=temp;
 }
} 
void traverse_sll(struct node *start)
{
 struct node *temp;
 if (start==NULL)
  printf("Linked list is empty\n");
 else
 {
  temp=start;
  while(temp!=NULL)
  {
   printf("%d->",temp->data);
   temp=temp->link;
  }
  printf("NULL\n");
 }
}
void insert_end_sll(struct node**start,int key)
{
 struct node *temp,*temp1;
 temp=(struct node *)malloc(sizeof(struct node));
 if (temp==NULL)
  printf("node is not created\nInsertion is not possible\n");
 else
 {
  temp->data=key;
  temp->link=NULL;
  if(*start==NULL)
   *start=temp;
  else
  {
   temp1=*start;
   while(temp1->link!=NULL)
    temp1=temp1->link;
   temp1->link=temp;
  }
 }
}
void insert_position_sll(struct node **start,int key,int position)
{
 struct node *temp1,*temp;
 int count;
 if (position<=0)
  printf("position is invalid\n");
 else
 {
  temp=(struct node*)malloc(sizeof(struct node));
  if (temp==NULL)
   printf("Node is not created\nInsertion is not possible\n");
  else
  {
   temp->data=key;
   temp->link=NULL;
   if (position==1)
   {
    temp->link=*start;
    *start=temp;
   }
   else
   {
    temp1=*start;
    count=1;
    while (count<position-1 && temp1!=NULL)
    {
     temp1=temp1->link;
     count++;
    }
    if(temp1==NULL)
     printf("Linked list is out of range\n");
    else
    {
     temp->link=temp1->link;
     temp1->link=temp;
    }
   }
  }
 }
}
void delete_begin_sll(struct node **start)
{
 struct node *temp;
 if(*start==NULL)
  printf("Linked list is empty\n Deletion is not possible\n");
 else
 {
  temp=*start;
  *start=(*start)->link;
  free(temp);
 }
}  
void delete_end_sll(struct node **start)
{
 struct node *prev,*temp;
 if(*start==NULL)
  printf("Linked list is empty \nDeletion is not possible\n");
 else
 {
  temp=*start;
  prev=*start;
  while(temp->link!=NULL)
  {
   prev=temp;
   temp=temp->link;
  }
  if(temp==prev)
   *start=NULL;
  else
   prev->link=NULL;
  free(temp);
 }
}
void delete_position_sll(struct node **start,int position)
{
 struct node *temp,*prev;
 int count;
 if (position<=0)
  printf("position is invalid\n");
 else
 {
  if (position==1)
  {
   temp=*start;
   *start=(*start)->link;
  }
  else
  {
   temp=*start;
   prev=*start;
   count=1;
  }
  while (count<position && temp!=NULL)
  {
   prev=temp;
   temp=temp->link;
   count++;
  }
  if(temp==NULL)
   printf("Linked list is out of range\n");
  else
   prev->link=temp->link;
  free(temp);
 }
}
void search_sll(struct node **start,int key)
{
 struct node *temp;
 int flag=0;
 if(start==NULL)
  printf("Linked list is empty.\nSearching is not possible\n");
 else
 {
  temp=*start;
  while (temp!=NULL && flag==0)
  {
   if (key==temp->data)
    flag=1;
   temp=temp->link;
  }
  if (flag==0)
   printf("key is not found\n");
  else
   printf("Key is found\n");
 }
}
Output

Pass 2 of SIC Assembler(updated for gcc)

C Program for Pass 2 of SIC Assembler
Note:- first run pass1 of SIC Assembler program in the blog
Output- object.txt
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int main()
{
 FILE *fint,*ftab,*flen,*fsym,*fout;
 int c,op1[10],txtlen,txtlen1,i,j=0,x,len,txtstart;
 char add[5],starttemp[5],txtrec[70]={'\0'},s[70],symadd[5],op[5],start[10],temp[30],line[20],label[20],mne[10],operand[10],symtab[10],opmne[10];

 fint=fopen("interm.txt","r");
 flen=fopen("length.txt","r");
 ftab=fopen("optab.txt","r");
 fsym=fopen("symtab.txt","r");
 fout=fopen("objectcode.txt","w");
 
 fscanf(fint,"%s%s%s%s",add,label,mne,operand);
 if(strcmp(mne,"START")==0)
 {
  strcpy(start,operand);
  fscanf(flen,"%d",&len);
 }
 strcpy(starttemp,start);
 fprintf(fout,"H%6s%6s%06d\n",label,start,len);
 fscanf(fint,"%s%s%s%s",add,label,mne,operand);
 strcat(txtrec,"T00");
 txtstart=atoi(start);
 strcat(txtrec,start);
 strcat(txtrec,"00");
 c=8;

 while(strcmp(mne,"END")!=0)
 {
  fscanf(ftab,"%s%s",opmne,op);
  while(!feof(ftab))
  {
   if(strcmp(mne,opmne)==0)
   {
    fclose(ftab);
    fscanf(fsym,"%s%s",symadd,symtab);
    while(!feof(fsym))
    {
     if(strcmp(operand,symtab)==0)
     {       fclose(fsym);
      strcat(txtrec,op);
      strcat(txtrec,symadd);

      c=c+6;
      break;
     }
     else
      fscanf(fsym,"%s%s",symadd,symtab);
    }
    break;
   }
   else
    fscanf(ftab,"%s%s",opmne,op);
  }
  if((strcmp(mne,"BYTE")==0)||(strcmp(mne,"WORD")==0))
  {
   if(strcmp(mne,"WORD")==0)
   {
        len=strlen(operand);
        for(j=0;j<(6-len);j++)
        strcat(txtrec,"0");
        strcat(txtrec,operand);
         c=c+6;
   }
   else
   {
    len=strlen(operand);
    for(i=2;i<len;i++)
    {
     x=toascii(operand[i]);
     sprintf(s,"%d",x);
     strcat(txtrec,s);
    }
    c=c+len*2;

   }
  }
  if(c==68)
  {
   x=(c-9)/2;
   x++;
   sprintf(s,"%d",x);
   txtrec[7]=s[0];
   txtrec[8]=s[1];
   fprintf(fout,"%s\n",txtrec);
   strcpy(txtrec,"\0");
   strcat(txtrec,"T00");
   txtstart=txtstart+x;
   sprintf(s,"%d",txtstart);
   strcat(txtrec,s);
   strcat(txtrec,"00");
   c=8;
  }

  fscanf(fint,"%s%s%s%s",add,label,mne,operand);
  ftab=fopen("optab.txt","r");
  fsym=fopen("symtab.txt","r");
  fseek(fsym,SEEK_SET,0);
  fseek(ftab,SEEK_SET,0);
 }
 x=(c-9)/2;
 x++;
 sprintf(s,"%d",x);
 txtrec[7]=s[0];
 txtrec[8]=s[1];
 fprintf(fout,"%s\n",txtrec) ;
 fprintf(fout,"E00%s",start);
 fclose(fint);

 
 fclose(fout);
 fclose(flen);
 printf("object code is generated. check objectcode.txt file\n");


       
}
Output
Objectc.txt
H  copy  1000000061
T001000304410481810531C10550C10580010481810531C10550C1058001048181053
T001030281C10550C10580010481810531C10550C10587576786769000005
E001000

Pass 1 of SIC Assembler Simulation

C Program for Pass 1 of SIC Assembler Simulation

Note: - use input.txt --SIC input program              Use optab.txt--opcode table of SIC

 Output:-symtab.txt--symbol table                interm.txt- intermediate code

Here addresses are in decimal format
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int  main()
{
FILE *f1,*f2,*f3,*f4,*f5;
int lc,sa,l,op1,o,len;
char m1[20],la[20],op[20],otp[20];
f1=fopen("input.txt","r");
f3=fopen("symtab.txt","w");
f4=fopen("interm.txt","w");
f5=fopen("length.txt","w");
fscanf(f1,"%s%s%d",la,m1,&op1);
if(strcmp(m1,"START")==0)
{
sa=op1;
lc=sa;
fprintf(f4,"%d\t\%s\t%s\t%d\n",op1,la,m1,op1);
}
else
{
lc=0;
}
fscanf(f1,"%s%s",la,m1);
while(!feof(f1))
{
fscanf(f1,"%s",op);
fprintf(f4,"\n%d\t%s\t%s\t%s\n",lc,la,m1,op);
if(strcmp(la,"-")!=0)
{
fprintf(f3,"\n%d\t%s\n",lc,la);
}
f2=fopen("optab.txt","r");
fscanf(f2,"%s%d",otp,&o);
while(!feof(f2))
{
if(strcmp(m1,otp)==0)
{
lc=lc+3;
break;
}
fscanf(f2,"%s%d",otp,&o);
}
fclose(f2);
if(strcmp(m1,"WORD")==0)
{
lc=lc+3;
}
else if(strcmp(m1,"RESW")==0)
{
op1=atoi(op);
lc=lc+(3*op1);
}
else if(strcmp(m1,"BYTE")==0)
{
if(op[0]=='X')
{
lc=lc+1;
}
else
{
len=strlen(op)-2;
lc=lc+len;
}
}
else if(strcmp(m1,"RESB")==0)
{
op1=atoi(op);
lc=lc+op1;
}
fscanf(f1,"%s%s",la,m1);
}
if(strcmp(m1,"END")==0)
{
fprintf(f5,"%d",lc-sa);
}
fclose(f1);
fclose(f4);
fclose(f5) ;
fclose(f3);
      
}

Input files
optab.txt( use tab key between mnemonic code and opcode)
ADD 18
AND 40
COMP 28
DIV         24
FIX         C4
J         3C
JEQ         30
JGT          34
JLT          38
JSUB  48
LDA 00
LDB 68
LDCH 50
LDL 08
LDX 04
MUL D0
OR          44
RD          D8
RSUB  4C
STA           0C
STB           78
STCH 54
STL            14
STX           10
SUB            1C
TD             E0
TIX           2C
WD               DC

input.txt (if symbol is not in the label field place a - mark)
copy    START   1000 
-       OR      ALPHA
-       ADD     ONE
-       SUB     TWO
-       STA     BETA
- LDA     ALPHA
-       ADD     ONE
-       SUB     TWO
-       STA     BETA
- LDA     ALPHA
-       ADD     ONE
-       SUB     TWO
-       STA     BETA
- LDA     ALPHA
-       ADD     ONE
-       SUB     TWO
-       STA     BETA
ALPHA   BYTE    C'KLNCE
ONE     RESB    2
TWO     WORD    5
BETA    RESW    1
_       END     _

Output
symtab.txt

1048 ALPHA

1053 ONE

1055 TWO

1058 BETA

1061 _

interm.txt

1000 copy START 1000

1000 - OR ALPHA

1003 - ADD ONE

1006 - SUB TWO

1009 - STA BETA

1012 - LDA ALPHA

1015 - ADD ONE

1018 - SUB TWO

1021 - STA BETA

1024 - LDA ALPHA

1027 - ADD ONE

1030 - SUB TWO

1033 - STA BETA

1036 - LDA ALPHA

1039 - ADD ONE

1042 - SUB TWO

1045 - STA BETA

1048 ALPHA BYTE C'KLNCE

1053 ONE RESB 2

1055 TWO WORD 5

1058 BETA RESW 1

1061 _ END _



C Program for Round Robin algorithm

C Program for Round Robin algorithm

#include<stdio.h>
#include<stdlib.h>
struct procdetail
{
int pno;
int arrivaltime;
int bursttime;
int waittime;
int turnaroundtime;
int prevslot;
}proces[100]={0},temp;
int main()
{

int slottime,slot,i,j,complete=0,no,idle;
float avgwait=0,avgturn=0;
printf("Enter time allocated for a slot?\n");
scanf("%d",&slottime);
printf("Enter No of process?\n");
scanf("%d",&no);
printf("Enter the process details?\n");
for(i=0;i<no;i++)
{
printf("Enter the arrival time and burst tiime of process p%d\n",i);
scanf("%d%d",&proces[i].arrivaltime,&proces[i].bursttime);
proces[i].pno=i;
}
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(proces[j].arrivaltime>proces[j+1].arrivaltime)
{
temp=proces[j];
proces[j]=proces[j+1];
proces[j+1]=temp;
}
}
}

slot=0;
i=0;

idle=0;
while(!complete)
{
if(proces[i].arrivaltime<=slot && proces[i].bursttime>0)
{
idle=0;
if(proces[i].bursttime>slottime)
{

proces[i].bursttime=proces[i].bursttime-slottime;
proces[i].waittime=proces[i].waittime+(slot-proces[i].prevslot);
proces[i].turnaroundtime=proces[i].turnaroundtime+slottime;
slot=slot+slottime;
proces[i].prevslot=slot;
}
else
{




proces[i].waittime=proces[i].waittime+(slot-proces[i].prevslot);

proces[i].turnaroundtime=proces[i].turnaroundtime+proces[i].bursttime;

slot=slot+proces[i].bursttime;
proces[i].prevslot=slot;
proces[i].bursttime=0;
}
printf("P%d  ",proces[i].pno);
}


if(idle==no)
{
slot=slot+1;
idle=0;
}

idle++;
i=(i+1)%no;
complete=1;
for(j=0;j<no;j++)
{

if(proces[j].bursttime!=0)
{
complete=0;
break;

}
}


}
printf("\n process\twaiting Time\t \ttourn around time\n");
for(i=0;i<no;i++)
{
proces[i].waittime=proces[i].waittime-proces[i].arrivaltime;
proces[i].turnaroundtime=proces[i].turnaroundtime+proces[i].waittime;
printf("p%d\t%d\t \t\t\t%d\n",proces[i].pno,proces[i].waittime,proces[i].turnaroundtime);
avgturn=avgturn+proces[i].turnaroundtime+proces[i].waittime;
avgwait=avgwait+proces[i].waittime;
}

printf("Average waiting time= %f\n",(float)avgwait/no);

printf("Average Turn around time= %f\n",(float)avgturn/no);
}