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

The Programming Project: September 2013

Sunday, September 29, 2013

PROGRAMMING IN 8086

Fact = N * (N–1)*….2*1  (all are unsigned Word variable)


 TITLE factorial 
;TO COMPUTE
;factorial N <= 8
;----------------------------------------------------------------
     .MODEL SMALL
     .STACK 64
     .DATA
     VAR_N DW 9 
     VAR_FACT DW 1
;----------------------------------------------------------------
     .CODE
MAIN PROC FAR
     MOV AX,@DATA
     MOV DS,AX
     MOV AX,VAR_N 
     MOV DX,01
     CMP AX,00h
     JE SPECIAL
     MOV CX,AX
     MOV BX,AX
     DEC BX      
     DEC CX  
     JZ SPECIAL
     LOOP: MUL BX
           DEC BX 
           DEC CX
           JNZ LOOP         
     MOV DX,AX
     SPECIAL:MOV VAR_FACT,DX
     MOV AX,4C00H
     INT 21H
MAIN ENDP
     END MAIN
    

ROGRAMMING IN 8086

  1. Write an assembly program that converts a number in the word variable D, 0 <= D <= 15 to an ASCII character in the byte variable HEXD which is its hex value. For instance, if D = 3 then HEXD = ‘3’ and if D = 14 then HEXD = ‘E’.


TITLE  word variable to ASCII character
;----------------------------------------------------------------
     .MODEL SMALL
     .STACK 64
     .DATA
     ;ACODE   DB '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
     ACODE DB '0123456789ABCDEF'
     VALUE   DW 7  
     INDEX   DB 0
     HEXVAL  DB '?'  
;----------------------------------------------------------------
     .CODE
MAIN PROC FAR
     MOV AX,@DATA
     MOV DS,AX
     MOV ES,AX
     MOV CX,15
     LOOP:
            MOV AX,CX
            CMP AX,VALUE
            JE GOT
            INC INDEX
            DEC CX
            JNZ LOOP
     GOT:MOV AL,15
     SUB AL,INDEX
     MOV INDEX,AL
     MOV CL,INDEX
     INC CL
     LEA SI,ACODE
     LOOP1:MOV AL,[SI]
           MOV HEXVAL,AL
          INC SI
          DEC CL
          JNZ LOOP1
     MOV AX,4C00H
     INT 21H
MAIN ENDP
     END MAIN  ;END MAIN PROCEDURE
    

Programming in 8086

  1. Write a program to find out the number of positive and negative numbers from a given array of 10 16-bit numbers


TITLE POSITIVE NEGATIVE
;TO COMPUTE THE NUMBER OF
;POSITIVE AND NEGATIVE NUMBERS
;----------------------------------------------------------------
     .MODEL SMALL
     .STACK 64
     .DATA
     VAR_POS DW 00
     VAR_NEG DW 00  
     ARRAY DW 2,-3,4,5,6,-7,8,9,-2,3 ; input to the program
;----------------------------------------------------------------
     .CODE
MAIN PROC FAR
     MOV AX,@DATA  ; SET ADDRESS OF DATA SEGMENT IN DS
     MOV DS,AX
     MOV CX,10d  ; LOOP COUNTER
     MOV SI,OFFSET ARRAY ; LOADING BX WITH OFFSET
     LOOP:      
         CLC
         XOR AX,AX
         MOV AX,[SI] ; MOVING THE POINTED DATA
         RCL AX,1    ; ROTATE TO CHECK FOR MSB
         JC NEGA
         INC VAR_POS ; IF CARRY FLAG IS RESET (0), INCREASE VAR_POS
         JMP NEXT
         NEGA:
             INC VAR_NEG ; IF CARRY FLAG IS SET (1), INCREASE VAR_NEG
         NEXT:
             INC SI   ;TO FETCH THE
             INC SI   ; NEXT WORD
             DEC CX   ; LOOP UNTIL DATA ARE PROCESSED
         JNZ LOOP                                                                    
     MOV AX,4C00H
     INT 21H
MAIN ENDP
     END MAIN  ;END MAIN PROCEDURE
    

Tuesday, September 24, 2013

Combination in Lexicographical Order




