Welcome to my blog, enjoy reading.

Saturday, May 22, 2010

Optimal Program using C

#include< stdio.h>
#include< conio.h>
void main()
{
 int page[20],n,f,i,j,a[20],pf=0,k=0,match;
 clrscr();
 printf("enter the length of the reference string");
 scanf("%d",&n);
 printf("enter the frame size");
 scanf("%d",&f);

 printf("enter reference string");
 for(i=0;i< n;i++)
 scanf("%d",&page[i]);
 for(j=0;j< f;j++)
 a[j]=-1;

 printf("\n-1\t-1\t-1\n\n");

 k=0;
 for(i=0;i< n;i++)
 {
  match=0;
  for(j=0;j< f;j++)
  if(a[j]==page[i])
  {
   match=1;
  }
  if(match==1)
  {
   goto x;
  }

  for(j=0;j< f;j++)
  if(a[j]==-1)
  {
   a[j]=page[i];
   pf++;
   goto x;
  }

  a[k]=page[i];
  k=(k+1)%f;
  pf++;
  x:;
  for(j=0;j< f;j++)
  printf("%d \t",a[j]);
  printf("\n\n");
 }
 printf("no of page faults are %d",pf);
 getch();
}


First In First Out program using C (F.I.F.O)

#include< stdio.h>
#include< conio.h>
void main()
{
 int page[20],n,f,i,j,a[20],pf=0,k=0,match;
 clrscr();
 printf("enter the length of the reference string");
 scanf("%d",&n);
 printf("enter the frame size");
 scanf("%d",&f);

 printf("enter reference string");
 for(i=0;i< n;i++)
 scanf("%d",&page[i]);
 for(j=0;j< f;j++)
 a[j]=-1;

 printf("\n-1\t-1\t-1\n\n");

 k=0;
 for(i=0;i< n;i++)
 {
  match=0;
  for(j=0;j< f;j++)
  if(a[j]==page[i])
  {
   match=1;
  }
  if(match==1)
  {
   goto x;
  }

  for(j=0;j< f;j++)
  if(a[j]==-1)
  {
   a[j]=page[i];
   pf++;
   goto x;
  }

  a[k]=page[i];
  k=(k+1)%f;
  pf++;
  x:;
  for(j=0;j< f;j++)
  printf("%d \t",a[j]);
  printf("\n\n");
 }
 printf("no of page faults are %d",pf);
 getch();
}


Least Recently Used program using C(L.R.U)

#include< stdio.h>
#include< conio.h>
void main()
{
 int page[20],n,f,i,j,a[20],pf=0,match,index,counter=0,count[20];
 clrscr();
 printf("enter the length of the reference string");
 scanf("%d",&n);
 printf("enter the frame size");
 scanf("%d",&f);

 printf("enter reference string");
 for(i=0;i< n;i++)
 scanf("%d",&page[i]);
 for(j=0;j< f;j++)
 a[j]=-1;

 printf("\n-1\t-1\t-1\n\n");

 for(i=0;i< n;i++)
 {
  match=0;
  for(j=0;j< f;j++)
  if(a[j]==page[i])
  {
   match=1;
   counter++;
   count[j]=counter;
  }
  if(match==1)
  {
   goto x;
  }

  for(j=0;j< f;j++)
  if(a[j]==-1)
  {
   a[j]=page[i];
   counter++;
   count[j]=counter;
   pf++;
   goto x;
  }

  index=0;
  for(j=0;j< f;j++)
  {
   if(count[j]< count[index])
   {
    index=j;
   }
  }
  a[index]=page[i];
  counter++;
  count[index]=counter;
  pf++;
  x:;
  for(j=0;j< f;j++)
  printf("%d \t",a[j]);
  printf("\n\n");
 }
 printf("no of page faults are %d",pf);
 getch();
}


Saturday, May 1, 2010

Bit stuffing of data using C

