Thursday, 19 September 2013

FIND MAX AND MIN SIMULTANEOUSLY C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

 Program:C Program to find maximum and minimum simultaneously*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[20],max,min,i,j;
clrscr();
printf("enter the no. of elemnets to be enterd");
scanf("%d",&n);
//entering data in array
printf("enter the elements");
for(i=1;i<=n;i++)
scanf("%d",&arr[i]);
// assigning last element in max and min
max=arr[1];
min=arr[1];
//comparing the elements with max and min
 for(i=2;i<n;i++)
 {
 if(arr[i]> max)
 {
  max=arr[i];
 }
 if(arr[i]< min)
 {
 min=arr[i];
 }
//printing the elements
printf("minimum element is %d",min);
printf("\nmaximum element is %d",max);
getch();
}
/*OUTPUT:
enter the no. of elemnts to be enterd 6
enter the elements
15   6    2   18   5  78
minimum element is 2
maximum element 78
*/

Wednesday, 4 September 2013

STRASSEN'S MATRIX MULTIPLICATION C PROGRAM



/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C PROGRAM FOR STRASSEN'S MATRIX MULTIPLICATION*/

#include<stdio.h>
#include<conio.h>
void main()
{
int arr1[2][2],arr2[2][2],i,j,p1,p2,p3;
int p4,p5,p6,p7,r,s,t,u,a,b,c,d,e,f,g,h;
clrscr();
printf("enter the elements of matrix1\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("enter the value of a[%d][%d]",i,j);
scanf("%d",&arr1[i][j]);
}}
printf("enter the elements of matrix2\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("enter the value of a[%d][%d]",i,j);
scanf("%d",&arr2[i][j]);
}}
a=arr1[0][0];
b=arr1[0][1];
c=arr1[1][0];
d=arr1[1][1];
e=arr2[0][0];
f=arr2[0][1];
g=arr2[1][0];
h=arr2[1][1];
p1=(a*f)-(a*h);
p2=(a+b)*h;
p3=(c+d)*e;
p4=(g-e)*d;
p5=(a+d)*(e+h);
p6=(b-d)*(g+h);
p7=(a-c)*(e+f);
r=p5+p4-p2+p6;
s=p1+p2;
t=p3+p4;
u=p5+p1-p3-p7;
printf("%d\t %d\n %d \t %d",r,s,t,u);
getch();
}
/*OUTPUT
enter the elements of matrix1
enter the value of a[0][0] 1
enter the value of a[0][1] 3
enter the value of a[1][0] 5
enter the value of a[1][1] 7
enter the elements of matrix2
enter the value of a[0][0] 8
enter the value of a[0][1] 4
enter the value of a[1][0] 6
enter the value of a[1][1] 2
 26   10
 82   34
*/

SELECTION SORT C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

Program: C program to implement selection sort*/

#include<stdio.h>
#include<conio.h>
void main( )
{
int arr[10];
int i, j, temp,n ;
printf("enter the no. of inputs");
scanf("%d",&n);
printf("enter the numbers");
for(i=0;i<n;i++)
{scanf("%d",&arr[i]);}
for ( i = 0 ; i < n-1 ; i++ )
{for ( j = i + 1 ; j < n ; j++ )
{
if ( arr[i] > arr[j] )
{
temp = arr[i] ;
       arr[i] = arr[j] ;
arr[j] = temp ;
}
} }
 printf ( "Array after sorting:\n") ;
for ( i = 0 ; i < n ; i++ )
printf ( "%d\t", arr[i] ) ;
}
/*Output
enter the no. of inputs 5
enter the numbers
12  4   67  1   90
Array after sorting:
1   4   12  67  90*/

QUICK SORT C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME:PROGRAM IN C FOR QUICK SORT*/
#include<stdio.h>
#include<conio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
void main()
{
int a[30],n,i;
clrscr();
printf("enter the no. of element:");

scanf("%d",&n);
printf("enter the element");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

quick_sort(a,0,n-1);
printf("\sorted elements\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void quick_sort(int a[],int p,int r)
{int q;
if(p<r)
{
q=partition(a,p,r);
quick_sort(a,p,q-1);
quick_sort(a,q+1,r);}
}

int partition(int a[],int p,int r)
{
int x;
int i,temp,j;
i=p-1;
x=a[r];
for(j=p;j<r-1;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;}
temp=a[i+1];
a[i+1]=a[r];
a[r]=a[i+1];}
return(i+1);
}

MERGE SORT C PROGRAM



/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C program to implement merge sort algorithm*/
#include<stdio.h>
#include<conio.h>
void mergesort(int[], int, int);
void merge(int[], int, int, int);
void main()
{
int n,i,arr[20];
clrscr();
printf("Enter the no. of elements of array");
scanf("%d",&n);
printf("Enter the elements of array");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
mergesort(arr,0,n-1);
printf("Sorted array is:");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getch();
}

void mergesort(int arr[],int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
mergesort(arr,p,q);
mergesort(arr,q+1,r);
merge(arr,p,q,r);
}
}

