The Programming Project

Thursday, August 1, 2013

Uniform Symbol Table

A program to create a Terminal Table on the basis of the subsets of tokens
{ "if","for","while","int","float","char","+","-","*","/","=","<",">","{",",","(",")",";","?"} . Given a C statement the system program should create and display a uniform table that consists of full list of valid tokens. There must be two entries for each token in the Symbol Table, namely class and index (that is a pointer to a specific location of an appropriate table where the description of the token can be found. The index can be found to any one of the following tables: Identifier table, Literal Table and Terminal Table depending on the type of token. Each  of these three table would have its own relevant fields.

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int tokencount(char *str);
void  classification(char iden[],int *key,int *sym,int *ope,int *lit,int *ide,int *pkey,int *psym,int *pope);
void print_table(int,int,int);
#define TRUE 1
#define FALSE 0
typedef struct
    {
    int indx;
    char tok[20];
    int pos;
    }Terminal_table;
typedef struct
    {
    int indx;
    char tok[20];
    char t_nme[20];
    int pos;
    }Uniform_sym_table;
typedef struct
    {
    int indx;
    char lit[20];
    int pos;
    }Literal;
typedef struct
    {
    int indx;
    char iden[20];
    int pos;
    }Identifier;
Terminal_table *TT;
Uniform_sym_table *UST;
Literal *LT;
Identifier *IDE;
int main(void)
{
char *keyword1[]={ "if","for","while","int","float","char","+","-","*","/","=","<",">",",","(",")",";","?"};
char *str,ar[80],dig[2],sp[20];
char *token;
int FLAG=FALSE,count=0,n,len,i=0,tc=0,p=0,q=0,r=0,s=0,t=0,j=0;
int key=FALSE,sym=FALSE,ope=FALSE,lit=FALSE,ide=FALSE,lit_count=0,FLAG1=FALSE,iden_count=0;
int pkey,psym,pope;
printf("\n Enter the maximum length of the statement:");
scanf(" %d", &n);
str=(char *)malloc( (n+1)*sizeof(char));
fflush(stdin);
printf("\n Enter the C statement:");
scanf("%[^\n]",str);
len=strlen(str);
if(len>n)
{
printf("\n Length of the statement is greater than the input length:");
exit(1);
}
token=str;
tc=tokencount(str);
UST=(Uniform_sym_table *)malloc(tc*sizeof(Uniform_sym_table));
TT=(Terminal_table *)malloc(20*sizeof(Terminal_table));
LT=(Literal *)malloc(tc*sizeof(Literal));
IDE=(Identifier *)malloc(tc*sizeof(Identifier));
for(i=0;i<18;i++)
    {
    strcpy(TT[i].tok,keyword1[i]);
    TT[i].indx=i+1;
    TT[i].pos=i+1;
    }
tc=0;
while(1)
         {
         while(*token!= ' ')
         {
         if(*token== '\0')
            {
            FLAG=TRUE;
            break;
            }
    ar[count++]=*token;
    token++;
    }
    ar[count]='\0';
    classification(ar,&key,&sym,&ope,&lit,&ide,&pkey,&psym,&pope);
   if(key)
        {
        UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
        }
    if(sym)
        {
        UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
      }
    if(ope)
        {
      UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
        }
    if(lit)
        {
        UST[tc].indx=tc+1;
        for(i=0;i<p;i++)
            {
            if((strcmp(LT[i].lit,ar))==0)
                {
                FLAG1=TRUE;
                s=i;
                break;
                }
            }
        if(FLAG1==0)
            {
            strcpy(UST[tc].tok,ar);
            strcpy(UST[tc].t_nme,"LTR");
            LT[p].indx=p+1;
            strcpy(LT[p].lit,ar);
            LT[p].pos=p+1;
            UST[tc].pos=LT[p].pos;
            p++;
            lit_count++;
            }
        else
        {
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"LTR");
        UST[tc].pos=LT[s].pos;
        }
        tc++;
        }
    if(ide)
        {
        
      UST[tc].indx=tc+1;
        for(i=0;i<q;i++)
            {
            if((strcmp(IDE[i].iden,ar))==0)
                {
                FLAG1=TRUE;
                r=i;
                break;
                }
            }
        if(FLAG1==0)
            {
            strcpy(UST[tc].tok,ar);
            strcpy(UST[tc].t_nme,"IDE");
            IDE[q].indx=q+1;
            strcpy(IDE[q].iden,ar);
            IDE[q].pos=q+1;
            UST[tc].pos=IDE[q].pos;
            q++;
            iden_count++;
            }
        else
        {
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"IDE");
        UST[tc].pos=IDE[r].pos;
        }
        tc++;

        }
/*    if(!key && !sym && !ope && !lit && !ide)
        {
        printf("\n Invalid tokens: %s ", ar);
      }*/
    if(FLAG==TRUE)
         break;
    while(*token==' ')
    {
         token++;
         if(*token=='\0')
           {
            FLAG=TRUE;
            break;
         }
      }
    if(FLAG==TRUE)
        break;
    count=0;
   FLAG1=FALSE;
    key=FALSE;
    sym=FALSE;
    ope=FALSE;
    lit=FALSE;
    ide=FALSE;
      }
