The Programming Project: jadavpur
Showing posts with label jadavpur. Show all posts
Showing posts with label jadavpur. Show all posts

Thursday, August 29, 2013

Random Quotes

This is a command line utility that prints a "Maths Quote of the Day" on the user's terminal when run.
User's types "java MathQuoteOFDay" on the command line
System prints out a quote.
import java.util.*;
public class MathsQuoteOFDay
    {
    public static void main(String[] args)
        {
        Quotes qts = new Quotes();
            int n = (int)(Math.random()*10);
            System.out.println("");
            System.out.println(qts.getQuote(n));   
            System.out.println("");        
        }
    }
class Quotes
    {
    Quotes()
        {
        }
    public String getQuote(int n)
        {
        return (quotes[n]);
        }
    private static String quotes[]={"I like mathematics because it is not human and has nothing particular to do with this planet or with the whole accidental universe                         - because like Spinoza's God, it won't love us in return.  ~Bertrand Russell",
                    "If there is a God, he's a great mathematician.  ~Paul Dirac",
                    "Do not worry about your problems with mathematics, I assure you mine are far greater. ~Albert Einstein",
                    "There are things which seem incredible to most men who have not studied mathematics. ~ Aristotle",
                    "Mathematics is a game played according to certain simple rules with meaningless marks on paper. ~ David Hilbert",
                    "Go down deep enough into anything and you will find mathematics~ Dean Schlicter",
                    " Life is good for only two things, discovering mathematics and teaching mathematics. ~ Simeon Poisson",
                    "In mathematics you don't understand things. You just get used to them. ~ Johann von Neumann",
                    " Medicine makes people ill, mathematics make them sad and theology makes them sinful. ~ Martin Luther",
                    "A man whose mind has gone astray should study mathematics. ~ Francis Bacon"};
    }

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~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;
}

Friday, March 29, 2013

C Program #19


/* Dynamic Implementation of queue and few basic operations!*/

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
struct queue{
                                                int items;
                                                struct queue *next;
                                                };
typedef struct queue node;
struct stack{
                        int top;
                        int items[MAXSIZE];
                        };
struct stack stk;
struct queue p,q,temp;
int pop(struct stack *);
int remove(node **);
int Isempty(node *);
void push(struct stack *,int);
int isempty_st(struct stack *);
void insert(node *,int);
void reverse_queue(node **);
void display_front_rear(node **);
void display_rear_front(node **);
void append(node *,node *);
void replace(node**,int,int,node **);
void display_front_rear_unchanged(node **,node **);
void display_rear_front_unchanged(node **,node **);
int Isequal(node **,node**,node**,node**);
int main()
{
            int choice,x,ins,e;
            stk.top=-1;
            node *rootp,*rootq,*tqueue,*rqueue;
            rootp=(node *)malloc(sizeof(node));
            rootp->next=NULL;
            rootq=(node *)malloc(sizeof(node));
            rootq->next=NULL;
            tqueue=(node *)malloc(sizeof(node));
            tqueue->next=NULL;
            rqueue=(node *)malloc(sizeof(node));
            rqueue->next=NULL;
            do
                        {
                        printf("\n Press 1 to add elements in Queue:");
                        printf("\n Press 2 to remove elements from Queue:");
                        printf("\n Press 3 to append queue Q at the end of queue P:");
                        printf("\n Press 4 to print elements of queue P from front to end with P becoming empty:");
                        printf("\n Press 5 to print elements of queue P from rear to front with P becoming empty:");
                        printf("\n Press 6 to print elements of queue P from front to end with P remaining same:");
                        printf("\n Press 7 to print elements of queue P from rear to front with P remaining same:");
                        printf("\n Press 8 to reverse the queue P:");
                        printf("\n Press 9 to replace a specific element of a queue:");
                        printf("\n Press 10 to check whether the queues P and Q are equal or not:");
                        printf("\n Type 11 to exit:");
                        scanf("%d",&choice);
                        switch(choice)
                                    {
                                    case 1:
                                                            printf("\n Enter 1 to insert in queue P, 2 to insert in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                                    printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(rootp,ins);
                                                                                    break;
                                                                                    case 2:
                                                                                    printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(rootq,ins);
                                                                                    break;
                                                                                    }
                          break;
                          case 2:
                                                            printf("\n Enter 1 to remove in queue P, 2 to remove in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                                    e=remove(&rootp);
                                                                                    //if(e=-9999)
                                                                                    //                      {}
                                                                           //       else
                                                                                    printf("\n Element removed is:%d\n",e);
                                                                                    break;
                                                                                    case 2:
                                                                                    e=remove(&rootq);
                                                                                    //if(e=-9999)
                                                                                    //          {}
                                                                                    //else
                                                                                    printf("\n Element removed is: %d\n",e);
                                                                                    break;
                                                                                    }
                                    break;
                                    case 3:
                                    append(rootp,rootq);
                                    break;
                                    case 4:
                                    display_front_rear(&rootp);
                                    break;
                                    case 5:
                                    display_rear_front(&rootp);
                                    break;
                                    case 6:
                                    display_front_rear_unchanged(&rootp,&tqueue);
                                    break;
                                    case 7:
         case 8:
                                    reverse_queue(&rootp);
                                    break;
                                    display_rear_front_unchanged(&rootp,&tqueue);
                                    break;
                                    case 9:
                                    printf("\n Enter the element to be replaced:");
                                    scanf("%d",&e);
                                    printf("\n Enter the element to be placed:");
                                    scanf("%d",&x);
                                    replace(&rootp,e,x,&tqueue);
                                    break;
                                    case 10:
                                    if(Isequal(&rootp,&rootq,&tqueue,&rqueue))
                                                printf("\n Queues are equal:\n");
                                    else
                                                printf("\n Queues are not equal:\n");
                                    break;
                                    }
                        }while(choice>=1 && choice<=10);
return 0;
}
int Isequal(node **qp,node **qq,node **t1,node **t2)
            {
            int FLAG=TRUE,s1=0,s2=0,x,y;
            if((Isempty(*qp) && !Isempty(*qq)) ||(!Isempty(*qp) && Isempty(*qq)))
                        return (FALSE);
            else if(Isempty(*qp) && Isempty(*qq))
                        return (TRUE);
            else
                                    {
                                    while(!Isempty(*qp))
                                                {insert(*t1,remove(qp)); s1++;}
                                    while(!Isempty(*qq))
                                                {insert(*t2,remove(qq)); s2++;}
                                    if(s1!=s2)
                                                return (FALSE);
                                    else
                                                {
                                                while(!Isempty(*t1))
                                                                        {
                                                                        x=remove(t1); y=remove(t2);
                                                                        if(x!=y)
                                                                        FLAG=FALSE;
                                                                        insert(*qp,x); insert(*qq,y);
                                                                        }
                                                }
                                     if(FLAG==TRUE)
                                                return (TRUE);
                                     else
                                                return (FALSE);
                                    }

   }