void merge(int arr[], int p, int q, int r)
{
int n1,n2,l[20],r1[20],i,j,k;
n1=q-p+1;
n2=r-q;
for(i=0;i<n1;i++)
{
l[i]=arr[p+i-1];
}
for(i=0;i<n2;i++)
{
r1[i]=arr[q+i];
}
l[n1]=1/0;
r1[n2]=1/0;
i=1;
j=1;
for(k=p;k<r;k++)
{
if(l[i]>r1[j])
{
arr[k]=l[i];
i++;
}
else
{
arr[k]=r1[j];
j++;
}
}
}

LONGEST COMMON SUBSEQUENCE C PROGRAM



/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C PROGRAM FOR LCS*/

#include<stdio.h>
#include<conio.h>
void print_lcs(char b[20][20],char x[],int,int);
int main()
{
char arr[10],arr1[10];
char b[20][20];
int i,j,k,num,num1,c[20][20];
printf("enter the values to be entered for 1st");
scanf("%d",&num);
 printf("enter the string x\n");
 for(i=1;i<=num;i++)
 scanf("%s",&arr[i]);
 printf("enter the values to be entered for 2nd");
 scanf("%d",&num1);
 printf("enter the string y\n");
  for(i=1;i<=num1S;i++)
  scanf("%s",&arr1[i]);
   for(i=1;i<=num;i++)
   c[i][0]=0;
for(j=0;j<=num1;j++)
c[0][j]=0;
  for(i=1;i<=num;i++)
   {
    for(j=1;j<=num1;j++)
     {
     if(arr[i]==arr1[j])
     {c[i][j]=c[i-1][j-1]+1;
      b[i][j]='d'; // d is for diagonal
     }
     else if(c[i-1][j]>=c[i][j-1])
     {c[i][j]=c[i-1][j];
     b[i][j]='u'; // u is for up
     }
   else
     {c[i][j]=c[i][j-1];
     b[i][j]='s'; // s is for right side
     }
     }
  }
    for(i=0;i<=num;i++)
    {
    for(j=0;j<=num1;j++)
     {
      printf("\t%d",c[i][j]);
     }
     printf("\n");
    }
printf("\n the notation matrix is:\n");
 for(i=1;i<=num;i++)
 {
  for(j=1;j<=num1;j++)
  {
   printf("\t%c",b[i][j]);
  }
  printf("\n");
 }
  printf("\nthe mateched string \n");
  print_lcs(b,arr,num,num1);

   getch();
}

void print_lcs(char e[20][20],char x[10],int i,int j)
{
 if(i==0 || j==0)
 {
  return ;
 }
 if( e[i][j]=='d')
 {
 printf("\t%c",x[i]);
 print_lcs(e,x,i-1,j-1);
 }
 else if (e[i][j]=='u')
{
print_lcs(e,x,i-1,j);
}
else
{
 print_lcs(e,x,i,j-1);
}
}

/*OUTPUT:

enter the values to be entered for 1st7
enter the string x
a
b
c
b
d
a
b
enter the values to be entered for 2nd6
enter the string Y
b
d
c
a
b
a

        0       0       0       0       0       0       0
        0       0       0       0       1       1       1
        0       1       1       1       1       2       2
        0       1       1       2       2       2       2
        0       1       1       2       2       3       3
        0       1       2       2       2       3       3
        0       1       2       2       3       3       4
        0       1       2       2       3       4       4

 the notation matrix is:
        u       u       u       d       s       d
        d       s       s       u       d       s
        u       u       d       s       u       u
        d       u       u       u       d       s
        u       d       u       u       u       u
        u       u       u       d       u       d
        d       u       u       u       d       u

the mateched string
        a       b       c       b
*/

HEAP SORT C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
Program:C program to implement heap sort*/