print_table(tc,lit_count,iden_count);
printf("\n Enter a key");
getche();
return 0;
}
void print_table(int tc, int lit_count, int iden_count)
{
int i;
printf("\n");
printf("\n\t Uniform Symbol Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tClass\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<tc;i++)
    {
    printf("\n%d\t\t%s\t\t%s\t\t%d",UST[i].indx,UST[i].tok,UST[i].t_nme,UST[i].pos);
    }
printf("\n\tTerminal Table\n");
printf("---------------------------------");
printf("\nIndex\tKeywords\tPosition\n");
printf("---------------------------------");
for(i=0;i<18;i++)
{
printf("\n#%d\t%s\t %d",TT[i].indx,TT[i].tok,TT[i].pos);
}
printf("\n\n\n");
printf("\n\t Literal Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<lit_count;i++)
    {
    printf("\n%d\t\t%s\t\t%d",LT[i].indx,LT[i].lit,LT[i].pos);
    }
printf("\n\n\n");
printf("\n\t Identifier Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<iden_count;i++)
    {
    printf("\n%d\t\t%s\t\t%d",IDE[i].indx,IDE[i].iden,IDE[i].pos);
    }
return;
}

int tokencount(char *str)
{
char *token,ar[30];
int tc=0,count=0,FLAG=FALSE;
token=str;
while(1)
       {
       while(*token!= ' ')
       {
       if(*token== '\0')
         {
         FLAG=TRUE;
         break;
         }
    ar[count++]=*token;
    token++;
    }
    ar[count]='\0';
    tc++;
    if(FLAG==TRUE)
       break;
         while(*token== ' ')
       {
       token++;
       if(*token== '\0')
           {
            FLAG=TRUE;
              break;
        }
            
    }
    if(FLAG==TRUE)
    break;
    count=0;
    }
    return (tc);
}
void classification( char ar[],int *key,int *sym,int *ope,int *lit,int *ide,int *pkey,int *psym,int *pope)
{
int  FLAG=FALSE,len,count=0,flag=TRUE,numb=0,d,t=0,m=0;
char *keyword[]={ "if","for","while","int","float","char"};
char *operatr[]={ "+","-","*","/","=","<",">"};
char *symbols[]={ ",","(",")",";","?"};
int i=0;
len=strlen(ar);
for(i=0;i<len;i++)
    if(ar[i]==' ')
    t++;
if(t==len)
    return;
if(len>8)
FLAG=FALSE;
for(i=0;i<=5;i++)
{
    if(strcmp(keyword[i],ar)==0)
    {
  //      printf("\n%s\tis a Keyword:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *key=TRUE;
      *pkey=i;
    }

}
i=0;
for(i=0;i<=6;i++)
    {
    if(strcmp(operatr[i],ar)==0)
        {
     //    printf("\n%s\tis a Operator:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *ope=TRUE;
      *pope=i;
        }
    }
i=0;
for(i=0;i<5;i++)
    {
    if(strcmp(symbols[i],ar)==0)
        {
     //    printf("\n%s\tis a Symbol:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *sym=TRUE;
      *psym=i;
        }
    }
if ( ((65 <= ar[0] && ar[0] <= 93 )|| ( 97 <= ar[0] && ar[0] <= 122)) && len <= 8 && FLAG==FALSE)
    {
  //    printf("\n%s\tis an Identifier:",ar);
   for(i=1;i<strlen(ar);i++)
        {
        if( (65 <= ar[i] && ar[i] <= 93 ) ||  ( 97 <= ar[i] && ar[i] <= 122)  || ( 48 <= ar[i] && ar[i] <= 57) || ar[i]==95 )
            {
             m++;
            }

        }
    if(m==strlen(ar)-1)
    {
  //    printf("\n%s\tis an Identifier:\n",ar);
    FLAG=TRUE;
    flag=FALSE;
    *ide=TRUE;
    }
  //    FLAG=TRUE;
  //    flag=FALSE;
  //    *ide=TRUE;
    }
for(i=0;i<len;i++)
    {
    if(48 <= ar[i] && ar[i] <=57 )
    count++;
    else
        continue;
    }
if(count==len && flag==TRUE && FLAG==FALSE)

    {
    numb=atoi(ar);
    if(numb==0 || numb >999)
     {printf("\n Invalid token: %s ", ar);}//printf("%s\tis not a valid Constant:",ar);
    else
    {*lit=TRUE;}//printf("\n%s\tis a Constant:",ar);
    FLAG=TRUE;

}
i=0;
if( FLAG==FALSE)
    {
    if(ar[0]=='-' || (48 <= ar[0] && ar[0] <=57))
    {printf("\n Invalid token: %s ", ar);}//printf("%s\tis not a valid Constant ",ar);
    else
    {printf("\n Invalid token: %s ", ar);}//printf("\n%s\tis an INVALID TOKEN:",ar);
    }
//printf("\n");
return;
}

Sunday, July 21, 2013

Matrix Inversion

To find the inverse of the matrix if it exists by elementary row operations


#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main()
{
double *coeff[15],*I[15],temp,temp1,str,str1;
int i,j,ROW,k,nz,p,q;
printf("\n Enter the value of row OR coloumn:");
scanf("%d",&ROW);
for(i=0;i<ROW;i++)
    {
    coeff[i]=(double *)malloc(ROW*sizeof(double));
    I[i]=(double *)malloc(ROW*sizeof(double));
    }
printf("\n Enter the elements of the matrix(A)");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
          scanf("%lf",(coeff[i]+j));
    }
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
          {
          if(i==j)
          I[i][j]=1.0;
          else
          I[i][j]=0.0;
          }
    }
printf("\n The Matrix A is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
        {
        printf("%lf ",*(coeff[i]+j));
        }
    printf("\n");
        }

for(i=0;i<ROW-1;i++)
    {
    nz=i;
    if(*(coeff[i]+i)==0.0)
        {   
        do
             {
              nz++;
              if(nz>=ROW)
                  {
                  printf("\n Not invertible:\n");
                  exit(1);
                  }
              }while(*(coeff[nz]+i)==0.0);
             //printf("\nnz=%d",nz);
    for(k=0;k<ROW;k++)
        {
        temp=*(coeff[nz]+k); temp1=*(I[nz]+k);
        *(coeff[nz]+k)=*(coeff[i]+k); *(I[nz]+k)=*(I[i]+k);
        *(coeff[i]+k)=temp; *(I[i]+k)=temp1;
            }
             }
           /* printf("\n \n The Augumented (A|b) matrix after Row operations is:\n");
        for(p=0;p<ROW;p++)
            {
            for(q=0;q<ROW;q++)
                printf("%lf ",*(coeff[p]+q));
            printf("\n");
                }*/
        for(k=0;k<ROW-i-1;k++)
            {
            temp=0.0;
            temp1=0.0;
                  str=coeff[i+1+k][i];
                  //printf("\n str=%lf",str);
                  //printf("\n pivot=%lf",coeff[i][i]);
                  for(j=0;j<ROW;j++)
                {
                       temp=coeff[i+k+1][j]; temp1=I[i+k+1][j];
                       //printf(" temp=%lf ",temp);
                       if(str==0.0)
                           continue;
                //temp=temp-((str/coeff[i][i])*coeff[i][j]); temp1=temp1-((str/coeff[i][i])*I[i][j]);
                temp=temp/str; temp1=temp1/str;
                //temp=temp-((temp/str)*coeff[i][i]); temp1=temp1-((str/coeff[i][i])*I[i][j]);
                temp=temp*coeff[i][i]; temp1=temp1*coeff[i][i];
                temp=temp-coeff[i][j]; temp1=temp1-I[i][j];
                //printf(" [temp=%lf] ",temp);
                 coeff[i+k+1][j]=temp; I[i+k+1][j]=temp1;
                          }
                   }
        
            
    }
/*printf("\n \n The Augumented (A|b) matrix after Row operations is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
        printf("%lf ",*(coeff[i]+j));
    printf("\n");
        }
printf("\n \n The Inverse of the matrix is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
        printf("%lf ",*(I[i]+j));
    printf("\n");
        }*/
for(i=ROW-1;i>0;i--)
    {
    temp=0.0;
    temp1=0.0;
   
    for(k=i-1;k>=0;k--)
        {   
            str=coeff[k][i];
            //printf("\n str1=%lf",str1);
            //printf("\n str=%lf\n",str);
            for(j=0;j<ROW;j++)
            {
            if(str==0.0)
                continue;
           
            temp=coeff[k][j]/str; temp1=I[k][j]/str;
            //printf(" temp1=%lf",temp1);
            temp= temp*coeff[i][i]; temp1=temp1*coeff[i][i];
            //printf("\n temp=%lf",temp);
            temp=temp-coeff[i][j];  temp1=temp1-I[i][j];
            coeff[k][j]=temp;       I[k][j]=temp1;
            }
        }
       
    }
/*printf("\n \n The Augumented (A|b) matrix after Row operations is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
        {
        //if(i==j)
        //    printf("%lf ",*(coeff[i]+j)/coeff[i][i]);
        //else
        printf("%lf ",*(coeff[i]+j));
        }
   
    printf("\n");
        }*/
printf("\n \n The Inverse of the matrix is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
        {
        if(coeff[i][i]==0.0)
            printf("%lf ",*(I[i]+j));
        else
        printf("%lf ",*(I[i]+j)/coeff[i][i]);
        }
    printf("\n");
        }

return 0;
}

Saturday, July 20, 2013

Minimal Spanning Tree~Prims Alogorithm

Minimal Spanning Tree by Prims Alogorithm


#include<stdio.h>
int main()
{
float graph[11][11],lcost[11],sum=0.0,min;
int close[11],i,j,k,n;
printf("\n Enter the number of vertices:\n");
do
   {
   scanf("%d",&n);
   if(n<=10)
       break;
   printf("\n Number of vertices cannot exceed 10:");
    }while(n>10);
for(i=1;i<=n;i++)
    graph[i][i]=0.0;
printf("\n Enter 999 if there is no edge between selected vertices:\n");
printf("\n Enter the weighted matrix:\n");
for(i=1;i<n;i++)
    for(j=i+1;j<=n;j++)
        {
        printf("\n Enter the cost of edge %d<--->%d : ",i,j);
        scanf("%f",&graph[i][j]);
        graph[j][i]=graph[i][j];
        }
printf("\nThe weighted matrix is:\n");
for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
        {
        printf("%f\t  ",graph[i][j]);
        }
    printf("\n");
    }
for(i=2;i<=n;i++)
    {
    lcost[i]=graph[1][i];
    close[i]=1;
    }
printf("\n Minimum cost spanning tree by Prim's Algorithm are:\n");
    for(i=2;i<=n;i++)
    {
    min=lcost[2];
    k=2;
    for(j=3;j<=n;j++)
        if(lcost[j]<min)
        {
            min=lcost[j];
            k=j;
        }
    printf("Min=%f\n",min);
    sum +=min;
    printf(" %d<--->%d\n",k,close[k]);
    lcost[k]=1000.0;
    for(j=2;j<=n;j++)
        if((graph[k][j]<lcost[j]) && (lcost[j]<1000.0))
            {
            lcost[j]=graph[k][j];
            close[j]=k;
            }
    }
printf("\n Minimum cost is: %f\n",sum);
return 0;
}
   