#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int main()
{
char alpha[]={'?','A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,pos,r,tmp,j,flag=1;
long int tot=0;
char *combination;
printf("\n Enter the number of objects (max value is 26):");
scanf("%d",&n);
printf("\n Enter the value of r ( 1<= r <= n):");
scanf("%d",&r);
combination =(char *)malloc(n*sizeof(char));
for(i=1;i<=n;i++)
combination[i]=alpha[i];
printf("\n The combinations are (is):\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot=1;
while(flag)
{
for(i=r;i>=1;i--)
{
if(combination[i]-64==n-r+i)
{
}
else
{
pos=i;
break;
}
}
if(i==0)
{
flag=0;
break;
}
for(j=1;j<=26;j++)
if(combination[pos]==alpha[j])
break;
combination[pos]=alpha[j+1];
for(i=pos+1;i<=r;i++)
combination[i]=alpha[(combination[i-1]-64)+1];
printf("\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot++;
}
printf("\n Total number of combination is: %ld",tot);
return 0;
}


Friday, September 20, 2013

Permutation in Lexicographical Order

This C program prints the permutation of n distinct objects (0 < n <= 26 )  in Lexicographical Order.



#include<stdio.h>
#include<malloc.h>
void sort(char *permutation,int n,int pos);
void replace_min (char *permutation,int n,int pos);
int main()
{
char alpha[]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
          'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,flag=1,pos;
long int tot=0;
char *permutation;
printf("\n Enter the numbr of objects (max values is 26):");
scanf("%d",&n);
permutation =(char *)malloc(n*sizeof(char));
for(i=0;i<n;i++)
    permutation[i]=alpha[i];
printf("\n The permutations are:\n");
printf("\t\t      ");
for(i=0;i<n;i++)
    printf("%c",permutation[i]);
    tot=1;
while(flag)
    {
    pos=-1;
    for(i=n-1;i>=1;i--)
        {
        if(permutation[i-1]<permutation[i])
             {
             pos=i-1;
             flag=1;
             break;
             }
        }
        if(pos==-1)
            {
            flag=0;
            break;
            }
    replace_min(permutation,n,pos);
    sort(permutation,n,pos);
   printf("\n");
    printf("\t\t      ");
    for(i=0;i<n;i++)
        printf("%c",permutation[i]);
    //tot++;
    //getche();
    }
//printf("\n Total number of permutation is: %ld",tot);
return 0;
}
void sort(char *permutation,int n,int pos)
{
    int i,j;
   char temp='A';
    for(i=pos+1;i<n-1;i++)
        {
        for(j=pos+1;j<n-1;j++)
            {
            if(permutation[j]>permutation[j+1])
                {
                temp=permutation[j+1];
                permutation[j+1]=permutation[j];
                permutation[j]=temp;
                }
            }
        }
  return;
}
void replace_min(char *permutation,int n,int pos)
{
  char min='Z',temp;
  int k,min_pos=0;
  for(k=pos+1;k<n;k++)
    {
        if(permutation[k]>permutation[pos])
            {
            if(permutation[k]<min)
                {
                min=permutation[k];
                min_pos=k;
                }
            }
    }
    temp=permutation[min_pos];
    permutation[min_pos]=permutation[pos];
    permutation[pos]=temp;
    return;
}

Monday, September 16, 2013

Intersection of Sets



To find intersection of two sets through C

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void inter_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
inter_set(s1,s2,n,m);
return 0;
}
void inter_set(int *s1,int*s2,int n,int m)
{
int *s,i=0,j,k;
int flag=0;
s=(int *)malloc((m<n ? m: n)*sizeof(int));
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s1[k])
{
flag=1;
break;
}
}

if(flag==1)
{
s[i]=s2[j];
         i++;
flag=0;
}
 if(flag==0)
continue;
 }
 if(i==0)
printf("\n Intersection is Null:");
else
{
  printf("\n Intersection of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);
}


return;
}

Union of Sets


This C program finds the Union of two Sets (atleast one of is non-empty)
#Task- Apply recursion to the program below to find Union of finite number of sets

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void union_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
union_set(s1,s2,n,m);
return 0;
}
void union_set(int *s1,int*s2,int n,int m)
{
int *s,i,j,k;
int flag=0;
s=(int *)malloc((m+n)*sizeof(int));
for(i=0;i<n;i++)
s[i]=s1[i];
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s[k])
{
flag=1;
break;
}
}

if(flag==1)
{
flag=0;
continue;
         }
else
      if(flag==0)
{
s[i]=s2[j];
i++;
flag=0;
}
}
  printf("\n Union of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);



}

Saturday, September 14, 2013

Calculating Factorial in Python

A simple program in Python to calculate the factorial of an natural number:

#File: fact.py
#A simple program
def main():
    print "Factorial Function"
    x=input("Enter a natural number:")
    l=1
    for i in range(x):
        l=l*x
        x=x-1
    print l
main()

Run the python interpreter from the folder where the file (fact.py) is residing and import it as shown below:


administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fact
Factorial Function
Enter a natural number:350
123587405826548875014395199766546457224532073946919515879429330230093035357491314216934583295011178445941552109432761532449767761892237043444942213964090091669490545661255111334533069825455607852789836451585122902099649977304226794874840601811017764137584868137504975397325925882541777117706619490238363409254589994079334626893194608016888986949684994333459029365214555784862353939102567266745712846824819004146064184543888123533464975621179287075018586481357643313075153359002713294611632614208134036650116689052585573350955360246170451786972351365370405722036294385680478287278827977511411909071460914807681131728232182991517416470483157998067487290163200000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>>


Modified Euler's Method for Numerical Solution of first order Differential Equation

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
double dfxy(double x,double y);
FILE *fp;
int main(int argc, char* argv[])
{
double x0,y0,h,x,*xi,*yi,temp=0.0;
char c;
int n,i;
if(argc==1)
    {
    printf("\n Enter the value of h (interval-width):");
    scanf("%lf",&h);
    printf("\n Enter the initial value of x:");
    scanf("%lf",&x0);
    printf("\n Enter the initial value of y:");
    scanf("%lf",&y0);
    printf("\n Enter the value of x at which y is calculated:");
    scanf("%lf",&x);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else if(argc==2)
    {
    /*fp=fopen(argv[1],"r");
    fscanf(fp,"%d",&n);
    fscanf(fp,"%lf",&lower_limit);
    fscanf(fp,"%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));*/
    }
else
    {
    printf("\n Invalid arguments, program will terminate:");
    exit(1);
    }
n=(x-x0)/h;
xi[0]=x0;
yi[0]=y0;
printf("\n\tX\t\t\tY\n");
printf("\tf(%+lf)\t\t=%+lf",xi[0],yi[0]);
i=1;
while(x0!=x)
    {
    yi[i]=yi[i-1]+dfxy(xi[i-1],yi[i-1])*h;
    x0=x0+h;
    xi[i]=x0;
    //printf("\n >>>>>>>>%lf",yi[i]);
    do
    {
    temp=yi[i];
    yi[i]=yi[i-1]+(h/2)*(dfxy(xi[i-1],yi[i-1])+dfxy(xi[i],yi[i]));
    //printf("\n >>>>>>>>%lf",yi[i]);
    }while(fabs(temp-yi[i])>0.00001);
    i++;
    xi[i]=x0;
    yi[i]=temp;
    printf("\n\tf(%+lf)\t\t=%+lf",xi[i],yi[i]);
    }
printf("\n");
return 0;
}
double dfxy(double x,double y)
    {
    return (x*x+y);
    }

Euler's Method for Numerical Solution of first order Differential Equation





#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
double dfxy(double x,double y);
FILE *fp;
int main(int argc, char* argv[])
{
double x0,y0,h,x,*xi,*yi,temp=0.0;
int n,i;
if(argc==1)
    {
    printf("\n Enter the value of h (interval-width):");
    scanf("%lf",&h);
    printf("\n Enter the initial value of x:");
    scanf("%lf",&x0);
    printf("\n Enter the initial value of y:");
    scanf("%lf",&y0);
    printf("\n Enter the value of x at which y is calculated:");
    scanf("%lf",&x);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else if(argc==2)
    {
    /*fp=fopen(argv[1],"r");
    fscanf(fp,"%d",&n);
    fscanf(fp,"%lf",&lower_limit);
    fscanf(fp,"%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));*/
    }
else
    {
    printf("\n Invalid arguments, program will terminate:");
    exit(1);
    }
n=(x-x0)/h;
xi[0]=x0;
yi[0]=y0;
printf("\n\tX\t\t\tY\n");
printf("\t%+lf\t\t%+lf",xi[0],yi[0]);
i=1;
while(x0!=x)
    {
    yi[i]=yi[i-1]+dfxy(xi[i-1],yi[i-1])*h;
    x0=x0+h;
    xi[i]=x0;
    printf("\n\t%+lf\t\t%+lf",xi[i],yi[i]);
    }
printf("\n");
//printf("\n Value of the integral using Trapezoidal Rule is %+.8lf\n",invert==0 ? (h/2.0)*temp:-(h/2.0)*temp);
return 0;
}
double dfxy(double x,double y)
    {
    return (x+y);
    }

Simpson's Rule




#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
double integrand(double x);
FILE *fp;
int main(int argc, char* argv[])
{
double lower_limit,upper_limit,h,*xi,*yi,temp=0.0;
int n,i,invert=0;
if(argc==1)
    {
    printf("\n Enter the number of intervals:");
    scanf("%d",&n);
    printf("\n Enter the lower limit:");
    scanf("%lf",&lower_limit);
    printf("\n Enter the upper limit:");
    scanf("%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else if(argc==2)
    {
    fp=fopen(argv[1],"r");
    fscanf(fp,"%d",&n);
    fscanf(fp,"%lf",&lower_limit);
    fscanf(fp,"%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else
    {
    printf("\n Invalid arguments, program will terminate:");
    exit(1);
    }
if(lower_limit>upper_limit)
    {
    temp=lower_limit;
    lower_limit=upper_limit;
    upper_limit=temp;
    invert=1;
    }
h=(upper_limit-lower_limit)/n;
temp=lower_limit;
for(i=0;i<=n;i++)
    {
    xi[i]=temp;
    temp=temp+h;
    }
printf("  xi\t\t\tyi\n");
temp=0.0;
for(i=0;i<=n;i++)
    {
    yi[i]=integrand(xi[i]);
    printf("%+lf\t\t%+.8lf\n",xi[i],yi[i]);
    if(i==0 || i==n)
        temp+= yi[i];
    else   
        {
        if(i%2==0)
        temp += 2*yi[i];
        else
        temp += 4*yi[i];
        }
    }   
printf("\n Value of the integral using Simpson's Rule is %+.8lf\n",invert==0 ? (h/3.0)*temp:-(h/3.0)*temp);
return 0;
}
double integrand(double x)
    {
    return (log(x));
    }

Trapezoidal Rule

C Program to implement Trapezoidal Rule



#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
double integrand(double x);
FILE *fp;
int main(int argc, char* argv[])
{
double lower_limit,upper_limit,h,*xi,*yi,temp=0.0;
int n,i,invert=0;
if(argc==1)
    {
    printf("\n Enter the number of intervals:");
    scanf("%d",&n);
    printf("\n Enter the lower limit:");
    scanf("%lf",&lower_limit);
    printf("\n Enter the upper limit:");
    scanf("%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else if(argc==2)
    {
    fp=fopen(argv[1],"r");
    fscanf(fp,"%d",&n);
    fscanf(fp,"%lf",&lower_limit);
    fscanf(fp,"%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else
    {
    printf("\n Invalid arguments, program will terminate:");
    exit(1);
    }
if(lower_limit>upper_limit)
    {
    temp=lower_limit;
    lower_limit=upper_limit;
    upper_limit=temp;
    invert=1;
    }
h=(upper_limit-lower_limit)/n;
temp=lower_limit;
for(i=0;i<=n;i++)
    {
    xi[i]=temp;
    temp=temp+h;
    }
printf("  xi\t\t\tyi\n");
temp=0.0;
for(i=0;i<=n;i++)
    {
    yi[i]=integrand(xi[i]);
    printf("%+lf\t\t%+.8lf\n",xi[i],yi[i]);
    if(i==0 || i==n)
        temp+= yi[i];
    else
        temp += 2*yi[i];
    }   
printf("\n Value of the integral using Trapezoidal Rule is %+.8lf\n",invert==0 ? (h/2.0)*temp:-(h/2.0)*temp);
return 0;
}
double integrand(double x)
    {
    return (log(x));
    }

Tuesday, September 10, 2013

Applet

This Java code simply display a "Quote" on the Applet. Here is the snapshot.
 

import java.awt.*;
import java.applet.*;
/*
<applet code="AppletQuote" width=900 height=80>
</applet>
*/
public class AppletQuote extends Applet
    {
    public void init()
        {
        setBackground(Color.cyan);
        }
    public void paint(Graphics g)
        {
        Quotes qts = new Quotes();
            int n = (int)(Math.random()*10);
              g.drawString(qts.getQuote(n),30,30);   
            }
        }
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"};
    }

Lagrangian Interpolation






#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
FILE *fp;
int main(int argc, char *argv[])
{
double **A,*b,*xn,*xnplus1,*p,temp=1.0,x,value=0.0,**U;
int ROW,i,j,k,t=1;
char ch;
if(argc==1)
    {
    printf("\n Enter the number of values:");
    scanf("%d",&ROW);
    printf("\n Enter the value at which value has to be aprroximated:");
    scanf("%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    for(i=0;i<=ROW;i++)
        {
        A[i]=(double *)malloc((ROW+1)*sizeof(double));
        }
    b=(double *)malloc((ROW+1)*sizeof(double));
    p=(double *)malloc((ROW+1)*sizeof(double));
    xn=(double *)malloc((ROW+1)*sizeof(double));
    xnplus1=(double *)malloc((ROW+1)*sizeof(double));
    for(i=1;i<=ROW;i++)
        {
        printf("\n Enter the value of X[%d]:",i);
        scanf("%lf",&A[i][1]);
        }
    for(i=1;i<=ROW;i++)
        {
        printf("\n Enter the value of Y[%d]:",i);
        scanf("%lf",&A[i][2]);
        }
       
    }
else if (argc==2)
    {
    fp=fopen(argv[1],"r");
        if(fp==NULL)
            {
            printf("\n File not found, program will terminate:");
            exit(0);
            }
    fscanf(fp,"%d",&ROW);
    fscanf(fp,"%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    for(i=0;i<=ROW;i++)
        {
        A[i]=(double *)malloc((ROW+1)*sizeof(double));
        }
    b=(double *)malloc((ROW+1)*sizeof(double));
    p=(double *)malloc((ROW+1)*sizeof(double));
    xn=(double *)malloc((ROW+1)*sizeof(double));
    while(!feof(fp))
    {
    for(i=1;i<=ROW;i++)
    {
        for(j=1;j<=2;j++)
        {
        fscanf(fp,"%lf ",&A[i][j]);
        }
       
    }
    }
    fclose(fp);
    }
    else
    {
    printf("\n Invalid Arguments,program will terminate:\n");
    exit(0);
    }
printf("\n         X          Y");
printf("\n------------------------------------------------------------------\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=2;j++)
        {
       
        A[i][j];
        printf("   %+lf",A[i][j]);
        }
    printf("\n");
    }
printf("\n------------------------------------------------------------------\n");
for(i=1;i<=ROW;i++)
    {
     for(j=1;j<=ROW;j++)
         {
         if(i==j)
             continue;
         else
             temp = temp * ((x-A[j][1])/(A[i][1]-A[j][1]));
         }
     //xn[i]=temp;
     value += temp*A[i][2];
     //printf("\n ------------------------------->%lf",temp);   
     temp=1.0;
    }   
printf("\n Value at %+lf by Lagrangian Interpolation formula is %+lf\n",x,value);
return 0;
}

Divided Difference




#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void differenceTable(double **A,double **L,int ROW);
double result(double **A,double **L,double x,int ROW);
double factorial(int n);
FILE *fp;
int main(int argc, char *argv[])
{
double **A,*b,*xn,*xnplus1,*p,temp,x,**L,**U,lambda,error,t1=0.0;
int ROW,i,j,k=0,t=1,m=1,bo=1;
char ch;
if(argc==1)
    {
    printf("\n Enter the number of values:");
    scanf("%d",&ROW);
    printf("\n Enter the value at which value has to be aprroximated:");
    scanf("%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    L=(double **)malloc((ROW+1)*sizeof(double*));
    U=(double **)malloc((ROW+1)*sizeof(double*));
    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));
    p=(double *)malloc((ROW+1)*sizeof(double));
    xn=(double *)malloc((ROW+1)*sizeof(double));
    xnplus1=(double *)malloc((ROW+1)*sizeof(double));
    for(i=1;i<=ROW;i++)
        {
        printf("\n Enter the value of X[%d]:",i);
        scanf("%lf",&A[i][1]);
        }
    for(i=1;i<=ROW;i++)
        {
        printf("\n Enter the value of Y[%d]:",i);
        scanf("%lf",&A[i][2]);
        }
       
    }
else if (argc==2)
    {
    fp=fopen(argv[1],"r");
        if(fp==NULL)
            {
            printf("\n File not found, program will terminate:");
            exit(0);
            }
    fscanf(fp,"%d",&ROW);
    fscanf(fp,"%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    L=(double **)malloc((ROW+1)*sizeof(double*));
    U=(double **)malloc((ROW+1)*sizeof(double*));
    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));
    p=(double *)malloc((ROW+1)*sizeof(double));
    xn=(double *)malloc((ROW+1)*sizeof(double));
    xnplus1=(double *)malloc((ROW+1)*sizeof(double));
   
    while(!feof(fp))
    {
    for(i=1;i<=ROW;i++)
    {
        for(j=1;j<=2;j++)
        {
        fscanf(fp,"%lf ",&A[i][j]);
        }
       
    }
    }
    fclose(fp);
    }
    else
    {
    printf("\n Invalid Arguments,program will terminate:\n");
    exit(0);
    }
printf("\n         X          Y");
printf("\n------------------------------------------------------------------\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=2;j++)
        {
       
        U[i][j]=A[i][j];
        printf("   %+lf",U[i][j]);
        }
    printf("\n");
    }
for(i=1;i<=ROW-1;i++)
    {
    for(j=ROW-i,t=1;j>=1;j--,t++)
        {
        L[t][i]=(U[t+1][2]-U[t][2])/(A[t+1+k][1]-A[t][1]);
        //printf("\n (%d,%d)------>%lf",t-1,t+1-1+k,L[t][i]);
        }
    for(t=1;t<=ROW-i;t++)
        U[t][2]=L[t][i];
    //printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    k++;
    }   
differenceTable(A,L,ROW);
printf("\n Value at %+lf by Divied Difference Interpolation formula is %+lf\n",x,result(A,L,x,ROW));
return 0;
}
double result(double **A,double **L,double x,int ROW)
{
int i,j;
double value=0,tmp=1.0;
for(i=0;i<ROW;i++)
{
    if(i==0)
        value +=A[1][2];
    else
    {
    for(j=1;j<=i;j++)
        tmp=tmp*(x-A[j][1]);
    tmp = tmp/factorial(i);
    tmp = tmp*L[1][i];
    value +=tmp;
    }
    if(i==0)
    printf("%2.2lf ",A[1][2]);
    else
    printf("%2.2lf ",L[1][i]);
    tmp=1.0;
}
return value;
}
double factorial(int n)
{
 if(n<=1)
     return 1;
 else
     return n*factorial(n-1);
}
void differenceTable(double **A,double **L,int ROW)
{
int **position,*ap,j,m,i;
position=(int **)malloc((ROW+1)*sizeof(int*));
ap=(int *)malloc((ROW+1)*sizeof(int));
for(i=0;i<=ROW;i++)
    position[i]=(int *)malloc((ROW+1)*sizeof(int));
int an,tmp;
tmp=ROW;
for(i=1;i<=ROW;i++)
    {
    an=1+(i-1)*2;
    ap[i]=an;
    for(j=1;j<=tmp;j++)
        {
        position[i][j]=an+(j-1)*4;
        }
    tmp--;
    }
tmp=ROW;
tmp=ROW+(ROW-1)*3;
int *match,tmp1,l,*pos,flag,z,k;
match=(int *)malloc((ROW+1)*sizeof(int));
pos=(int *)malloc((ROW+1)*sizeof(int));
for(i=0;i<=ROW;i++)
    {
    match[i]=0;
    pos[i]=0;
    }

printf("\n-------------------------------------Divided Difference Table----------------------------------\n");
printf("\n X        Y");
for(i=1;i<=ROW-1;i++)
    printf("                D^%d",i);
printf("\n----------------------------------------------------------------------------------------------\n");
for(i=1;i<=tmp;i++)
{
tmp1=ROW;

    for(l=1;l<=ROW;l++)
    {
    flag=0;
    for(m=1;m<=tmp1;m++)
        {
        if(i==position[l][m])
            {
            flag=1;
            match[l]=1;
            pos[m]=position[l][m];
            break;
            }
        } //inner for
    if(flag==1)
    {
    for(k=1;k<=ROW;k++)
        {
    if(match[k]==0)
    {
        printf("");
    }
    else
        {
        if(k==1)
            //printf("(%d,%d)+(%d,%d)|%d",(position[l][m]/4)+1,k,(position[l][m]/4)+1,k+1,i);
            printf("%+2.4lf\t %+2.4lf",A[(position[l][m]/4)+1][k],A[(position[l][m]/4)+1][k+1]);
        else
            {
            z=(position[l][m]-ap[k])/4+1;
            //printf("\t\t(%d,%d)|%d",z,k-1,i);
            printf("\t\t\t%+lf",L[z][k-1]);
            }
       
        }
        } //end of k-loop
           
    } //end of flag==1
   
    else
        {printf(" ");}
    tmp1--;
    for(k=0;k<=ROW;k++)
    {
    pos[k]=0;
    match[k]=0;
    }
    }
   
printf("\n");
}
printf("\n----------------------------------------------------------------------------------------------\n");
}

Nested Class Demonstration

This program will generate a Bank A/C class for some customers with provision for Locker and FixedDeposit classes, both of which will be inner-class of Bank class. One deducts a fixed locker fee from the outer class balance variable and the other one adds interest to the balance variable, calculated on the amount to be fixed.



import java.util.*;
public class BankAccount
    {
    public static void main(String[] args)
        {
        String choice="";
        Bank[] customer = new Bank[10];
        for(int i=0;i<10;i++)
            customer[i]=new Bank();
            int k=0;
            boolean flag=false;
            Scanner in = new Scanner(System.in);
        do
            {
            customer[k].setData();
            for(int i=0;i<k;i++)
                  if(customer[k].getAccountNumber()==customer[i].getAccountNumber())
                    flag=true;
            if(flag==true)
                {
                System.out.println("Account Number already exisits:");
                flag=false;
                continue;       
                }
            k++;   
            System.out.println(" Want to add another record yes/no:");
            choice = in.next();
            }while("YES".equalsIgnoreCase(choice));
            flag=false;
            int i;
            System.out.println(" Do you want to get locker facility  yes/no:");
            choice = in.next();
                if("YES".equalsIgnoreCase(choice))
                    {
                    System.out.println(" Enter the A/C number:");
                    int ac = in.nextInt();
                    for(i=0;i<k;i++)
                         if(ac==customer[i].getAccountNumber())
                         {
                         flag=true;
                         break;
                         }
                    if(flag)
                        customer[i].displayLocker();
                    else
                        System.out.println(" Invalid A/C Number:");
                    }
            flag=false;
            System.out.println(" Do you want to fix an amount yes/no:");
            choice = in.next();
                if("YES".equalsIgnoreCase(choice))
                    {
                    System.out.println(" Enter the A/C number:");
                    int ac = in.nextInt();
                    for(i=0;i<k;i++)
                         if(ac==customer[i].getAccountNumber())
                         {
                         flag=true;
                         break;
                         }
                    if(flag)
                        {
                        System.out.println(" Enter the amount you want to deposite:");
                        double amt = in.nextDouble();
                        customer[i].displayFixedDeposit(amt);
                        }
                    else
                        System.out.println(" Invalid A/C Number:");
                    }
        }
    }
class Bank
    {
    Bank()
        {
        accountNumber=0;
        balance=0.0;
        name="";
        }
    public void setData()
        {
        Scanner in = new Scanner(System.in);
        System.out.println(" Enter the name:");
        name=in.nextLine();
        System.out.println(" Enter the A/C number:");
        accountNumber=in.nextInt();
        System.out.println(" Enter the balance:");
        balance=in.nextDouble();
        }
    public void displayFixedDeposit(double amt)
        {
        FixedDeposit fd = new FixedDeposit();
        fd.addInterest(amt);
        fd.afterAdd();
        }
    public void displayLocker()
        {
        Locker loc = new Locker();
        loc.deduct();
        loc.afterDeduct();
        }
    public int getAccountNumber()
        {
        return accountNumber;
        }
    public void getData()
        {
        System.out.println("A/C number:"+accountNumber+" Name:"+name+" Balance:"+balance);
        }
    class Locker{
            void deduct()
                {   
                balance -= lockerFee;
                }
            void afterDeduct()
                {
                System.out.println(" After Deduction:");
                getData();            
                }
            final double lockerFee = 30.0;
                }
    class FixedDeposit{
              void addInterest(double amt)
                  {
                  balance += (8.25*amt)/100;
                  }
              void afterAdd()
                  {
                  System.out.println(" After adding Interest:");
                  getData();
                  }
              final double interestRate = 8.25;
              }
    private int accountNumber;
    private double balance;
    private String name;
    }

Monday, September 9, 2013

Aitken's Interpolation

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void differenceTable(double **A,double **L,int ROW);
FILE *fp;
int main(int argc, char *argv[])
{
double **A,*b,*xn,*xnplus1,*p,temp=0.0,x,**L,**U,lambda,error,t1=0.0;
int ROW,i,j,k,t=1,m=1,bo=1;
char ch;
if(argc==1)
{
printf("\n Enter the number of values:");
scanf("%d",&ROW);
printf("\n Enter the value at which value has to be aprroximated:");
scanf("%lf",&x);
A=(double **)malloc((ROW+1)*sizeof(double*));
L=(double **)malloc((ROW+1)*sizeof(double*));
U=(double **)malloc((ROW+1)*sizeof(double*));
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));
p=(double *)malloc((ROW+1)*sizeof(double));
xn=(double *)malloc((ROW+1)*sizeof(double));
xnplus1=(double *)malloc((ROW+1)*sizeof(double));
for(i=1;i<=ROW;i++)
{
printf("\n Enter the value of X[%d]:",i);
scanf("%lf",&A[i][1]);
}
for(i=1;i<=ROW;i++)
{
printf("\n Enter the value of Y[%d]:",i);
scanf("%lf",&A[i][2]);
}

}
else if (argc==2)
{
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("\n File not found, program will terminate:");
exit(0);
}
fscanf(fp,"%d",&ROW);
fscanf(fp,"%lf",&x);
A=(double **)malloc((ROW+1)*sizeof(double*));
L=(double **)malloc((ROW+1)*sizeof(double*));
U=(double **)malloc((ROW+1)*sizeof(double*));
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));
p=(double *)malloc((ROW+1)*sizeof(double));
xn=(double *)malloc((ROW+1)*sizeof(double));
xnplus1=(double *)malloc((ROW+1)*sizeof(double));

while(!feof(fp))
{
for(i=1;i<=ROW;i++)
{
for(j=1;j<=2;j++)
{
fscanf(fp,"%lf ",&A[i][j]);
}

}
}
fclose(fp);
}
else
{
printf("\n Invalid Arguments,program will terminate:\n");
exit(0);
}
printf("\n         X          Y");
printf("\n------------------------------------------------------------------\n");
for(i=1;i<=ROW;i++)
{
for(j=1;j<=2;j++)
{

U[i][j]=A[i][j];
printf("   %+lf",U[i][j]);
}
printf("\n");
}
k=0;
for(i=1;i<=ROW-1;i++)
{
for(j=ROW-i,t=1;j>=1;j--,t++)
{
//printf("\n %lf-----------%lf",U[t+1+k][1],U[i][1]);
temp=1/(U[t+1+k][1]-U[i][1]);
//printf("\n (%d,%d)----------(%d,%d)",t+k,1,i-1,1);
//printf("\n yi-1(%lf)*x1-x(%lf)-------yi(%lf)*x0-x(%lf)",U[1][2],U[t+1+k][1]-x,U[1+t][2],U[i][1]-x);
temp = temp * ((U[t+1+k][1]-x)*U[1][2]-(U[i][1]-x)*U[t+1][2]);
L[t][i]=temp;
//printf("\n-----------%lf",temp);
}
for(t=1;t<=ROW-i;t++)
{
U[t][2]=L[t][i];
//printf("\n>>>>>>>>>>>%lf",U[t][2]);
}
k++;
temp=0.0;
//printf("\n___________________________________________________");
}
differenceTable(A,L,ROW);
printf("\n Value at %+lf by Aitken's Interpolation formula is %+lf\n",x,U[1][2]);
return 0;
}
void differenceTable(double **A,double **L,int ROW)
{
int **position,*ap,j,m,i;
position=(int **)malloc((ROW+1)*sizeof(int*));
ap=(int *)malloc((ROW+1)*sizeof(int));
for(i=0;i<=ROW;i++)
position[i]=(int *)malloc((ROW+1)*sizeof(int));
int an,tmp;
tmp=ROW;
for(i=1;i<=ROW;i++)
{
an=1+(i-1)*2;
ap[i]=an;
for(j=1;j<=tmp;j++)
{
position[i][j]=an+(j-1)*4;
}
tmp--;
}
tmp=ROW;
tmp=ROW+(ROW-1)*3;
int *match,tmp1,l,*pos,flag,z,k;
match=(int *)malloc((ROW+1)*sizeof(int));
pos=(int *)malloc((ROW+1)*sizeof(int));
for(i=0;i<=ROW;i++)
{
match[i]=0;
pos[i]=0;
}

printf("\n-----------------------------Aitken's Table-----------------------------\n");

printf("\n X       Y");
for(i=1;i<=ROW-1;i++)
printf("           D_%d",i);
printf("\n----------------------------------------------------------------------------------\n");
for(i=1;i<=tmp;i++)
{
tmp1=ROW;

for(l=1;l<=ROW;l++)

{
flag=0;
for(m=1;m<=tmp1;m++)
{
if(i==position[l][m])
{
flag=1;
match[l]=1;
pos[m]=position[l][m];
break;
}
} //inner for
if(flag==1)
{
for(k=1;k<=ROW;k++)
{
if(match[k]==0)
{
printf("");
}
else
{
if(k==1)
//printf("(%d,%d)+(%d,%d)|%d",(position[l][m]/4)+1,k,(position[l][m]/4)+1,k+1,i);
printf("%+2.2lf\t%+2.2lf",A[(position[l][m]/4)+1][k],A[(position[l][m]/4)+1][k+1]);
else
{
z=(position[l][m]-ap[k])/4+1;
//printf("\t\t(%d,%d)|%d",z,k-1,i);
printf("\t\t%+lf",L[z][k-1]);
}

}
} //end of k-loop

} //end of flag==1

else
{printf(" ");}
tmp1--;
for(k=0;k<=ROW;k++)
{
pos[k]=0;
match[k]=0;
}
}

printf("\n");
}
printf("\n--------------------------------------------------------------------------\n");
}