The Programming Project

Thursday, July 18, 2013

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

Monday, June 10, 2013

Interchanging specific bits

There are N bytes stored from m/m location 0001H. The value of N is stored in 000H. Write an 8085 program to interchange the bits D2 and D6 and store them into the m/m locations starting from 0010H.

LXI H,0000H
MOV B,M
INX H
LXI D,0010H
XRA A
MOV C,A
LOOP: MOV A,M
ANI 0EDH
ADD C
MOV C,A
MOV A,M
ANI 02H
RLC
RLC
RLC
ADD C
MOV C,A
MOV A,M
ANI 10H
RRC
RRC
RRC
ADD C
MVI C,00H
STAX D
INX H
INX D
DCR B
JNZ LOOP
HLT


Saturday, May 25, 2013

One's Compliment

To find 1's compliment without complimenting the accumulator or using XOR in 8085 Assembly Language

LDA 0000H
MVI B,09H
LOOP: RAR
CMC
DCR B
JNZ LOOP
STA 0001H
HLT

Tuesday, May 14, 2013

Sum of numbers with specified condition


N numbers stored consecutively from 0001H. The value of N stored at 0000H. Find the sum of the numbers whose 6th bit is 1 and store the sum and carry at 0010H, 0011H respectively

LXI H,0000H
MOV B,M
XRA A
MOV D,A
MOV E,A
INX H
LOOP: MOV A,M
ANI 40H
CPI 40H
JNZ SKIP 
MOV A,M
ADD D 
MOV D,A
JNC SKIP
INR E
SKIP: INX H
DCR B
JNZ LOOP
LXI H,0010H
MOV M,D 
INX H
MOV M,E 
HLT

Wednesday, May 1, 2013

Factorial in 8085 Programming


To find the factorial of n <=6 stored at 0000H. Result stored at memory location 0001H

LDA 0000H
MOV B,A
CPI 00H
JZ LAST1
CPI 01H
JZ LAST1
XRA A
MOV D,B
DCR B
MOV C,B
MOV E,C
LOOP: ADD  D
DCR C
JNZ LOOP
MOV D,A
XRA A
DCR E
MOV C,E
DCR B
JNZ LOOP
MOV A,D
STA 0001H
JMP LAST
LAST1: MVI A,01H
STA 0001H
LAST: HLT

Tuesday, April 30, 2013

Program to add 16 bit numbers in 8085


Two 16 bits number 2498H and 54A1H is to added the lower 8-bit sum stored at 0000H, higher 8-bit sum at 0001H and the carry at 0002H

LXI H,2498H
LXI D,54A1H
XRA A
MOV B,A
MOV A,E
ADD L
STA 0000H
MOV A,D
ADC H
JNC SKIP
INR B
SKIP: STA 0001H
MOV A,B
STA 0002H
HLT