#include<stdio.h>
#include<conio.h>
void heapsort(int[],int);
void maxheap(int[],int);
void heapify(int[],int);
void main()
{
 int n,i,a[50];
 printf("Enter the no. to be input:\n");
 scanf("%d",&n);
 printf("Enter the elements:\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 heapsort(a,n);
 printf("\nThe Sorted Elements Are:");
 for(i=0;i<n;i++)
  printf("\t%d",a[i]);
 getch();
}
void heapsort(int a[],int n)
{
 int i,t;
 maxheap(a,n);
 for(i=n-1;i>0;i--)
 {
  t = a[0];
  a[0] = a[i];
  a[i] = t;
  heapify(a,i);
 }
}
void maxheap(int a[],int n)
{
 int k,i,j,item;
 for(k=1;k<n;k++)
 {
  item = a[k];
  i = k;
  j = (i-1)/2;
 while((i>0)&&(item>a[j]))
  {
   a[i] = a[j];
   i = j;
   j = (i-1)/2;
  }
  a[i] = item;
 }
}

void heapify(int a[],int n)
{
 int i,j,item;

 j = 0;
 item = a[j];
 i = 2*j+1;

 while(i<=n-1)
 {
  if(i+1 <= n-1)
   if(a[i] <a[i+1])
    i++;
  if(item<a[i])
  {
   a[j] = a[i];
   j = i;
   i = 2*j+1;
  }
  else
   break;
 }
 a[j] = item;
}
/*Output
Enter the no. to be input: 5
Enter the elements:
12 34 7 9 22
The Sorted Elements Are:
7 9 12 22 34*/

FRACTIONAL KNAPSACK C PROGRAM



/*CSEMATTER.BLOGSPOT.IN

Program:C Program to implement fractional kanpsack*/


#include<stdio.h>
#include<conio.h>
int main()
{
    float min(float ,float);
    int i,j,n;
    float p[20],w[20],v[20],sol[20],amt,W,temp,sum_sol=0.0;
    printf("Enter the no. of entities ");
    scanf("%d",&n);
    printf("Enter the no. of entities of each items");
    for(i=0; i<n; i++)
        scanf("%f",&w[i]);
    printf("Enter respective costs of entities");
    for(i=0; i<n; i++)
        scanf("%f",&v[i]);
    printf("Enter the capacity of knapsack: ");
    scanf("%f",&W);

    for(i=0; i<n; i++)
        p[i]=v[i]/w[i];
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(p[i]<p[j])
            {
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;

                temp=w[i];
                w[i]=w[j];
                w[j]=temp;
            }
        }
    }
    i=0;
    while(W>0)
    {
        amt=min(W,w[i]);
        sol[i]=amt;
        W=W-amt;
        i++;
    }
    printf("Solution array is :");
    for(i=0; i<n; i++)
        printf("%f \t",sol[i]);
        for(i=0;i<n;i++)
        sum_sol=sum_sol+sol[i];
        printf("\n Hence maximum profit entity is: %f",sum_sol);
    getch();
}

float min(float W,float a)
{
    if(W>a)
        return a;
    else
        return W;
}

/*OUTPUT:
Enter the no. of entities 5
Enter the no. of entities of each items5
10
20
30
40
Enter respective costs of entities30
20
100
90
160
Enter the capacity of knapsack: 60
Solution array is :5.000000     20.000000       35.000000       0.000000      0.000000
 Hence maximum profit entity is: 60.000000  */

0/1 KNAPSACK C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

Program:C Program to implement 0/1 Knapsack*/


#include<stdio.h>
#include<conio.h>
int main()
{
    int i,j,n,w[20],v[20],W,c[10][10];
    printf("Enter the no. of entities ");
    scanf("%d",&n);
    printf("Enter the no. of entities of each items");
    for(i=0; i<n; i++)
        scanf("%d",&w[i]);
    printf("Enter respective costs of entities");
    for(i=0; i<n; i++)
        scanf("%d",&v[i]);
    printf("Enter the capacity of knapsack: ");
    scanf("%d",&W);

    for(i=0;i<=W;i++)
    c[0][i]=0;
    for(i=1;i<=n;i++)
    c[i][0]=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=W;j++)
        {
            if(w[i]<=j)
            {
                if((v[i]+c[i-1][j-w[i]])>c[i-1][j])
                c[i][j]=v[i]+c[i-1][j-w[i]];
                else
                {c[i][j]=c[i-1][j];}
            }
            else
            {c[i][j]=c[i-1][j];}
        }
    }
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=W;j++)
        {
            printf("%d \t",c[i][j]);
        }
        printf("\n");
    }
    printf("Value of maximum profit entity for knapsack is: %d",c[n][W]);
    getch();
}