Minimal Spanning Tree~Krushkal Algorithm

Minimal Spanning Tree by Krushkal Algorithm


#include<stdio.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
typedef struct krushkal
    {
    int svertex;
    int evertex;
    float ecost;
    }Graph;
void sort_edges(Graph *G,int);
void check_cycle(Graph *G,int,int);
int main()
{
int n,i,j,edge_count=1;
float temp;
Graph *G;
printf("\n Enter the number of vertices:\n");
scanf("%d",&n);
G=(Graph *)malloc((n*n+1)*sizeof(Graph));
printf("\n Enter the cost of the edges :\n");
printf("\n Enter cost as 999 if there is no edge between displayed vertices :\n");
for(i=1;i<=n-1;i++)
    {
    for(j=i+1;j<=n;j++)
        {
        printf("\n Enter the cost of edge %d<--->%d : ",i,j);
        scanf("%f",&temp);
        G[edge_count].ecost=temp;
        G[edge_count].svertex=i;
        G[edge_count].evertex=j;
        edge_count++;
        }
    }
edge_count--;
sort_edges(G,edge_count);
int t=0;
printf("\n Edges is ascending order is:\n");
for(i=1;i<=edge_count;i++)
        {
        if(G[i].ecost==999.0)
            t++;
        }
for(i=1;i<=edge_count-t;i++)
        {
        printf("\n Edge %d<--->%d Cost %f ",G[i].svertex,G[i].evertex,G[i].ecost);
        }
check_cycle(G,edge_count-t,n);
return 0;
}
void check_cycle(Graph *G,int edge_count, int vertices)
{
   
 int edge_ST=1,Partition[30][20],i=1,j=1,k,V1=FALSE,V2=FALSE,pelement[30],p_count=1,m,l=1,wp1,wp2,y,z,p;
 float min_cost=0.0;
 if(edge_count<vertices-1)
     {
     printf("\n No Spanning tree is possible:\n");
     return;
     }
 pelement[1]=1;
 Partition[j][pelement[1]]=G[i].svertex;
 pelement[1]++;
 Partition[j][pelement[1]]=G[i].evertex;
 printf("\n\n Edges of Minimal Spanning Tree by Krushkal Algorithm:\n");
 printf("\n%d<--->%d  ",G[i].svertex,G[i].evertex);
 min_cost +=G[i].ecost;
 i++;
 while(1)
    {
    V1=FALSE,V2=FALSE;
    for(m=1;m<=p_count;m++)
        for(k=1;k<=pelement[m];k++)
        {
        if(G[i].svertex==Partition[m][k])
            {
            V1=TRUE;
            wp1=m;
            }
        }
    for(m=1;m<=p_count;m++)
        for(k=1;k<=pelement[m];k++)
        {
        if(G[i].evertex==Partition[m][k])
            {
            V2=TRUE;
            wp2=m;
            }
        }
    if((V1==TRUE && V2==FALSE) || (V1==FALSE && V2==TRUE) ||(V1==FALSE && V2==FALSE)||((V1==TRUE && V2==TRUE) && wp1!=wp2))
        {
        printf("\n%d<--->%d  ",G[i].svertex,G[i].evertex);
         min_cost +=G[i].ecost;
        edge_ST++;
        if(V1==FALSE && V2==FALSE)
            {
            l=1;
            p_count++;
            Partition[p_count][l]=G[i].svertex;
            l++;
            Partition[p_count][l]=G[i].evertex;
            pelement[p_count]=l;
            //printf("\n New partition is created\n");
            i++;
            }
        if(V1==TRUE && V2==FALSE)
            {
            pelement[wp1]++;
            Partition[wp1][pelement[wp1]]=G[i].evertex;
            //for(k=1;k<=pelement[wp1];k++)
                //printf(" S= %d ",Partition[wp1][k]);
            //printf("\n");
            i++;
            }
        if(V1==FALSE && V2==TRUE)
            {
            pelement[wp2]++;
                 Partition[wp2][pelement[wp2]]=G[i].svertex;
            //for(k=1;k<=pelement[wp2];k++)
                //printf(" S= %d ",Partition[wp2][k]);
            //printf("\n");
            i++;
                }
            if(V1==TRUE && V2==TRUE)
            {
            y=(wp1<wp2) ? wp1 :wp2;
            z=(wp1>wp2) ? wp1 :wp2;
            l=pelement[y];
            pelement[y] += pelement[z];
            p=1;
            for(k=l+1;k<=pelement[y];k++)
                {
                Partition[y][k]=Partition[z][p];
                //printf(" %d ",Partition[z][p]);
                p++;
                }
            //for(k=1;k<=pelement[y];k++)
                //printf(" S= %d ",Partition[y][k]);
            //printf("\n");
            for(k=z;k<p_count;k++)
                {
                l=pelement[k+1];
                for(m=1;m<=l;m++)
                    Partition[k][m]=Partition[k+1][m];
                }
            i++;
            p_count--;
                }
            if(i==edge_count)
        break;
        }
    else
        {
        i++;
        if(i==edge_count)
        break;
        }
    }  
    printf("\n The minimum cost is: %f\n",min_cost);
 return;
}
void sort_edges(Graph *G,int edge_count)
{
    int i,j,count=0;;
    float temp;
    for(i=1;i<=edge_count;i++)
        {
            for(j=1;j<=edge_count-i;j++)
                {
                if(G[j].ecost>G[j+1].ecost)
                    {
                    temp=G[j].ecost;
                    G[j].ecost=G[j+1].ecost;
                    G[j+1].ecost=temp;
                    temp=G[j].svertex;
                    G[j].svertex=G[j+1].svertex;
                    G[j+1].svertex=temp;
                    temp=G[j].evertex;
                    G[j].evertex=G[j+1].evertex;
                    G[j+1].evertex=temp;
                    }
                }
        }
    printf("\n");
    return;
}

