Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

Friday, 22 February 2013

Round Robin algorithm program in C

Programming Language : C

Code :
#include<stdio.h>
#include<conio.h>
int rr();


int z[10],b[10],n,m[50],r,q,e=0,avg=0,i,j;
float f;

main()
{
 clrscr();
 printf("\n\tJOB SCHEDULING ALGORITHM[Round Robin]");
 printf("\n\t*******************************************************\n");
 printf("\nEnter how many jobs:");
 scanf("%d",&n);
 printf("\nEnter burst time for corresponding job...\n");
 for(i=1;i<=n;i++)
 {
  printf("\nProcess %d: ",i);
  scanf("%d",&b[i]); z[i]=b[i];
 }

 printf("\nENTER THE TIME SLICE VALUE:");
 scanf("%d",&q);

 rr();


 getch();
 return 0;
}




rr()
{
 int max=0;
 max=b[1];
    for(j=1;j<=n;j++)
       if(max<=b[j])
    max=b[j];

 if((max%q)==0)
   r=(max/q);
 else
  r=(max/q)+1;
   for(i=1;i<=r;i++)
   {
      printf("\nround %d",i);
      for(j=1;j<=n;j++)
    {
      if(b[j]>0)
        {
         b[j]=b[j]-q;

         if(b[j]<=0)
        {
        b[j]=0;
         printf("\nprocess %d is completed",j);
        }
         else
         printf("\nprocess %d remaining time is %d",j,b[j]);
        }
     }

    }
   return 0;
}

Written by 'Shojib'.

Sortest Job Next Algorithm implement in C

Programming Language : C

Code :

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

void main()
{
 int n,b[10],w[10],i,j,h,t,tt;
 int stime[10],a[10];
 float avg=0;
 clrscr();
 printf("\nEnter how many jobs:");
 scanf("%d",&n);
 printf("\nEnter burst time for corresponding job....\n");
 for(i=1;i<=n;i++)
 {
  printf("\nProcess %d:",i);
  scanf("%d",&b[i]);  a[i]=i;
 }
    for(i=1;i<=n;i++)
           for(j=i;j<=n;j++)
              if(b[i]>b[j])
              {
              t=b[i]; tt=a[i];
              b[i]=b[j];  a[i]=a[j];
              b[j]=t;     a[j]=tt;
              }

 w[1]=0;


 printf("\nprocess %d waiting time is 0",a[1]);

 for(i=2;i<=n;i++)
 {
  w[i]=b[i-1]+w[i-1];
  printf("\nProcess %d waiting time: %d",a[i],w[i]);
  avg+=w[i];
 }

 printf("\ntotal waiting time:%f",avg);
 printf("\n\nthe average waiting time is:%f\n",avg/n);



getch();
}


Written by 'Shojib'.

Cyclic Redundancy Check(CRC) program in C

Programming Language : C

Code :

#include<stdio.h>
#include<string.h>
#define N strlen(g)

char t[28],cs[28],g[]="1010";
int a,e,c;

void xor(){
     for(c = 1;c < N; c++)
     cs[c] = (( cs[c] == g[c])?'0':'1');
}

void crc(){
     for(e=0;e<N;e++)
          cs[e]=t[e];
    do{
          if(cs[0]=='1')
                xor();
          for(c=0;c<N-1;c++)
                cs[c]=cs[c+1];
          cs[c]=t[e++];
     }while(e<=a+N-1);
}

int main()
{
    printf("\n\tCyclic Redundancy Check ");
    printf("\n\n\tShojibul Alam Shojib ");
     printf("\n\t-------------------------------");
     printf("\nEnter data : ");
     scanf("%s",t);
     printf("\n-------------------------------");
     printf("\nGeneratng polynomial : %s",g);
     a=strlen(t);
     for(e=a;e<a+N-1;e++)
          t[e]='0';
     printf("\n----------------------------------------");
     printf("\nModified data is : %s",t);
     printf("\n----------------------------------------");
     crc();
     printf("\nChecksum is : %s",cs);
     for(e=a;e<a+N-1;e++)
          t[e]=cs[e-a];
     printf("\n----------------------------------------");
     printf("\nFinal codeword is : %s",t);
     printf("\n----------------------------------------");
     printf("\nTest error detection 0(yes) 1(no)? : ");
     scanf("%d",&e);
     if(e==0)
    {
        do{
                printf("\nEnter the position where error is to be inserted : ");
                scanf("%d",&e);
          }while(e==0 || e>a+N-1);
          t[e-1]=(t[e-1]=='0')?'1':'0';
          printf("\n----------------------------------------");
          printf("\nErroneous data : %s\n",t);
    }
     crc();
     for(e=0;(e<N-1) && (cs[e]!='1');e++);
          if(e<N-1)
                printf("\nError detected\n\n");
        else
            printf("\nNo error detected\n\n");
                printf("\n----------------------------------------\n");
          return 0;
}