#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
    int i,j,a=0;
    char sdata[17],stuff[40],rdata[17];
    clrscr();
    printf("Enter the data in the form of 16 bits     : ");
    gets(sdata);
    printf("\nSender frame                              : ");
    puts(sdata);
    for(i=0,j=0;j <16;j++)
    {
        if(sdata[j]=='1')
        {
            a=a+1;
            if((a==5)&&(sdata[j+1]=='0'))
            {
                stuff[i]=sdata[j];
                i++;
                stuff[i]='0';
                a=0;
                i++;
            }
            else if(a==6)
            {
                stuff[i]='0';
                a=0;
                i++;
                j--;
            }
            else
            {
                stuff[i]=sdata[j];
                i++;
            }
        }
        else
        {
            stuff[i]=sdata[j];
            i++;
            a=0;
        }
    }
    stuff[i]='\0';
    printf("\nSender frame with bit stuffed             : ");
    puts(stuff);
    a=0;
    i=0;
    for(j=0;stuff[j]!='\0';j++)
    {
        if(stuff[j]=='1')
        {
            a=a+1;
            if(a==5)
            {
                a=0;
                rdata[i]=stuff[j];
                j++;
                i++;
            }
            else
            {
                rdata[i]=stuff[j];
                i++;
            }
        }
        else
        {
            a=0;
            rdata[i]=stuff[j];
            i++;
        }
    }
    rdata[i]='\0';
    printf("\nThe reciever data  without stuffed bit is : ");
    puts(rdata);
    if(strcmp(sdata,rdata)==0)
    printf("\nTransmission successful");
    else
    printf("\nTransmission error");
    getch();
}

Character stuffing of data using C

#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
    char sdata[17],rdata[17],cstuff[40];
    int i,j;
    clrscr();
    printf("Enter any 16 characters   : ");
    gets(sdata);
    printf("\nOriginal data             : %s\n",sdata);
    for(i=0,j=0;sdata[i]!='\0';i++)
    {
    if(sdata[i]=='f'&&sdata[i+1]=='l'&&sdata[i+2]=='a'&&sdata[i+3]=='g')
    {
        cstuff[j++]='e';
        cstuff[j++]='s';
        cstuff[j++]='c';
        cstuff[j++]=sdata[i];
    }
    else if(sdata[i]=='e'&&sdata[i+1]=='s'&&sdata[i+2]=='c')
    {
        cstuff[j++]='e';
        cstuff[j++]='s';
        cstuff[j++]='c';
        cstuff[j++]=sdata[i];
    }
    else
    {
        cstuff[j++]=sdata[i];
    }
    }
    cstuff[j]='\0';
    printf("\nAfter character stuffing  : %s\n",cstuff);
    for(i=0,j=0;cstuff[i]!='\0';i++)
    {
    if(cstuff[i]=='e'&&cstuff[i+1]=='s'&&cstuff[i+2]=='c')
    {
        i=i+3;
        rdata[j++]=cstuff[i];
    }
    else
    {
        rdata[j++]=cstuff[i];
    }
    }
    rdata[j]='\0';
    printf("\nReceiver data             : %s",rdata);
    if(strcmp(sdata,rdata)==0)
    printf("\n\n\nTransmission successful");
    else
    printf("\n\n\nTransmission error");
    getch();
}

Cyclic Redundancy check Using C

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


char I[40],d[40]="1100000001011";
char a[12]="000000000000",t[13];
int i=0,j,l,n,p;


void main()
{
    void division(void);
    clrscr();
    printf("\n enter a message string    : ");
    gets(I);
    n=strlen(I);
    printf("\nOriginal message             : %s \n",I);
    strcat(I,a);
    p=n+12;
    division();
    printf("\nThe reminder at sender is : %s \n",t);
    for(i=n,j=1;i     {
    I[i]=t[j];
    }
    printf("\nThe padded string to be sent : %s \n",I);
    division();
    printf("\nThe remainder string at receiver : %s\n",t);
    printf("\nThe received message is correct");
    getch();
}

void division(void)
{
    for(i=0;i <13;i++)
    t[i]=I[i];
    while(i 
  {
    for(l=0;l <13;l++)
    {
        if(t[l]==d[l])
         t[l]='0';
        else
         t[l]='1';
    }
    if(t[0]=='0')
    {
        for(j=0;j <12;j++)
         t[j]=t[j+1];

        t[j]=I[i++];
    }
    }
    while(t[0]=='1')
    for(l=0;l <13;l++)
    {
    if(t[l]==d[l])
        t[l]='0';
    else
        t[l]='1';
    }
    t[l]='\0';
}






Thursday, April 29, 2010

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

void main()
{

int a[20],c[20],page[20];
int i,j,k,f,n;
int count=0;fault=0,hit=0;
clrscr();
printf("Enter reference string length : ");
scanf("%d",&n);
printf("Enter reference string");
for(i=0;i < n;i++)
   scanf("%d",&page[i]);
printf("Enter frame size ");
scanf("%d",&f);

for(i=0;i < f;i++)
{
   for(j=0;j < fault;j++)
   {
      if(page[i]==a[j])
      {
         hit++;
         c[j]=++count;
         goto xyz;
      }
    }
    a[i]=page[i];
    fault++;
    c[i]=++count;
    xyz: ;
}