Showing posts with label LAB PROGRAM. Show all posts
Showing posts with label LAB PROGRAM. Show all posts

Friday, 17 October 2014

Fibonacci program in java

/*CSEMATTER.BLOGSPOT.IN
Program-fibonacci series in java*/

import java.util.Scanner;
class fib
{
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
int prev=0,next=1,sum,num,i;
System.out.println("ENTER THE NO.");
num=sc.nextInt();
System.out.println(""+prev);
System.out.println(""+next);
for(i=2;i<num;i++)
{
sum=prev+next;
prev=next;
next=sum;
System.out.println(""+sum);
}
}
}

Saturday, 11 October 2014

FACTORIAL JAVA PROGRAM

/*CSEMATTER.BLOGSPOT.IN
Program-java program to calculate factorial*/

import java.util.Scanner;
class factorial
{
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
int i,num,f=1;
System.out.println("ENTER THE NO. TO CALCULATE FACTORIAL");
num=sc.nextInt();
for(i=1;i<num;i++)
{
f=f*i;
}
System.out.println("factorial is"+f);

}
}

Friday, 10 October 2014

Concurrent checkpoint

/*CSEMATTER.BLOGSPOT.IN
Program-to implement concurrent checkpoint*/


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

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
int main()
{
    int pr[10],p[10],n,i,j,count,temp;

    printf("Enter the no. of processes: ");
    scanf("%d",&n);
    for(i=0; i<n; i++)
        p[i]=i+1;
    printf("Enter the priority of processes \n");
    for(i=0; i<n; i++)
    {
        printf("Priority of process P%d : \n",i+1);
        scanf("%d",&pr[i]);
    }

    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(pr[i]>pr[j])
            {
                temp=pr[i];
                pr[i]=pr[j];
                pr[j]=temp;

                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    for(i=0; i<n; i++)
    {
        count=0;
        for(j=0; j<n; j++)
        {
            if(i!=j)
                printf("P%d is sending request to P%d \n",p[i],p[j]);
             else
                 continue;
        }
        for(j=0; j<n; j++)
        {
            if(i!=j)
            {
                printf("P%d is sending ack to P%d \n",p[j],p[i]);
                count++;
            }
            else
                continue;
        }
        if(count==n-1)
        {
            printf("Ack from all the processes has been received \n");
            printf("P%d is forming a Consistent Checkpoint point \n",p[i]);
             delay(10000);
            for(j=0; j<n; j++)
            {
                if(i!=j)
                    printf("P%d sending release msg to P%d \n",p[i],p[j]);
                else
                    continue;
            }
            printf("\n \n");
        }
        else
            printf("Ack from all the processes not received hence Process P%d cann't form a consistent checkpoint \n \n",p[i]);
    }
    getch();

}

/* OUTPUT:

Enter the no. of processes: 3
Enter the no. of processes: 3
Enter the priority of processes
Priority of process P1 :
3
Priority of process P2 :
2
Priority of process P3 :
1
P3 is sending request to P2
P3 is sending request to P1
P2 is sending ack to P3
P1 is sending ack to P3
Ack from all the processes has been received
P3 is forming a Consistent Checkpoint point
P3 sending release msg to P2
P3 sending release msg to P1


P2 is sending request to P3
P2 is sending request to P1
P3 is sending ack to P2
P1 is sending ack to P2
Ack from all the processes has been received
P2 is forming a Consistent Checkpoint point
P2 sending release msg to P3
P2 sending release msg to P1


P1 is sending request to P3
P1 is sending request to P2
P3 is sending ack to P1
P2 is sending ack to P1
Ack from all the processes has been received
P1 is forming a Consistent Checkpoint point
P1 sending release msg to P3
P1 sending release msg to P2    */

Thursday, 9 October 2014

bayzentine program

/*CSEMATTER.BLOGSPOT.IN
Program-implement bayzentine program in c*/

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


int main()
{
int i,j,p[20][20],n,nf,cnt0=0,cnt1=1;
printf("Enter the no. of processes:");
scanf("%d",&n);
printf("Enter the process no. of faulty process:");
scanf("%d",&nf);
for(i=0;i<n;i++)
{
if(i==nf)
{
for(j=0;j<n;j++)
{
if(j!=i)
p[i][j]=random(2);
else
p[i][j]=NULL;}
}
else
{
for(j=0;j<n;j++)
{
if(j!=i)
p[i][j]=1;
else
p[i][j]=NULL;
}}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(p[i][j]==1)
++cnt1;
else if(p[i][j]==0)
++cnt0;
else
continue;
}
if(cnt1>cnt0)
continue;
else
{
printf("Processes are not synchronised \n \n");

}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",p[i][j]);
}
printf("\n");
}
getch();
}