Thursday, July 18, 2013

Gauss-Jordan Elimination Process

Solution of system of linear equation by Gauss-Jordan elimination process. #include<stdio.h>


#include<malloc.h>
#include<stdlib.h>
int main()
{
double *coeff[15],*b,x1,x2,x3,*solution,temp,str,temp1;
int i,j,ROW,k,nz,p,q;
printf("\n Enter the value of row OR coloumn:");
scanf("%d",&ROW);
for(i=0;i<ROW;i++)
    coeff[i]=(double *)malloc(ROW*sizeof(double));
solution=(double *)malloc((ROW+1)*sizeof(double));
b=(double *)malloc(ROW*sizeof(double));
printf("\n Enter the elements of the coefficient matrix(A):\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
          scanf("%lf",(coeff[i]+j));
    }
printf("\n Enter the elements of the constant matrix:\n");
for(i=0;i<ROW;i++)
    {
    printf("\n Enter the %dth value:",i+1);
    scanf("%lf",(b+i));
       }
for(i=0;i<ROW;i++)
    coeff[i][ROW]=b[i];
printf("\n The Augumented (A|B) matrix is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<=ROW;j++)
        printf("%lf ",*(coeff[i]+j));
    printf("\n");
        }

for(i=0;i<ROW-1;i++)
    {
    nz=i;
    if(*(coeff[i]+i)==0.0)
        {
        do
             {
              nz++;
              if(nz>=ROW)
                  {
                  printf("\n Not solvable by Gauss Jordan:\n");
                  exit(1);
                  }
              }while(*(coeff[nz]+i)==0.0);
             printf("\nnz=%d",nz);
    for(k=0;k<=ROW;k++)
        {
        temp=*(coeff[nz]+k);// temp1=b[nz];
        *(coeff[nz]+k)=*(coeff[i]+k);// b[nz]=b[i];
        *(coeff[i]+k)=temp;// b[i]=temp1;
            }
             }
          /* printf("\n \n The Augumented (A|b) matrix after Row operations is:\n");
        for(p=0;p<ROW;p++)
            {
            for(q=0;q<ROW;q++)
                printf("%lf ",*(coeff[p]+q));
            printf("\n");
                }*/
        for(k=0;k<ROW-i-1;k++)
            {
            temp=0.0;
                   str=coeff[i+1+k][i];
                  //printf("\n str=%lf",str);
                  //printf("\n pivot=%lf",coeff[i][i]);
                  for(j=0;j<=ROW;j++)
                {
                       temp=coeff[i+k+1][j];
                       //printf(" temp=%lf ",temp);
                       if(str==0.0)
                           continue;
                //temp=temp-((str/coeff[i][i])*coeff[i][j]); temp1=temp1-((str/coeff[i][i])*I[i][j]);
                temp=temp/str;
                //temp=temp-((temp/str)*coeff[i][i]); temp1=temp1-((str/coeff[i][i])*I[i][j]);
                temp=temp*coeff[i][i];
                temp=temp-coeff[i][j];
                //printf(" [temp=%lf] ",temp);
                 coeff[i+k+1][j]=temp;
                          }
                   }
     
         
    }
printf("\n The Augumented (A|B) matrix after Row operations is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<=ROW;j++)
        printf("%lf ",*(coeff[i]+j));
    printf("\n");
        }
for(i=ROW;i>0;i--)
    {
    if(i==ROW)
        solution[i]=(*(coeff[i-1]+i)/ *(coeff[i-1]+i-1));
    else
        {
        temp=0.0;
        nz=ROW-i;
        j=ROW;
        for(k=1;k<=nz;k++)
            {
            temp += coeff[i-1][j-1]*solution[j];
            //printf("coeff[%d,%d]*solution[%d]\n",i-1,j-1,j);
            j--;
            }
        temp=coeff[i-1][ROW]-temp;
        solution[i]=(temp/coeff[i-1][i-1]);
        }
    }
printf("\n The solution by Gauss-Jordan elimination process is:\n");
for(i=1;i<=ROW;i++)
    printf("x[%d]=%lf ",i,solution[i]);
printf("\n");
return 0;
}

 


       
           


 