/*OUTPUT:
Enter the no. of entities 4
Enter the no. of entities of each items2
3
4
5
Enter respective costs of entities3
4
5
6
Enter the capacity of knapsack: 5
0       0       0       0       0       0
0       0       3       3       3       3
0       0       3       4       4       7
0       0       3       4       5       7
0       0       3       4       5       7
Value of maximum profit entity for knapsack is: 7   */

Kth SMALLEST NUMBER C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C program to find Kth smallest number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[20],i,j,temp,find;
clrscr();
printf("enter the no. of elemnets to be enterd");
scanf("%d",&n);
//entering data in array
 printf("enter the elements");
 for(i=0;i<n;i++)
 scanf("%d",&arr[i]);
//performing binary sort
  for(i=0;i<n;i++)
  {
  for(j=0;j<n-i;j++)
  {
  if(arr[j]>arr[j+1])
  {temp=arr[j];
  arr[j]=arr[j+1];
  arr[j+1]=temp;}}}
//input the term to be find
printf("enter the Kth minimum element to be found\n");
scanf("%d",&find);
//checking the existence of index
 if(find<0 || find >n)
 printf("index not exist");
else
//printing the output
printf("element is %d",arr[find-1]);
getch();
}
/*OUTPUT:
enter the no. of elemnts to be enterd 6
enter the elements
5  2  28  0   7  6
enter the Kth minimum element to be found
3
element is 5*/

BINARY SEARCH C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

Program: Program in C to perform binary search*/

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],num,count,mid,beg,end,term,found=0;
clrscr();
printf("enter the no. of elements to enter");
scanf("%d",&num);
printf("enter the numbers");
for(count=0;count<num;count++)
{scanf("%d",&arr[count]);}
printf("enter the term to be searched");
scanf("%d",&term);
beg=0;
end=num;
mid=(beg+end)/2;
while(beg<=end)
{
mid=(beg+end)/2;
if(term<arr[mid])
{end=mid-1;
}
if(term>arr[mid])
{
beg=mid+1;
}
if(term==arr[mid])
{
printf("element found at %d position in array",mid+1);
found=1;
break;
}
}
if(found==0)
{
printf("term not found");
}
getch();
}

/*OUTPUT:
enter the no. of elements to enter 6
enter the numbers
10
42
48
56
78
99
enter the term to be searched 48
element found at 3 position in array*/

SEQUENTIAL SEARCH C PROGRAM

ALGORITHM LAB PROGRAMS

/*CSEMATTER.BLOGSPOT.IN
SEQUENTIAL SEARCH PROGRAM

Program:Program in C to perform sequential search*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],num,count,term,found=0;
clrscr();
printf("enter the no. of elements to enter");
scanf("%d",&num);
printf("enter the numbers");
for(count=0;count<num;count++)
{scanf("%d",&arr[count]);}
printf("enter the term to be searched");
scanf("%d",&term);
for(count=0;count<num;count++)
{
if(arr[count]==term)
{printf("term found at %d position in array",count+1);
found=1;
break;
}
}
if(found==0)
printf("term no found");
getch();
}

/*enter the no. of elements to enter 5
enter the numbers
10
94
66
78
12
enter the term to be searched 78
term found at 4 position in array*/



Monday, 2 September 2013

PRIORITY BASED CPU SCHEDULING C PROGRAM