void replace(node**qp,int e,int x,node **qq)
            {
            int y;
            node *tmp;
            tmp=*qp;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                        insert(*qq,remove(qp));
            while(!Isempty(*qq))
                        {
                        y=remove(qq);
                        if(y==e)
                                    {
                                    printf("%d ",x);
                                    insert(tmp,x);
               }
                        else
                                    {
                                    insert(tmp,y);
                                    printf("%d ",y);
                                    }
                        }
            printf("\n");
            }
void display_rear_front_unchanged(node **qp,node **qq)
            {
            int x;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                         {
                         x=remove(qp);
                         push(&stk,x);
                         insert(*qq,x);
                         }
            while(!isempty_st(&stk))
                        {
                        printf("%d ",pop(&stk));
                        insert(*qq,remove(qq));
                        }
            printf("\n");
            }
void display_front_rear_unchanged(node **qp,node **qq)
            {
            int x;
            node *tmp;
            tmp=*qp;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                        insert(*qq,remove(qp));
            while(!Isempty(*qq))
                        {
                        x=remove(qq);
                        printf("%d ",x);
                        insert(tmp,x);
                        }
            printf("\n");
            }

void reverse_queue(node **use)
{
            node *curr,*prev;
   prev=(node *)malloc(sizeof(node));
            prev->next=NULL;
            curr=*use;
            while((*use)->next!=NULL)
                        {
                                                curr=*use;
                                                *use=(*use)->next;
                                                curr->next=prev;
                                                prev=curr;
                        }
                        *use=curr;
}
void display_rear_front(node **use)
            {
            printf("\n Elements of the queue:");
            while(!Isempty(*use))
                        push(&stk,remove(use));
            while(!isempty_st(&stk))
                        printf("%d ",pop(&stk));
            printf("\n");
            }
void display_front_rear(node **use)
            {
            printf("\n Elements of the queue:");
            while(!Isempty(*use))
                        printf("%d ",remove(use));
            printf("\n");
            }
void append(node *p,node *q)
            {
            while(p->next->next!=NULL)
                        p=p->next;
            p->next=q;
            }
int Isempty(node *use)
            {
            return ((use->next==NULL)? TRUE : FALSE);
            }
int remove(node **use)
            {
            if(Isempty(*use))
                        {printf("\n Queue is empty:\n");
                        return (-9999);}
            else{
            node *temp,*tp;
            temp=*use;
            tp=temp;
            temp=temp->next;
            *use=temp;
            return(tp->items);
            free(tp);}
            }
void insert(node *use,int x)
            {
            node *p;
            while(use->next!=NULL)
            use=use->next;
            use->items=x;
            p=(node *)malloc(sizeof(node));
            use->next=p;
            p->next=NULL;
            }
int isempty_st(struct stack *stk)
            {
             return((stk->top==-1)?TRUE:FALSE);
            }
int pop(struct stack *stk)
            {
            return(stk->items[(stk->top)--]);
            }
void push(struct stack *stk,int x)
            {
            stk->items[++stk->top]=x;
            }