/*OUTPUT:
Enter the no. of processes:3
Enter the process no. of faulty process:2
Processes are not synchronised

    1   1
1       1
1   0          */

Tuesday, 7 October 2014

MAEKAWA ALGORITHM DISTRIBUTED SYSTEM

/*CSEMATTER.BLOGSPOT..IN
Program name-Maekawa algorithm in distributed system*/

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

void process1();
void process2();
void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
int p[10],pr[10],n,i,j,count=0,ran_pr,temp,max_lmt,min_lmt;

int main()
{
    printf("Enter the no. of processses \n");
    scanf("%d",&n);
    for(i=0; i<n; i++)
        p[i]=i+1;
      do
       {
           ran_pr=random(n);
       }
       while(ran_pr!=0);
    ran_pr=3;
    printf("Control process is P%d \n",ran_pr);
    printf("Enter the priority of processes \n");
    for(i=0; i<n; i++)
    {
        printf("Priority of process P%d : \n",p[i]);
        scanf("%d",&pr[i]);
    }
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(pr[i]>pr[j])
            {
                temp=pr[i];
                pr[i]=pr[j];
                pr[j]=temp;

                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    for(i=0; i<n-1; i++)
    {
        if(p[i]<ran_pr)
        {
            max_lmt=ran_pr;
            process1();
        }
        else
        {
            min_lmt=ran_pr+1;
            process2();
        }
    }

    getch();
}

void process1()
{
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
            printf("Process P%d is sending request to P%d \n",p[i],p[j]);
        else
            continue;
    }
    printf("\n");
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
        {
            printf("Process P%d is acknowledging P%d \n",p[j],p[i]);
            count++;
        }
        else
            continue;
    }
    printf("\n");
    if(count==(ran_pr-1))
    {
        printf("Process P%d is entering CS \n",p[i]);
        delay(10000);
    }
    printf("\n");
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
            printf("Process P%d is sending reply to P%d \n",p[i],p[j]);
        else
            continue;
    }
}

void process2()
{
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
            printf("Process P%d is sending request to P%d \n",p[i],p[j]);
        else
            continue;
    }
    printf("\n");
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
        {
            printf("Process P%d is acknowledging P%d \n",p[j],p[i]);
            count++;
        }
        else
            continue;
    }
    printf("\n");

    if(count==(n-ran_pr-1))
    {
        printf("Process P%d is entering CS \n",p[i]);
        delay(10000);
    }
    printf("\n");
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
            printf("Process P%d is sending reply to P%d \n",p[i],p[j]);
        else
            continue;
    }
}