LU Factorisation

Solution of system of linear equation by LU factorisation.



#include<stdio.h>
#include<malloc.h>
void LUfactorisation(double *A[10],double *L[10],double *U[10], int ROW);
void Solution1(double *A[10],double *L[10],double *U[10],double *b,int ROW);
int main()
{
double *A[10],*L[10],*U[10],*b;
int ROW,i,j;
printf("\n Enter the order of the matrix:");
scanf("%d",&ROW);
for(i=0;i<=ROW;i++)
    {
    A[i]=(double *)malloc((ROW+1)*sizeof(double));
    L[i]=(double *)malloc((ROW+1)*sizeof(double));
    U[i]=(double *)malloc((ROW+1)*sizeof(double));
    }
b=(double *)malloc((ROW+1)*sizeof(double *));
printf("\n Enter the elements of the matrix A:");
for(i=1;i<=ROW;i++)
    for(j=1;j<=ROW;j++)
        {
        printf("\n Enter the element at position A[%d][%d]:",i,j);
        scanf("%lf",A[i]+j);
        }
printf("\n Enter the elements of the coloumn matrix b:");
for(i=1;i<=ROW;i++)
    {
    printf("\n Enter the element at position b[%d]:",i);
    scanf("%lf",(b+i));
    }
printf("\n The matrix A is:\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=ROW;j++)
        printf("%lf  ",*(A[i]+j));
    printf("\n");
    }
for(i=1;i<=ROW;i++)
    for(j=1;j<=ROW;j++)
        {
        if(i>j)
            *(U[i]+j)=0.0;
        else
            *(L[i]+j)=0.0;
        }
for(i=1;i<=ROW;i++)
    *(L[i]+i)=1.0;
LUfactorisation(A,L,U,ROW);
printf("\n The matrix L is:\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=ROW;j++)
        printf("%lf  ",*(L[i]+j));
    printf("\n");
    }
printf("\n The matrix U is:\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=ROW;j++)
        printf("%lf  ",*(U[i]+j));
    printf("\n");
    } 
Solution1(A,L,U,b,ROW);                             
return 0;
}
void Solution1(double *A[10],double *L[10],double *U[10],double *b,int ROW)
    {
    double *Y,*X,temp=0.0;
    int i,j,k,l;
    Y=(double *)malloc((ROW+1)*sizeof(double));
    X=(double *)malloc((ROW+1)*sizeof(double));
    for(i=1;i<=ROW;i++)
        {
        if(i==1)
            Y[i]=b[i]/L[i][i];
        else
            {
            temp = 0.0;
            k=i-1;
            l=1;
            while(l<=k)
                {
                temp = temp +(L[i][l]*Y[l]);#include<stdio.h>
#include<malloc.h>
void LUfactorisation(double *A[10],double *L[10],double *U[10], int ROW);
void Solution1(double *A[10],double *L[10],double *U[10],double *b,int ROW);
int main()
{
double *A[10],*L[10],*U[10],*b;
int ROW,i,j;
printf("\n Enter the order of the matrix:");
scanf("%d",&ROW);
for(i=0;i<=ROW;i++)
    {
    A[i]=(double *)malloc((ROW+1)*sizeof(double));
    L[i]=(double *)malloc((ROW+1)*sizeof(double));
    U[i]=(double *)malloc((ROW+1)*sizeof(double));
    }
b=(double *)malloc((ROW+1)*sizeof(double *));
printf("\n Enter the elements of the matrix A:");
for(i=1;i<=ROW;i++)
    for(j=1;j<=ROW;j++)
        {
        printf("\n Enter the element at position A[%d][%d]:",i,j);
        scanf("%lf",A[i]+j);
        }
printf("\n Enter the elements of the coloumn matrix b:");
for(i=1;i<=ROW;i++)
    {
    printf("\n Enter the element at position b[%d]:",i);
    scanf("%lf",(b+i));
    }
printf("\n The matrix A is:\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=ROW;j++)
        printf("%lf  ",*(A[i]+j));
    printf("\n");
    }
for(i=1;i<=ROW;i++)
    for(j=1;j<=ROW;j++)
        {
        if(i>j)
            *(U[i]+j)=0.0;
        else
            *(L[i]+j)=0.0;
        }
for(i=1;i<=ROW;i++)
    *(L[i]+i)=1.0;
LUfactorisation(A,L,U,ROW);
printf("\n The matrix L is:\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=ROW;j++)
        printf("%lf  ",*(L[i]+j));
    printf("\n");
    }
printf("\n The matrix U is:\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=ROW;j++)
        printf("%lf  ",*(U[i]+j));
    printf("\n");
    } 
Solution1(A,L,U,b,ROW);                             
return 0;
}
void Solution1(double *A[10],double *L[10],double *U[10],double *b,int ROW)
    {
    double *Y,*X,temp=0.0;
    int i,j,k,l;
    Y=(double *)malloc((ROW+1)*sizeof(double));
    X=(double *)malloc((ROW+1)*sizeof(double));
    for(i=1;i<=ROW;i++)
        {
        if(i==1)
            Y[i]=b[i]/L[i][i];
        else
            {
            temp = 0.0;
            k=i-1;
            l=1;
            while(l<=k)
                {
                temp = temp +(L[i][l]*Y[l]);
                l++;
                }
                Y[i]=(b[i]-temp)/L[i][i];
            } // end of else
        } // end of  for   
    for(i=ROW;i>=1;i--)
        {
        if(i==ROW)
            X[ROW]=Y[ROW]/U[ROW][ROW];
        else
            {
            temp = 0.0;
            k=i+1;
            while(k<=ROW)
                {
                temp = temp +(U[i][k]*X[k]);
                k++;
                }
            X[i]=(Y[i]-temp)/U[i][i];
                } // end of else
        } // end of outer for
    printf("\n Solution of the given linear equation using LU factorisation:\n");
    for(i=1;i<=ROW;i++)
        printf(" x[%d] = %lf\n",i,X[i]);
    return;
    }
void LUfactorisation(double *A[10],double *L[10],double *U[10], int ROW)
    {
    int i,j,k,l;
    double temp=0.0;
       for(j=1;j<=ROW;j++)
        {
        for(i=1;i<=ROW;i++)
            {
            if(i<=j)
                {
                temp=0.0;
                k=i-1;
                l=1;
                while(l <=k && i!=1)
                    {
                    temp = temp + (L[i][l]*U[l][j]);
                    l++;
                    }
                U[i][j]=A[i][j]-temp;
                                } // end of if
            else
                {
                temp=0.0;
                k=j-1;
                        l=1;
                while(l<=k)
                    {
                    temp = temp + L[i][l]*U[l][j];
                    l++;
                    }
                L[i][j]=(A[i][j]-temp)/U[j][j];
                } // end of else
            }  // end of inner for loop
        }  // end of outer for loop
return;
}

                l++;
                }
                Y[i]=(b[i]-temp)/L[i][i];
            } // end of else
        } // end of  for   
    for(i=ROW;i>=1;i--)
        {
        if(i==ROW)
            X[ROW]=Y[ROW]/U[ROW][ROW];
        else
            {
            temp = 0.0;
            k=i+1;
            while(k<=ROW)
                {
                temp = temp +(U[i][k]*X[k]);
                k++;
                }
            X[i]=(Y[i]-temp)/U[i][i];
                } // end of else
        } // end of outer for
    printf("\n Solution of the given linear equation using LU factorisation:\n");
    for(i=1;i<=ROW;i++)
        printf(" x[%d] = %lf\n",i,X[i]);
    return;
    }