/*CSEMATTERBLOGSPOT.IN

Write a program in C to implement priority based CPU scheduling algo
calculate avg. waiting time & average turn around time*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
    int gd=DETECT,gm,i,j,k,n,b,count=0,arr[20],prio[20],bt[20],wt[20],tat[20];
    float avg_wt,avg_tat,sum_wt=0.0,sum_tat=0.0;
    initgraph(&gd,&gm,"C:\TC\BIN");
    printf("Enter the no. of processes  ");
    scanf("%d",&n);
    printf("Enter the burst time of processes \n");
    for(i=0;i<n;i++)
        scanf("%d",&bt[i]);
    printf("Enter priority of processes \n");
    for(i=0;i<n;i++)
    {    scanf("%d",&prio[i]);
        arr[i]=prio[i];}
    for(i=0;i<=(n-2);i++)
    {for(j=0;j<=((n-2)-i);j++)
      {   if(arr[j]>arr[j+1])
          {
              b=arr[j];
              arr[j]=arr[j+1];
              arr[j+1]=b;
          }} }
    wt[0]=0;
    tat[0]=0;
    for(i=0;i<n;i++)
    {   for(j=0;j<n;j++)
        {      if(arr[i]!=prio[j])
               count=count+1;
           else
               break;}
        wt[i+1]=bt[count]+wt[i];
        tat[i+1]=bt[count]+tat[i];
        count=0; }
    for(i=0;i<n;i++)
    sum_wt=sum_wt+wt[i];
    for(i=0;i<(n+1);i++)
    sum_tat=sum_tat+tat[i];
    avg_wt=(sum_wt/(float)n);
    avg_tat=(sum_tat/(float)n);
    printf("Process_no. Burst_Time Priority Waiting_Time Turn_Around_Time \n");
    for(i=0;i<n;i++)
    {
    printf("P%d\t\t%d\t%d\t\t%d\t\t%d\n",i,bt[i],prio[i],wt[i],tat[i+1]);}
    printf("Average waiting time is: %f \n",avg_wt);
    printf("Average turn around time is: %f",avg_tat);
    printf("\n\n \t GANTT CHART \n");
    for(i=0;i<n;i++)
        printf("P%d \t",i);
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d \t",wt[i]);
    }
    rectangle(2,300,270,320);
    getch();
    closegraph();
}
/* OUTPUT
Enter the no. of processes 5
Enter the burst time of processes
12 8 4 6 10
Enter priority of processes
5 1 2 4 3

Process_no.  Burst_Time  Priority  Waiting_Time  Turn_Around_Time
P1 8    1 0 8
P2 4    2 8 12
P4 10    3 12 22
P3 6    4 22 28
P0 12    5 28 40

Average waiting time is 14.00000
Average turn around time is 22.00000

GANTT CHART
 ||P1|| ||P2||  ||P4||   ||P3||   ||P0||
0      8       12 22 28  40*/

PREEMPTIVE SCHEDULING C PROGRAM


/* CSEMATTER.BLOGSPOT.IN
C Program for preemptive cpu scheduling*/
#include<stdio.h>
#include<conio.h>
int main()
{
    int min(int, int []);
    int temp,time,temp4,c,n,i,j,a1[20],wt[20],pr[20],bt[20],gc[20],arr_ti[20],tat[20],sum_bt=0,a=0,p=0,temp1,count;
    printf("Enter the no. of proceses");
    scanf("%d",&n);
    printf("Enter the burst time of processes");
    for(i=0; i<n; i++)
    {scanf("%d",&bt[i]);
      sum_bt=sum_bt+bt[i];
       a1[i]=bt[i];}
    for(i=0; i<n; i++)
    pr[i]=i+1;
    printf("Enter the arrival time");
    for(i=0; i<n; i++)
        scanf("%d",&arr_ti[i]);
    for(i=0; i<(n-2); i++)
    {  for(j=i; j<((n-2)-i); i++)
        {
            if(arr_ti[j]>arr_ti[j+1])
            {   temp=arr_ti[j];
                arr_ti[j]=arr_ti[j+1];
                arr_ti[j+1]=temp;
                temp=a1[j];
                a1[j]=a1[j+1];
                a1[j+1]=temp;
                temp=pr[j];
                pr[j]=pr[j+1];
                pr[j+1]=temp;
            }}}
    for(i=0; i<n; i++)
    {
        wt[i]=0;
        tat[i]=0;
    }
    for(i=0; i<=sum_bt; i++)
    {
        a++;
        if(a<5)
        {
            c=min(a,a1)+1;
            gc[i]=c;
        }
        else
        {
            c=min(5,a1)+1;
            gc[i]=c;
        }  }
    for(i=0; i<n; i++)
    {
        temp=i+1;
        time=0;
        temp1=0;
        for(j=0; j<sum_bt; j++)
        {time++;
            if(temp==gc[j])
            {
             if(temp1==0)
            {
            wt[i]=wt[i]+time-(i+1);
            temp1=j+2;
            }
            else
            {
            wt[i]=wt[i]+(j+1)-temp1;
            temp1=j+2;
            }}}}

    for(i=0; i<n; i++)
    {
        temp=0;
        for(j=0; j<sum_bt; j++)
        {
            if(i+1==gc[j])
            {
                if(temp==0)
                {
                    tat[i]=tat[i]+bt[i]+j-arr_ti[i];
                    temp=j+1;
                }
                else
                {
                    tat[i]=tat[i]+j-temp;
                    temp=j+1;
                }  }}}
     //printing the output

    printf("DATA IS AS FOLLOWS: \n");
    printf("Process\t Burst Time\t Arrival Time\t Waiting Time\t Turn Around Time\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t %d\t\t\t %d\t\t %d\t\t %d\n",pr[i],bt[i],arr_ti[i],wt[i],tat[i]);
    }
    printf("\n \n");
    printf("GANTT CHART: \n");
    c=gc[0];
    printf("  ||P%d\t",gc[0]);
    for(i=1;i<sum_bt;i++)
    {
        if(gc[i]!=c)
        {printf(" ||P%d|| ",gc[i]);
        c=gc[i];}
    }
    printf("\n0\t");
    c=gc[0];
    for(i=1;i<=sum_bt;i++)
    {    p=p+1;
         if(gc[i]!=c)
        {printf("%d \t",p);
        c=gc[i];
        }
        }
    getch();
}


int min(int a,int a1[20])
{
    int i,count=0;
    int temp=a1[0];
    if(a==1)
    { temp--;}
    else
    {
        for(i=0; i<a; i++)
        {
            if(temp>a1[i])
            {
                temp=a1[i];
                count=i;}}
        temp--;}
    if(temp==0)
    {temp=500;}
    a1[count]=temp;
    return(count);  /* returning the index of array whose value is minimum*/
}

/*OUTPUT:
Enter the no. of proceses 5
Enter the burst time of processes
5   2   2   3   6
Enter the arrival time
0   1   2   3   4
DATA IS AS FOLLOWS:
Process  Burst Time  ArrivalTime  Waiting Time Turn AroundTime
1           5           0           7               12
2           2           1           0               2
3           2           2           1               3
4           3           3           2               5
5           6           4           8               14
GANTT CHART:
  ||P1||    ||P2||  ||P3||  ||P4||  ||P1||  ||P5||
0        1        3        5       8      12      18*/

SJF CPU SCHEDULING C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
program in C to implement shortest job first CPU scheduling algo
calculate avg. waiting time & average turn around time*/

#include<stdio.h>
#include<conio.h>
int main()
{
int p[10],temp;
int tot=0,wt[10],pt[10],tat[10],i,j,n,temp1,tot1=0;
float avg=0,avg1=0;
printf("enter no of processes:");
scanf("%d",&n);
printf("enter process time");
for(i=0;i<n;i++)
{
scanf("%d",&pt[i]);
p[i]=i;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
temp=p[i];
p[i]=p[j];
p[j]=temp;

}
}
}
wt[0]=0;
for(i=1;i<=n;i++)
{
wt[i]=wt[i-1]+pt[i-1];

tot=tot+wt[i];
}
for(i=0;i<n;i++)
   {tat[i]=wt[i]+pt[i];
    tot1=tot1+tat[i];}
avg=(float)tot/n;
avg1=(float)tot1/n;
printf("p_no.\t P_time\t Waiting_Time\t  Turn_Around_Time\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t %d\n",p[i],pt[i],wt[i],tat[i]);
printf("avg waiting time=%f\n",avg);
printf("avg turn around time=%f",avg1);
printf("\n\n \t GANTT CHART \n");
    for(i=0;i<n;i++)
        printf("P%d \t",i);
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d \t",wt[i]);
    }
        getch();
}
/* OUTPUT
Enter the no. of processes 5
Enter the burst time of processes
12 8 4 6 10
P_no.  P_Time  Waiting_Time  Turn_Around_Time
2 4     0 4
3 6     4 10
1 8     10 18
4       10        18 28
0 12     28 40

Average waiting time is 20.000000
Average turn around time is 20.000000

GANTT CHART
P0 P1 P2 P3 P4
0 4 10 18 28*/

ROUND ROBIN OS C PROGRAM

/*CSEMATTERBLOG
PROGARM TO IMPLEMENT ROUND ROBIN ALGO*/
#include<stdio.h>
#include<conio.h>
int main()
{
    int bt[20],gc[20],wt[20],tat[20],bt1[20],st[20],ts,n,i,j,k,count=0,count1,sum_bt=0,tq;
    int swt=0,stat=0,temp,sq=0,c,p=0;
float awt=0.0,atat=0.0;
    printf("Enter the nuo. of processs");
    scanf("%d",&n);
    printf("Enter the burst time");
    for(i=0; i<n; i++)
        {scanf("%d",&bt[i]);
        bt1[i]=bt[i];
        st[i]=bt[i];}
    printf("Enter the time slice");
    scanf("%d",&ts);
    tq=ts;
    for(i=0; i<n; i++)
    sum_bt=sum_bt+bt[i];
        for(k=0; k<n; k++)
    {
       do
        {
            for(i=0; i<n; i++)
            {
                if(bt[i]>=ts)
                {
                    for(j=count; j<(count+ts); j++)
                        gc[j]=i+1;
                    count+=ts;
                    bt[i]=bt[i]-ts;
                }
                else
                {
                    for(j=count; j<=(count+bt[i]); j++)
                        gc[j]=i+1;
                    count+=bt[i];
                    bt[i]=0;
                }
            }
        }while(bt[k]!=0);
    }

 while(1)
{
       for(i=0,count=0;i<n;i++)
       {
       temp=tq;
       if(st[i]==0)
      {
        count++;
        continue;
       }
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt1[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no Burst time Wait time Turn around time\n");
for(i=0;i<n;i++)
printf("%d %d %d %d\n",i+1,bt1[i],wt[i],tat[i]);
printf("Avg wait time is %f Avg turn around time is %f",awt,atat);
 printf("\n \n");
    printf("GANTT CHART: \n");
    c=gc[0];
    printf("  ||P%d\t",gc[0]);
    for(i=1;i<sum_bt;i++)
    {
        if(gc[i]!=c)
        {printf(" ||P%d|| ",gc[i]);
        c=gc[i];}
    }
    printf("\n0\t");
    c=gc[0];
    for(i=1;i<=sum_bt;i++)
    {    p=p+1;
         if(gc[i]!=c)
        {printf("%d \t",p);
        c=gc[i];
        }}
   getch();
}
/*OUTPUT:
Enter the no. of proceses 5
Enter the burst time of processes
5   1   2   2   3
Enter the time slice 2
Process  Burst Time   Waiting Time Turn AroundTime
1           5           8               13
2           1           2               3
3           2           3               5
4           2           5               7
5           3           9               12
GANTT CHART:
  ||P1||    ||P2||  ||P3||  ||P4||  ||P5||  ||P1||  ||P5||  ||P1||
0        2        3        5      7       9       11      12      13*/

PRODUCER CONSUMER C PROGRAM

/*CSEMATTER.BLOGSPOT.IN
C Program to implement producer consumer problem*/
#include<stdio.h>
#include<conio.h>
int main()
{
int producer(int [],int,int);
int consumer(int [],int,int);
int buffer[20],max,n,a=0,a1=0,ch,cons=0;
 printf("\n\n\n\tEnter Stack Size :");
 scanf("%d",&max);
do
 {
   printf("\n\t\tCHOICES\n\t\t\n\t1.Producer\n\t2.Consumer\n\t3.Exit\nEnter your choice :\n ");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:
  {
   if(a==max & cons==0)
   {printf("STACK FULL...\n");
            break;}
     else if(cons==2)
     {
         printf("Consumer has not consumed all item yet");
         break;
    }
    else
    {a=producer(buffer,max,a);
     break;}
    }
    case 2:
  {if(a!=max)
  {printf("STACK NOT FULL YET...PRODUCER TURNS TO PRODUCE\n");
        break;}
      if(a==max)
     {a1=consumer(buffer,max,a1);
      if(a1==max)
      {a=0;
      cons=0;}
      else
      cons=2;}
     break;}
     case 3:
     break;
    }
 }
while(ch!=3);
getch();
}
 int producer(int buffer[20],int max,int a)
{
int i,n;
int counter=a;
printf("Enter the no. of items to be produced");
scanf("%d",&n);
for(i=a;i<a+n;i++)
{
printf("Enter the item to be produced");
scanf("%d",&buffer[i]);
counter=counter+1;
}
return(counter);
}
int consumer(int buffer[20],int max,int a1)
{
int i,n;
int counter1=a1;
printf("Enter the no. of items to be consumed");
scanf("%d",&n);
for(i=a1;i<a1+n;i++)
{
printf("Consumed item is: %d \n",buffer[i]);
buffer[i]=0;
counter1=counter1+1;
}
return(counter1);}
/*OUTPUT
     Enter Stack Size :5
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 2
STACK NOT FULL YET...PRODUCER TURNS TO PRODUCE
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 1
Enter the no. of items to be produced3
Enter the item to be produced1
Enter the item to be produced2
Enter the item to be produced3
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 2
STACK NOT FULL YET...PRODUCER TURNS TO PRODUCE
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 1
Enter the no. of items to be produced2
Enter the item to be produced4
Enter the item to be produced5
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 1
STACK FULL...
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 2
Enter the no. of items to be consumed3
Consumed item is: 1
Consumed item is: 2
Consumed item is: 3
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 1
Consumer has not consumed all item yet
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 2
Enter the no. of items to be consumed2
Consumed item is: 4
Consumed item is: 5
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
3 */

READER WRITER C PROGRAM

/*CSEMATTER.BLOGSPOT.IN
C Program to implement reader writer problem*/
#include<stdio.h>
#include<conio.h>
int ch,write=0,readcount=0;
int main()
{
void writerin();
void writerout();
void readerin();
void readerout();
 do
 {
   printf("\n\t\tCHOICES\n\t\t\n\t1 Writer in\n\t2 Writer out\n\t3.Reader in");
   printf("\n\t3..Reader out\n\t5.exit\nEnter your choice :\n ");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:
    {
     writerin();
    }
    case 2: 
    {
       writerout();   
     }
     case3:
 {
         readerin();
 }
     case4:
       {readerout();
       }
     case 5:
     break;
    }
 }
while(ch!=5);
getch();
}
void readerout()
{
if(write==1 && readcount>0)
      {
        readcount--;
 }
 else if(write==1 && readcount==0)
        {
      write=0;
         }
  else
 printf("no reader is reading");
}
void readerin()
{
if(write==0 && readcount==0)
      {
      readcount++;
      write=1;
      }
else if(write==1 && readcount>0)
{
readcount++;
}
else
{
printf("writer is writing");
}
}

void writerin()
{
if(readcount==0)
{
write=1;
printf("writer is writing\n");
}
else
{
printf("%d reader are reading..wait for finish\n",readcount);
}
}
void writerout()
{
if(write==1)
{
write=0;
printf("writer is exiting\n");
}
else
{
printf("no writer is writing\n");}
}
}