Written by 'Shojib'.

Parity bit checking for error detecting program in c

Programming Language : C

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

struct s
{
        int seq_no,frame[10],parity;
}s1[20],tmp[20];
void copy(struct s[]);
void parityCount(struct s[]);
void print(struct s[]);
void error(int *);
void sender(int *,int);
void reciver(struct s[]);
int main()
{
        char str[20];
          int i,j,as,q,arr[10],a[50],n,k;
          printf("\t\t\tShojibul Alam Shojib\n\n");
        printf("Enter any string..");
        scanf("%s",str);
        n=0;
        printf("\n\n String in binary..\n\n");
        for(j=0;str[j];j++)
        {
                i=0;
                as=toascii(str[j]);
                q=as;
                while(q!=1)
                {
                        arr[i++]=q%2;
                        q/=2;
                }
                arr[i++]=1;
                for(k=i-1;k>=0;k--)
                        a[n++]=arr[k];
        }
        for(j=0;j<n;j++)
                printf("%d",a[j]);
        printf("\n");
        sender(a,n);
        return 0;
}
int len;
void sender(int a[],int i)
{
        int j,no=0,k;
        len=i/10;
        if(i%10==0)
                len--;
        for(j=0;j<=len;j++)
        {
                s1[j].seq_no=(j+1);
                for(k=0;k<10;k++)
                {
                        if(no!=i)
                                s1[j].frame[k]=a[no++];
                }
        }
        parityCount(s1);
        copy(s1);
        printf("\n Sending data..\n\n");
        print(s1);
        reciver(s1);
}
void copy(struct s temp[])  // After receiving check for correct frame.
{
        int j,k;
        for(j=0;j<=len;j++)
        {
                tmp[j].seq_no=(j+1);
                for(k=0;k<10;k++)
                        tmp[j].frame[k]=s1[j].frame[k];
                tmp[j].parity=temp[j].parity;
        }
}
void parityCount(struct s *s1) // For count the parity bits of frame.
{
        int ans=0,j,k;
        for(j=0;j<=len;j++)
        {
                for(k=0;k<10;k++)
                        ans=ans ^ s1[j].frame[k];
                s1[j].parity=ans;
        }
}

void print(struct s s1[]) // Printing individual frames.
{
        int j,k;
        for(j=0;j<=len;j++)
        {
                printf("\n\t%d \t ",s1[j].seq_no);
                for(k=0;k<10;k++)
                        printf("%d",s1[j].frame[k]);
                printf("\t %d ",s1[j].parity);
                printf("\n");
        }
}
void error(int *a)  // To give the error to frame.
{
        int j;
        for(j=0;j<9;j++)
        {
                     a[j]=a[j+1];
        }
}



void reciver(struct s s2[])
{
        int k,j,flg=1;
          error(&s2[0].frame[0]);
          parityCount(&s2[0]);
        printf("\n\n");              
        for(j=0;j<=len;j++)
        {
                if(s2[j].parity != tmp[j].parity)
                {
                        flg=0;
                        break;
                }
                else
                        flg=1;
        }
        printf("\n\n");
          if(flg==0)

                printf("\n Wrong Data Recievd..\n");
        else

                printf("\n Correct Data Recievd..\n");

        printf("\n Recived data..\n\n");
        print(s2);
}





Written by 'Shojib'

Thursday, 4 October 2012

Checking Keyword and Identifier

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



void main()
{
again:


int i,flag=0,m;
char s[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto","if","int",
"long","register","return","short","signed","sizeof","static","struct",
"switch","typedef","union","unsigned","void","volatile","while"},st[10];
clrscr();
  printf("Checking Keyword and Identifier \n") ;
printf("\n Enter the string :");
gets(st);

for(i=0;i<32;i++)
{
m=strcmp(st,s[i]);
if(m==0)
flag=1;
}
if(flag==0) {


if((st[0]>'0' && st[0]<='9') || (st[0]>='a'&& st[0]<='z') || (st[0]>='A')&& st[0]<='Z'|| st[0]>='_')
{
    if(st[0]='0' && st[0]<='9')
    {
    printf("\n\tIdentifier is not valid");
    }
    else
    {
printf("\n \tThis is identifier");
}

}


}
else
printf("\n\tThis is a keyword\n");


getch();
 goto again;
}


Written by 'Shojib'

Monday, 20 June 2011

Insertion sort program in c



#include <stdio.h>

void insertion_sort();
int a[50],n;

main()
{
  int i;
  
  printf("\nEnter size of an array: ");
  scanf("%d", &n);
  
     printf("\nEnter elements of an array:\n");
  for(i=0; i<n; i++)
    scanf("%d", &a[i]);
  
     insertion_sort();
  
     printf("\n\nAfter sorting:\n");
  for(i=0; i<n; i++)
    printf("\n%d", a[i]);
    
 
}