void LUfactorisation(double *A[10],double *L[10],double *U[10], int ROW)
    {
    int i,j,k,l;
    double temp=0.0;
       for(j=1;j<=ROW;j++)
        {
        for(i=1;i<=ROW;i++)
            {
            if(i<=j)
                {
                temp=0.0;
                k=i-1;
                l=1;
                while(l <=k && i!=1)
                    {
                    temp = temp + (L[i][l]*U[l][j]);
                    l++;
                    }
                U[i][j]=A[i][j]-temp;
                                } // end of if
            else
                {
                temp=0.0;
                k=j-1;
                        l=1;
                while(l<=k)
                    {
                    temp = temp + L[i][l]*U[l][j];
                    l++;
                    }
                L[i][j]=(A[i][j]-temp)/U[j][j];
                } // end of else
            }  // end of inner for loop
        }  // end of outer for loop
return;
}

Wednesday, July 10, 2013

Tokens

Given a program statement in C, list the tokens in the order as they appear in the source statement. Assume that the source statement may contain more than one blank space between two successive tokens and classify the tokens

      

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
void  classification(char iden[]);
#define TRUE 1
#define FALSE 0
int main(void)
{

     char *str,ar[10];
    char *token;
     int FLAG=FALSE,count=0,n,len,i,t=0;
    printf("\n Enter the maximum length of the statement:");
     scanf(" %d", &n);
     str=(char *)malloc( n*sizeof(char));
    fflush(stdin);
    printf("\n Enter the C statement:");
     scanf("%[^\n]",str);
     len=strlen(str);
     if(len>n)
        {
        printf("\n Length of the statement is greater than the input length:");
      exit(1);
        }
    token=str;
 //   printf("\n The tokens are:\n");
    while(1)
        {
        while(*token!= ' ')
            {
            if(*token== '\0')
                {
                FLAG=TRUE;
                break;
                }
             //    printf("%c",*token);
                ar[count++]=*token;
                token++;
                }
                ar[count]='\0';
      //      printf(" %s ",ar);
             classification(ar);
      //   printf("\n");
          if(FLAG==TRUE)
            break;
        while(*token== ' ')    
            {
            *token++;
            if(*token== '\0')
                {
                     FLAG=TRUE;
                break;
                }
                }
          if(FLAG==TRUE)
                break;
          count=0;
          }
     return 0;
}
void classification( char ar[])
{

int  FLAG=FALSE,len,count=0,flag=TRUE,numb=0,d,t=0;
char *keyword[]={ "if","for","while","int","float","char"};
char *operatr[]={ "+","-","*","/","=","<",">"};
char *symbols[]={ "{","}",",","(",")",";","?",":"};
int i=0;
len=strlen(ar);
for(i=0;i<len;i++)
            if(ar[i]==' ')
                t++;
            if(t==len)
         return;
if(len>8)
    FLAG=FALSE;
for(i=0;i<=5;i++)
    {
    if(strcmp(keyword[i],ar)==0)
      {
      printf("\n%s\tis a Keyword:",ar);
      FLAG=TRUE;
      flag=FALSE;
      }
    }
    i=0;
for(i=0;i<=6;i++)
    {
    if(strcmp(operatr[i],ar)==0)
        {
        printf("\n%s\tis a Operator:",ar);
        FLAG=TRUE;
        flag=FALSE;
        }
    }
    i=0;
for(i=0;i<=7;i++)
    {
    if(strcmp(symbols[i],ar)==0)
        {
        printf("\n%s\tis a Symbol:",ar);
        FLAG=TRUE;
        flag=FALSE;
        }
    }
if ( ((65 <= ar[0] && ar[0] <= 93 )|| ( 97 <= ar[0] && ar[0] <= 122)) && len <= 8 && FLAG==FALSE)
    {
    printf("\n%s\tis an Identifier:",ar);
    FLAG=TRUE;
    flag=FALSE;
    }
for(i=0;i<len;i++)
    {
    if(49 <= ar[i] && ar[i] <=57 )
        count++;
    else
       continue;
    }
if(count==len && flag==TRUE && FLAG==FALSE)
    {
    printf("\n%s\tis a Constant:",ar);
    FLAG=TRUE;
    }
i=0;
if( FLAG==FALSE)
    {
    if(ar[0]=='-' || ar[0]=='0')
        numb=atoi(ar);
    if(numb <= 0)
    printf("%d\tis not a valid Constant ",numb);
   else
    printf("\n%s\tis an INVALID TOKEN:",ar);
   }
printf("\n");
return;
}