Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: July 2013

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