void insertion_sort()
{
  int j, k, temp;
  
     for(j=1; j<n; j++)
     {
    temp = a[j];
          k = j-1;
      
          while (k>=0 && a[k]>temp)
    {
      a[k+1] = a[k];
      k--;
    }
          a[k+1] = temp;
     }    
}
Editing & Program by 'Shojib'

Selection Sort Program in c



#include <stdio.h>

void selection_sort();
int a[50], n;

main()
{
  int i;
  
  printf("\nEnter size of an array: ");
  scanf("%d", &n);
  
     printf("\nEnter elements of an array:\n");
  for(i=0; i<n; i++)
    scanf("%d", &a[i]);
  
     selection_sort();
  
     printf("\n\nAfter sorting:\n");
  for(i=0; i<n; i++)
    printf("\n%d", a[i]);


}

void selection_sort()
{
     int i, j, min, temp;

     for (i=0; i<n; i++)
     {
       min = i;
          for (j=i+1; j<n; j++)
          {
      if (a[j] < a[min])
        min = j;
          }
          temp = a[i];
          a[i] = a[min];
          a[min] = temp;
     }
}
Editing & Program by 'Shojib'

Tower of Hanoi Programm in c++

#include<iostream.h>

void move(int n,char *b,char *a,char *e)
{
 if(n>0)
 {
  move(n-1,b,e,a);
  cout<<"disk "<<n<<" is moved from "<<b<<" to "<<e<<endl;
    move(n-1,a,b,e);
 }
}

void main()
{
 cout<<"\n";
 cout<<"Enter the no. of disks: ";
 int n;
 cin>>n;
 move(n,"beg","aux","end");
}
Editing & Program by 'Shojib'

Sunday, 5 June 2011

Merging in arrays program in c++

#include<iostream.h>
void main()
{
int na, nb, ptr, r,s, k,a[50],b[50],c[50];
int i,j,m;
cout<<"Number of element for Array[A] : "; cin>>r;
cout<<"Number of element for Array[B] : "; cin>>s;
cout<<"Array : "<<endl;
for(i=1;i<=r;i++)
{
cout<<"A["<<i<<"] : ";
cin>>a[i];
}
cout<<"\n";
  for(j=1;j<=s;j++)
  {
  cout<<"B["<<j<<"] : ";
  cin>>b[j];
  }
  na = 1;
  nb = 1;
  ptr= 1;
  while(na<=r && nb<=s)
  {
  if(a[na]<b[nb])
  {
  c[ptr] = a[na];
  ptr = ptr +1; na = na+1;
  }
  else
  {
  c[ptr] = b[nb];
  ptr = ptr+1;
  nb= nb+1;
  }
  }
  if(na>r)
  {
  for(k=0;k<=s-nb;k++)
  {
  c[ptr+k]=b[nb+k];

  }
  cout<<"After sorting these two arrays :"<<endl;
  for(i=1;i<=r+s;i++)
  {
  cout<<"Array["<<i<<"] : "<<c[i]<<endl;
  }

  }
  else
  {
  for(k=0;k<=r-na;k++)
  {
  c[ptr+k]=a[na+k];
  cout<<c[k];
  }
    cout<<"After sorting"<<endl;
  for(i=1;i<=r+s;i++)
  {
  cout<<"Array["<<i<<"] : "<<c[i]<<endl;
  }

  }

}
Editing & program by "Shojib"

Ternary search programm in c++

#include<iostream.h>
void main()
{
int i,j,n,l,u,loc,a[1000],x,y,p;
cout<<"No of Element : "; cin>>n;
cout<<"\n";
cout<<"Array  :"<<endl;
for(p=1;p<=n;p++)
{
cout<<"array ["<<p<<"] = "; cin>>a[p];
}
cout<<"Location search item : ";
  cin>>y;
 i=1;
 j=n;
 while(i<j-1)
 {
 l = (i+j)/3;
 u = (2*(i+j))/3;
 if(y>a[u])
 {
 i= u+1;
 }
 else if(y>a[l])
 {
 i=l+1;
 j=u ;
 }
 else
 {j=l;}
    }

 if(y == a[i])
 {loc = i;
 cout<<"Location :"<<loc<<" position";
 }
 else if(y == a[j])
 {loc = j;
 cout<<"Location :"<<loc<<" position";
 }
 else
 {loc=0;
 cout<<"Location :"<<loc<<" position";
 }
}
Editing & program by "Shojib"

Find Maximun number in the array programm in c++

#include<iostream.h>
void main()
{
int a[2000], n,i, cmax;
cout<<"n : "; cin>>n;
cout<<"\n";
cout<<" Enter Array: "<<endl;
for(i=0;i<n;i++)
{
cout<<"["<<i<<"] : ";
cin>>a[i];
}
 cmax = a[0];
 for(i=1;i<=n-1;i++)
 {
 if(cmax<a[i])
 {cmax = a[i];
 }

 }
 cout<<"maximum number : "<<cmax;
}
Editing & programm by "Shojib"