FIFO PAGE REPLACEMENT C PROGRAM


/* CSEMATTERBLOG

C PROGRAM TO IMPLEMENT FIFO PAGE REPLACEMENT ALGO*/
#include<stdio.h>
#include<conio.h>
void main()
{
int frame[20],pages[20],n,i,j,k,fr,count=0,avail;
clrscr();
printf("enter the no. of pages");
scanf("%d",&n);
printf("enter the page sequence");
for(i=0;i<n;i++)
scanf("%d",&pages[i]);
printf("enter the no. of frames");
scanf("%d",&fr);
for(i=0;i<fr;i++)
frame[i]=-1;
j=0;
printf("ref string \t page frame\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",pages[i]);
avail=0;
for(k=0;k<fr;k++)
if(frame[k]==pages[i])
avail=1;
if(avail==0)
{
frame[j]=pages[i];
j=(j+1)%fr;
count++;
for(k=0;k<fr;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("page fault is :%d",count);
getch();
}
/*OUTPUT
enter the no. of pages12
enter the page sequence2
3
4
5
2
3
6
2
3
4
5
6
enter the no. of frames3
ref string       page frame
2               2       -1      -1
3               2       3       -1
4               2       3       4
5               5       3       4
2               5       2       4
3               5       2       3
6               6       2       3
2
3
4               6       4       3
5               6       4       5
6
page fault is :9
*/

FCFS DISK SCHEDULING C PROGRAM

/*CSEMATTER.BLOGSPOT.IN

C PROGRAM TO IMPLEMENT FCFS DISK SCHEDULING  ALGO*/
#include<stdio.h>
#include<conio.h>
void main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float aver;
clrscr();
printf("enter the max range of disk");
scanf("%d",&max);
printf("enter the size of queue request");
scanf("%d",&n);
printf("enter the queue");
for(i=1;i<=n;i++)
{scanf("%d",&queue[i]);}
printf("enter the initial head position");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("move is from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("total seek time is%d\n",seek);
aver=seek/(float)n;
printf("avrage seek time is %f\n",aver);
getch();
}
/*OUTPUT:
enter the max range of disk180
enter the size of queue request8
enter the queue87
170
40
150
36
72
66
15
enter the initial head position60
move is from 60 to 87 with seek 27
move is from 87 to 170 with seek 83
move is from 170 to 40 with seek 130
move is from 40 to 150 with seek 110
move is from 150 to 36 with seek 114
move is from 36 to 72 with seek 36
move is from 72 to 66 with seek 6
move is from 66 to 15 with seek 51
total seek time is557
avrage seek time is 69.625000
*/