Thursday, 9 November 2017

Write a program using LEX to recognize and count the number of identifiers in a given input file.

Source Code :

%{

#include<stdio.h>

int count=0;

%}

op [+-*/]

letter [a-zA-Z]

digitt [0-9]

id {letter}*|({letter}{digitt})+

notid ({digitt}{letter})+

%%

[\t\n]+

("int")|("float")|("char")|("case")|("default")| ("if")|("for")|("printf")|("scanf") {printf("%s is a keyword\n", yytext);}

{id} {printf("%s is an identifier\n", yytext); count++;}

{notid} {printf("%s is not an identifier\n", yytext);}

%%

int main()

{

FILE *fp;

char file[10];

printf("\nEnter the filename: ");

scanf("%s", file);

fp=fopen(file,"r");

yyin=fp;

yylex();

printf("Total identifiers are: %d\n", count);

return 0;

}

Output:

$cat > input

int

float

78f

90gh

a

d

are case

default

printf

scanf

$lex p3.l

$cc lex.yy.c –ll

$./a.out

Enter the filename: input

int is a keyword

float is a keyword

78f is not an identifier

90g is not an identifier

h is an identifier

a is an identifier

d is an identifier

are is an identifier

case is a keyword

default is a keyword

printf is a keyword

scanf is a keyword

total identifiers are: 4

Saturday, 4 November 2017

C Program to implement Dijkstra’s Algorithm.

#include <stdio.h>
#include <conio.h>
#define infinity 999

void dij(int n,int v,int cost[10][10],int dist[])
{
 int i,u,count,w,flag[10],min;
 for(i=1;i<=n;i++)
  flag[i]=0,dist[i]=cost[v][i];
 count=2;
 while(count<=n)
 {
  min=99;
  for(w=1;w<=n;w++)
   if(dist[w]<min && !flag[w])
    min=dist[w],u=w;
  flag[u]=1;
  count++;
  for(w=1;w<=n;w++)
   if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
    dist[w]=dist[u]+cost[u][w];
 }
}
void main()
{
 int n,v,i,j,cost[10][10],dist[10];
 clrscr();
 printf("\n Enter the number of nodes:");
 scanf("%d",&n);
 printf("\n Enter the cost matrix:\n");
 for(i=1;i<=n;i++)
  for(j=1;j<=n;j++)
  {
   scanf("%d",&cost[i][j]);
   if(cost[i][j]==0)
    cost[i][j]=infinity;
  }
 printf("\n Enter the source matrix:");
 scanf("%d",&v);
 dij(n,v,cost,dist);
 printf("\n Shortest path:\n");
 for(i=1;i<=n;i++)
  if(i!=v)
   printf("%d->%d,cost=%d\n",v,i,dist[i]);
 getch();
                           }

Input/Output:


C Program to implement Knapsack using Dynamic programming.