/*OUTPUT:
Enter the no. of processses 5
Control process is P3
Enter the priority of processes
Priority of process P1 :2
Priority of process P2 :1
Priority of process P3 :5
Priority of process P4 :3
Priority of process P5 :4

Process P2 is sending request to P1
Process P2 is sending request to P3
Process P1 is acknowledging P2
Process P3 is acknowledging P2
Process P2 is entering CS
Process P2 is sending reply to P1
Process P2 is sending reply to P3

Process P1 is sending request to P2
Process P1 is sending request to P3
Process P2 is acknowledging P1
Process P3 is acknowledging P1
Process P1 is entering CS
Process P1 is sending reply to P2
Process P1 is sending reply to P3

Process P4 is sending request to P5
Process P5 is acknowledging P4
Process P4 is entering CS
Process P4 is sending reply to P5

Process P5 is sending request to P4
Process P4 is acknowledging P5
Process P5 is entering CS
Process P5 is sending reply to P4

Process P3 is sending request to P1
Process P3 is sending request to P2
Process P1 is acknowledging P3
Process P2 is acknowledging P3
Process P5 is entering CS
Process P3 is sending reply to P1
Process P3 is sending reply to P2
*/

Monday, 2 June 2014

MIN FILTER IN MATLAB


/*CSEMATTER.BLOGSPOT.IN
PROGRAM FOR MIN FILTER IN MATLAB*/

A = imread('atul1.jpg');
A=imresize(A,[300,300]);
A = rgb2gray(A)
figure,imshow(A),title('ORIGINAL IMAGE');
B=zeros(size(A));
modifyA=padarray(A,[1 1]);

        x=[1:3];
        y=[1:3];
     
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
     
     
       window=reshape(modifyA(i+x-1,j+y-1),[],1);
               B(i,j)=min(window);

    end
end
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MIN FILTERING');

MEDIAN FILTER IN MATLAB

/*CSEMATTER.BLOGSPOT.IN
PROGRAM FOR MEDIAN FILTER IN MATLAB*/


A = imread('atul1.jpg');
A=imresize(A,[300,300]);
 A = rgb2gray(A)
figure,imshow(A),title('ORIGINAL IMAGE');
modifyA=zeros(size(A)+2);
B=zeros(size(A));
        for x=1:size(A,1)
            for y=1:size(A,2)
                modifyA(x+1,y+1)=A(x,y);
            end
        end
    
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        window=zeros(9,1);
        inc=1;
        for x=1:3
            for y=1:3
                window(inc)=modifyA(i+x-1,j+y-1);
                inc=inc+1;
            end
        end
      
        med=sort(window);
        B(i,j)=med(5);
      
    end
end
B=uint8(B);

figure,imshow(B),title('IMAGE AFTER MEDIAN FILTERING');

Wednesday, 28 May 2014

MEAN FILTER IN MATLAB PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM FOR MEAN FILTER IN MATLAB*/


A = imread('atul1.jpg');
A=imresize(A,[300,300]);
A = rgb2gray(A)
figure,imshow(A),title('ORIGINAL IMAGE');

modifyA=zeros(size(A)+2);
B=zeros(size(A));
        for x=1:size(A,1)
            for y=1:size(A,2)
                modifyA(x+1,y+1)=A(x,y);
            end
        end
   
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        window=zeros(9,1);
        inc=1;
        sum=0;
        for x=1:3
            for y=1:3
                window(inc)=modifyA(i+x-1,j+y-1);
     
                sum=sum+window(inc);
                inc=inc+1;
            end
        end
       sum=sum/9;
        B(i,j)=sum;
     
    end
end
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MEAN FILTERING');


MAX FILTER MATLAB PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM FOR MAX FILTER IN MATLAB*/


A = imread('atul1.jpg');
A=imresize(A,[300,300]);
 A = rgb2gray(A)
figure,imshow(A),title('ORIGINAL IMAGE');
B=zeros(size(A));
modifyA=padarray(A,[1 1]);

        x=[1:3];
        y=[1:3];
     
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
       window=reshape(modifyA(i+x-1,j+y-1),[],1);
               B(i,j)=max(window);
 
    end
end
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MAX FILTERING');

LINEAR FILTER IN MATLAB PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM FOR LINEAR FILTER IN MATLAB*/


A = imread('atul1.jpg');
A=imresize(A,[300,300]);
A = rgb2gray(A)
figure,imshow(A),title('ORIGINAL IMAGE');
modifyA=zeros(size(A)+2);
B=[1,2,4;1,2,4;1,2,4];
        for x=1:size(A,1)
            for y=1:size(A,2)
                modifyA(x+1,y+1)=A(x,y);
            end
        end
   
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        sum=0;
            for x=1:3
               for y=1:3
                sum=sum +((modifyA(i+x-1,j+y-1))*B(x,y));
            end
        end
       sum=sum/9;
        A(i,j)=sum;
     
    end
end

figure,imshow(A),title('IMAGE AFTER LINEAR FILTERING');

HISTOGRAM EQUALIZATION IN MATLAB

/*CSEMATTER.BLOGSPOT.IN
PROGRAM FOR HISTOGRAM EQUALIZATION IN MATLAB*/


im1=imread('atul1.jpg');
im=rgb2gray(im1);
im=imresize(im,[256,256]);
numofpixels=size(im,1)*size(im,2);
figure(1);
subplot(1,2,1);
imshow(im);
subplot(1,2,2);
imhist(im);
HIm=uint8(zeros(size(im,1),size(im,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
for i=1:size(im,1)
    for j=1:size(im,2)
        value=im(i,j);
        freq(value+1)=freq(value+1)+1;
        probf(value+1)=freq(value+1)/numofpixels;
   end
end
sum=0;
no_=255;
for i=1:size(probf)
   sum=sum+freq(i);
   cum(i)=sum;
   probc(i)=cum(i)/numofpixels;
   output(i)=round(probc(i)*no_);
end

for i=1:size(im,1)
    for j=1:size(im,2)
     HIm(i,j)=output(im(i,j)+1);
    end
end
figure(2);
subplot(1,2,1);
imshow(HIm);
subplot(1,2,2);
imhist(HIm);

OPERATIONS ON MATRIX IN MATLAB

/*CSEMATTER.BLOGSPOT.IN*/

DIGITAL IMAGE PROCESSING LAB
/*PROGRAM TO PERFORM ADDITION SUBTRACTION ETC. IN MATLAB*/

a=[1,2,3;4,5,6;7,8,9]
b=[2,4,6;1,3,5;3,4,1]
d=[1,2,3]
c=a+b;
disp('addition is ');
disp(c);
c=a-b;
disp('difference is ');
disp(c);
c=a*b;
disp('multiplication is');
disp(c);
disp('dot product');
c=a.*b;
disp(c);
disp('divison is');
c=a/d;
disp(c);
c=inv(a);
disp('inverse is');
disp(c);
disp('powerof matrix');
c=a^2;
disp(c);
disp('scalar divison');
c=a/2;
disp(c);



Tuesday, 25 March 2014

SHIFT REDUCE PARSER

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-SHIFT REDUCE PARSER*/


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

char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
void check();
int main()
{
printf("\n\t\t SHIFT REDUCE PARSER\n");
printf("\n GRAMMER\n");
printf("\n E->E+E\n ");
printf("\n E->E*E\n E->a");
printf("\n enter the input symbol:\t");
gets(ip_sym);
printf("\n\t stack implementation table");
printf("\n stack\t\t input symbol\t\t action");
printf("\n______\t\t ____________\t\t ______\n");
printf("\n $\t\t%s$\t\t\t--",ip_sym);
strcpy(act,"shift  ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0;i<=len-1;i++)
{
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
ip_ptr++;
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
check();
st_ptr++;
}
st_ptr++;
check();
}
void check()
{
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if((!strcmpi(temp2,"a")))
{
stack[st_ptr]='E';
if(!strcmpi(temp2,"a"))
printf("\n $%s\t\t%s$\t\t\tE->a",stack, ip_sym);
flag=1;
}
if((!strcmpi(temp2,"+"))||(strcmpi(temp2,"*")))
{
flag=1;
}
if((!strcmpi(stack,"E*E"))||(!strcmpi(stack,"E+E")))
{
strcpy(stack,"E");
st_ptr=0;
if(!strcmpi(stack,"E+E"))
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
if(!strcmpi(stack,"E*E"))
printf("\n $%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
flag=1;
}

if(!strcmpi(stack,"E")&& ip_ptr ==len)
{
 printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
getch();
 exit(0);
}
if(flag==0)
{
 printf("\n%s\t\t\t%s\t\t reject",stack,ip_sym);
 exit(0);
}
return;
}

THREE ADDRESS CODE

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO GENERATE THREE ADDRESS CODE*/

#include<stdio.h>
#include<string.h>
void pm();
void plus();
void div();
int i,ch,j,l,addr=100;
char ex[10],exp[10],exp1[10],exp2[10],id1[5],op[5],id2[5];
void main()
{
clrscr();
while(1)
{
printf("\n1.assignment\n2.arithmetic\n3.relational\n4.Exit\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the expression with assignment operator:");
scanf("%s",exp);
l=strlen(exp);
exp2[0]='\0';
i=0;
while(exp[i]!='=')
{
i++;
}
strncat(exp2,exp,i);
strrev(exp);
exp1[0]='\0';
strncat(exp1,exp,l-(i+1));
strrev(exp1);
printf("Three address code:\ntemp=%s\n%s=temp\n",exp1,exp2);
break;

case 2:
printf("\nEnter the expression with arithmetic operator:");
scanf("%s",ex);
strcpy(exp,ex);
l=strlen(exp);
exp1[0]='\0';

for(i=0;i<l;i++)
{
if(exp[i]=='+'||exp[i]=='-')
{
if(exp[i+2]=='/'||exp[i+2]=='*')
{
pm();
break;
}
else
{
plus();
break;
}
}
else if(exp[i]=='/'||exp[i]=='*')
{
div();
break;
}
}
break;

case 3:
printf("Enter the expression with relational operator");
scanf("%s%s%s",&id1,&op,&id2);
if(((strcmp(op,"<")==0)||(strcmp(op,">")==0)||(strcmp(op,"<=")==0)||(strcmp(op,">=")==0)||(strcmp(op,"==")==0)||(strcmp(op,"!=")==0))==0)
printf("Expression is error");
else
{
printf("\n%d\tif %s%s%s goto %d",addr,id1,op,id2,addr+3);
addr++;
printf("\n%d\t T:=0",addr);
addr++;
printf("\n%d\t goto %d",addr,addr+2);
addr++;
printf("\n%d\t T:=1",addr);
}
break;
case 4:
exit(0);
}
}
}
void pm()
{
strrev(exp);
j=l-i-1;
strncat(exp1,exp,j);
strrev(exp1);
printf("Three address code:\ntemp=%s\ntemp1=%c%ctemp\n",exp1,exp[j+1],exp[j]);
}
void div()
{
strncat(exp1,exp,i+2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,exp[i+2],exp[i+3]);
}
void plus()
{
strncat(exp1,exp,i+2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,exp[i+2],exp[i+3]);
}


/*OUTPUT
Example Generation of Three Address Project Output Result

1. assignment
2. arithmetic
3. relational
4. Exit
Enter the choice:1
Enter the expression with assignment operator:
a=b
Three address code:
temp=b
a=temp

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a+b-c
Three address code:
temp=a+b
temp1=temp-c

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a-b/c
Three address code:
temp=b/c
temp1=a-temp

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a*b-c
Three address code:
temp=a*b
temp1=temp-c

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:a/b*c
Three address code:
temp=a/b
temp1=temp*c
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:3
Enter the expression with relational operator
a
<=
b

100 if a<=b goto 103
101 T:=0
102 goto 104
103 T:=1

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:4

*/

Sunday, 23 March 2014

PREDICTIVE PARSER PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME-STACK IMPLEMENTATION OF PREDICTIVE PARSER*/

#include<ctype.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define non_term_count 5
#define term_count 6
#define rightHandMarker term_count-1
#define no_of_production 8

char nt[]={'E','A','T','B','F'},ter[]={'i','+','*','(',')','$'};
char arr[20][20][20]={
   {"TA","","","TA","",""},
    {"","+TA","","","@","@"},
    {"FB","","","FB","",""},
    {"","@","*FB","","@","@"},
    {"i","","","(E)","",""}
};
char ipstr[20];
char stack[40],prod[10];
int i=0,top=1,ia,ix;

struct predictive_table
{
 char prod[10];
}table[non_term_count][term_count]={"\0"};

struct
{
 int isNull;
 char term[10];
}first[non_term_count]={{0,"(i"},{1,"+"},{0,"(i"},{1,"*"},{0,"(i"}};
struct
{
 int isMarker;
 char term[10];
}follow[non_term_count]={{1,")"},{1,")"},{1,"+)"},{1,"+)"},{1,"+*)"}};
char terminals[term_count-1][2]={"i","+","*","(",")"};
char nonTerminals[non_term_count][3]={"E","A","T","B","F"};
char grammar[no_of_production][10]={"E T A","A + T A","A @","T F B","B * F B","B @","F ( E )","F i"};
char pgrammar[no_of_production][10]={"E->TA","A->+TA","A->@","T->FB","B->*FB","B->@","F->(E)","F->i"};

int matchTerminals(char term[])
{
 int i=-1;
 for(i=0; i<term_count-1; i++)
if(!strcmp(term,terminals[i]))
break;
 return i;
}

void copy_follow(int index,int loc)
{
 char term[2];
 int j,ind;
 if(follow[index].isMarker==1)
 {
  if(strlen(table[index][rightHandMarker].prod)==0)
   strcpy(table[index][rightHandMarker].prod,pgrammar[loc]);
  else
   printf("\nGiven Grammar is not LL(1)");
 }
 j=0;

 while(follow[index].term[j]!='\0')
 {
  term[0]=follow[index].term[j];
  term[1]='\0';
  ind = matchTerminals(term);
  if(strlen(table[index][ind].prod)==0)
   strcpy(table[index][ind].prod,pgrammar[loc]);
  else
   printf("\nGiven Grammar is not LL(1)");
  j++;
 }
}

int matchNonTerminals(char non_term[])
{
 int i=-1;
 for(i=0; i<non_term_count; i++)
if(!strcmp(non_term,nonTerminals[i]))
break;
 return i;
}



void show_table()
{
 int k,i,j;
 printf("\n\t\t\tPredictive Parser Table \n\n");
 printf("   |");
 for(i=0; i<term_count-1; i++)
  printf("    %s    |",terminals[i]);
 printf("    $    |");
 for(i=0; i<non_term_count; i++)
 {
  printf("\n----------------------------------------------------------------");
  printf("\n%s",nonTerminals[i]);
  for(k=strlen(nonTerminals[i]); k<3; k++)
   printf(" ");
  printf("|");
  for(j=0; j<term_count; j++)
  {
    printf(" %s",table[i][j].prod);
    for(k=strlen(table[i][j].prod); k<8; k++)
     printf(" ");
    printf("|");
  }
 }
}
void fun();
int main()
{
 int i,k,j,t,flag=0,indexTerm,indexNonTerm,index;
 char nextChar[10]="\0",nonterm[10]="\0",term[10]="\0";
 for(i=0; i<no_of_production; i++)
 {
  t=0;
  j=0;
  flag=0;
  while(grammar[i][j]!=' '&&grammar[i][j]!='\0')
   nonterm[t++]=grammar[i][j++];
  nonterm[t]='\0';
  indexNonTerm=matchNonTerminals(nonterm);
X:  j++;
  t=0;
  while(grammar[i][j]!=' '&&grammar[i][j]!='\0')
   nextChar[t++]=grammar[i][j++];
  nextChar[t]='\0';
  if(!strcmp(nextChar,"@"))
   copy_follow(indexNonTerm,i);
  else
  {
   index=matchTerminals(nextChar);
   if(index!=term_count-1)
   {
    if(strlen(table[indexNonTerm][index].prod)==0)
     strcpy(table[indexNonTerm][index].prod,pgrammar[i]);
    else
    {
      printf("\nGiven Grammar is not LL(1)");
      break;
    }
   }
   else
   {
    index=matchNonTerminals(nextChar);
    if(index!=non_term_count)
    {
     k=0;
     while(first[index].term[k]!='\0')
     {
       term[0]=first[index].term[k];
       term[1]='\0';
       indexTerm=matchTerminals(term);
       if(strlen(table[indexNonTerm][indexTerm].prod)==0)
strcpy(table[indexNonTerm][indexTerm].prod,pgrammar[i]);
       else
       {
printf("\n Given Grammar is not LL(1) ");
break;
       }
       k++;
     } //end while
     if(first[index].isNull==1)
     {
       flag=1;
       goto X;
     }
     else
      flag=0;
    }
   }
  }
  if(flag==1)
   copy_follow(indexNonTerm,i);
 }
 show_table();
 fun();
 getch();
}


void fun()
{

       void pop();
void push(char);
int resolve_nt(char);
int resolve_t(char);
void advance();
char a,x;
int len,k;
stack[0]='$';
stack[1]='E';
printf("\nenter the input string:\n");
scanf("%s",ipstr);
printf("I/p string\t\tStack\t\tProduction Used\n");
while(1)
{
a=ipstr[i];
x=stack[top];
for(k=i;ipstr[k]!='$';k++)
printf("%c",ipstr[k]);
printf("$\t\t");
if(x==a)
{
if(x=='$')
{
printf("input is accepted");
break;
}
else
{
pop();
advance();
}
}
else if(isupper(x))
{
ix=resolve_nt(x);
ia=resolve_t(a);
strcpy(prod,arr[ix][ia]);
len=strlen(prod);
pop();
for(k=1;k<=len;k++)
push(prod[len-k]);
if(stack[top]=='@')
pop();
}
else
{
printf("error");
break;
}
for(k=0;k<=top;k++)
printf("%c",stack[k]);
printf("\t\t\t%s\n",prod);
}
}

void push(char t)
{
top+=1;
stack[top]=t;
}
void pop()
{
top--;
}
void advance()
{
i++;
}
int resolve_nt(char t)
{
int k,index;
for(k=0;k<5;k++)
{
if(t==nt[k])
{
index=k;
break;
}
}
return index;
}

int resolve_t(char t)
{
int k,index;
for(k=0;k<6;k++)
{
if(t==ter[k])
{
index=k;
break;
}
}
return index;
}

/*OUTPUT


                        Predictive Parser Table

   |    i    |    +    |    *    |    (    |    )    |    $    |
----------------------------------------------------------------
E  | E->TA   |         |         | E->TA   |         |         |
----------------------------------------------------------------
A  |         | A->+TA  |         |         | A->@    | A->@    |
----------------------------------------------------------------
T  | T->FB   |         |         | T->FB   |         |         |
----------------------------------------------------------------
B  |         | B->@    | B->*FB  |         | B->@    | B->@    |
----------------------------------------------------------------
F  | F->i    |         |         | F->(E)  |         |         |
enter the input string:
i+i*i$
I/p string              Stack           Production Used
i+i*i$          $AT                     TA
i+i*i$          $ABF                    FB
i+i*i$          $ABi                    i
i+i*i$          $AB                     i
+i*i$           $A                      @
+i*i$           $AT+                    +TA
+i*i$           $AT                     +TA
i*i$            $ABF                    FB
i*i$            $ABi                    i
i*i$            $AB                     i
*i$             $ABF*                   *FB
*i$             $ABF                    *FB
i$              $ABi                    i
i$              $AB                     i
$               $A                      @
$               $                       @
$               input is accepted

*/

Wednesday, 4 September 2013

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*/