#include<stdio.h>
#include<conio.h>
Int sum=0;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
void knapsack(int m,int n,int w[],int p[])
{
int v[100][200],x[10],i,j;
for(i=0;i<=m;i++)
v[0][i]=0;
for(i=1;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(j>=w[i])
v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
else
v[i][j]=v[i-1][j];
}
}
for(i=1;i<=n;i++)
x[i]=0;
i=n;
j=m;
while(i>0 && j>0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
}
i--;
}
printf("\nTHE OPTIMAL SET OF WEIGHTS IS:\n");
for(i=1;i<=n;i++)
{
if(x[i]==1)
{
printf("X%d=1\t",i);
sum=sum+p[i];
}
Else
printf("X%d=0\t",i);
}
printf("\nTotal profit = %d",sum);
}
void main()
{
int w[10],p[10],i,m,n;
printf("\t0/1 KNAPSACK PROBLEM\n\n");
printf("ENTER THE NUMBER OF ITEMS: ");
scanf("%d",&n);
printf("ENTER THE WEIGHTS OF THE ITEMS:\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("ENTER THE PROFITS OF THE ITEMS:\n");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("ENTER THE CAPACITY OF KNAPSACK: ");
scanf("%d",&m);
knapsack(m,n,w,p);
getch();
           }

Input/Output:


C Program to implement Knapsack using Greedy technique.

#include<stdio.h>
#include<conio.h>
void knapsack(int n,float profit[],float weight[],float capacity){
 float x[20],total,tp=0;
 int i,j;
 total=capacity;
 printf("\nEntered Items are :\n ");
 printf(" Value\t\t\tProfit:");
 printf("\n---------------------------\n");
 for(i=0;i<n;i++){
     printf("%f\t\t\t%f\n",profit[i],weight[i]);
   }
 for(i=0;i<n;i++)
     x[i]=0.0;
 for(i=0;i<n;i++){
   if(weight[i]>total)
      break;
   else
       {
           x[i]=1.0;
           tp+=profit[i];
           total-=weight[i];
       }
    }
   if(i<n){
      x[i]=total/weight[i];
   }
   tp+=x[i]*profit[i];
   printf("\n\nProfit Vector is : ");
   for(i=0;i<n;i++)
     printf("%f\t",x[i]);
   printf("\n");
   printf("\nTotal Profit : %f",tp);
}
int main(){
  float weight[20],profit[20],ratio[20],capacity,temp;
  int n,i,j;
  printf("\nEnter no of items : ");
  scanf("%d",&n);
  printf("\nEnter Capacity : ");
  scanf("%f",&capacity);
  printf("\nEnter Weight nd Profit : ");
  for(i=0;i<n;i++){
      printf("\nEnter Weight nd Profit for item[%d] : ",i);
      scanf("%f %f",&weight[i],&profit[i]);
  }
  for(i=0;i<n;i++)
    ratio[i]=profit[i]/weight[i];
  for(i=0;i<n;i++)
     for(j=i+1;j<n;j++){
       if(ratio[i]<ratio[j]){
        temp=ratio[j];
        ratio[j]=ratio[i];
        ratio[i]=temp;
        temp=weight[j];
        weight[j]=weight[i];
        weight[i]=temp;
        temp=profit[j];
        profit[j]=profit[i];
        profit[i]=temp;
       }
       knapsack(n,profit,weight,capacity);
       getch();
       return(0);
     }
                            }

Input/Output:


C Program to implement Strassen's Matrix Multiplication by Divide and Conquer

     #include<stdio.h>
      int main(){
.        int a[2][2], b[2][2], c[2][2], i, j;
        int m1, m2, m3, m4 , m5, m6, m7;
      
        printf("Enter the 4 elements of first matrix: ");
        for(i = 0;i < 2; i++)
            for(j = 0;j < 2; j++)
                 scanf("%d", &a[i][j]);
   
   printf("Enter the 4 elements of second matrix: ");
   for(i = 0; i < 2; i++)
        for(j = 0;j < 2; j++)
           scanf("%d", &b[i][j]);
  
    printf("\nThe first matrix is\n");
    for(i = 0; i < 2; i++){
      printf("\n");
      for(j = 0; j < 2; j++)
   }
   
    printf("\nThe second matrix is\n");
    for(i = 0;i < 2; i++){
     printf("\n");
        for(j = 0;j < 2; j++)
          printf("%d\t", b[i][j]);
  }
   
   m1= (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);
  m2= (a[1][0] + a[1][1]) * b[0][0];
    m3= a[0][0] * (b[0][1] - b[1][1]);
    m4= a[1][1] * (b[1][0] - b[0][0]);
   m5= (a[0][0] + a[0][1]) * b[1][1];
  m6= (a[1][0] - a[0][0]) * (b[0][0]+b[0][1]);
    m7= (a[0][1] - a[1][1]) * (b[1][0]+b[1][1]);
   
 c[0][0] = m1 + m4- m5 + m7;
  c[0][1] = m3 + m5;
   c[1][0] = m2 + m4;
  c[1][1] = m1 - m2 + m3 + m6;
   
  printf("\nAfter multiplication using Strassen's algorithm \n");
    for(i = 0; i < 2 ; i++){
        printf("\n");
        for(j = 0;j < 2; j++)
            printf("%d\t", c[i][j]);
   }
  return 0;
 }

Input/Output: