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

Tuesday, September 10, 2013

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

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

Gauss Central Backward Interpolation

#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,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]);
//printf("\n %lf-----%lf",U[t+1][2],U[t][2]);
}
for(t=1;t<=ROW-i;t++)
U[t][2]=L[t][i];
}
differenceTable(A,L,ROW);
printf("\n Value at %+lf by Gauss Backward 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,flag=1,t,k,sta;
t=(ROW+1)/2;
double h,value=0,p,tmp=1.0;
h=A[2][1]-A[1][1];
//printf("\np is ***********%lf",h);
p=(x-A[t][1])/h;
//printf("\np is ***********%lf",p);
for(i=0;i<ROW;i++)
{
if(i==0)
{
value +=A[t][2];
//printf("\n*************%lf",value);
}
else
{
k=0;
sta=1;
for(j=1;j<=i;j++)
{
//printf("\nTTTTTTTTTTTTT %d",i);
if(j==1)
{
tmp=tmp*p;
//printf("\t>>>>>0");
}
else
{
tmp=tmp*(p+sta);
//printf("\t>>>>>%d",sta);
sta=sta*(-1);
k++;
}
if(k==2)
{
sta++;
k=0;
}
}
tmp = tmp/factorial(i);
//printf("\n Value of temp is %lf",tmp);
if(flag==1)
{
//printf("\n Value of t is %d",t);
t--;
tmp = tmp*L[t][i];
printf("\n******%d***************%lf",i,L[t][i]);
flag=0;
}
else
{
tmp = tmp*L[t][i];
flag=1;
printf("\n******%d***************%lf",i,L[t][i]);
}
//printf("\n Value of temp is %lf",tmp);
//printf("\n*****%d******%lf**********%lf",i,L[t][i],tmp);
//printf("\n***********%lf",tmp);
value +=tmp;
}
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-----------------------------Central 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.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");
}

Gauss Central Forward Interpolation

#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,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]);
//printf("\n %lf-----%lf",U[t+1][2],U[t][2]);
}
for(t=1;t<=ROW-i;t++)
U[t][2]=L[t][i];
}
differenceTable(A,L,ROW);
printf("\n Value at %+lf by Gauss Forward 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,flag=1,t,k,sta;
t=(ROW+1)/2;
double h,value=0,p,tmp=1.0;
h=A[2][1]-A[1][1];
//printf("\np is ***********%lf",h);
p=(x-A[t][1])/h;
//printf("\np is ***********%lf",p);
for(i=0;i<ROW;i++)
{
if(i==0)
{
value +=A[t][2];
//printf("\n*************%lf",value);
}
else
{
k=0;
sta=1;
for(j=1;j<=i;j++)
{
//printf("\nTTTTTTTTTTTTT %d",i);
if(j==1)
{
tmp=tmp*p;
//printf("\t>>>>>0");
}
else
{
tmp=tmp*(p-sta);
//printf("\t>>>>>%d",sta);
sta=sta*(-1);
k++;
}
if(k==2)
{
sta++;
k=0;
}
}
tmp = tmp/factorial(i);
//printf("\n Value of temp is %lf",tmp);
if(flag==1)
{
tmp = tmp*L[t][i];
flag=0;
}
else
{
t=t-1;
tmp = tmp*L[t][i];
flag=1;
}
//printf("\n Value of temp is %lf",tmp);
//printf("\n*****%d******%lf**********%lf",i,L[t][i],tmp);
//printf("\n***********%lf",tmp);
value +=tmp;
}
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-----------------------------Central 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.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");
}

Sunday, September 8, 2013

Simple Java Program to Demonstrate ArrayList and use of gc()

Inherit the STUDENT Class(Program here http://thecprojectt.blogspot.in/2013/09/simple-java-program-to-demonstrate.html)  and override the input method so as to input the department of each student. Search and display a sorted list of students one department or students based on scoring criteria. Create an arraylist of students and remove a student based on scoring criteria and then call gc() and check for free memory



import java.util.*;
public class ListStudent
    {
    public static void main(String[] args)
    {
      int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
int ch,k=0,n;
String d1;
boolean lp,fg=true;
Runtime r = Runtime.getRuntime();
    Scanner in = new Scanner(System.in);
    Course co = new Course();
    extendStudent[] students = new extendStudent[6];
    for(int i=0;i<6;i++)
    students[i]=new extendStudent();
    ArrayList<extendStudent> L = new ArrayList<extendStudent>();
    System.out.println("Courses available are Graduation, PG and PhD with 3,2 and 1 seats respectively:");
    String choice1="";
        do
    {
   
    System.out.println("\n Enter the course name:");
    String S = in.next();
    if(co.getCourse(S).equals("XX"))
    {
    System.out.println("Invalid course:");
    continue;
    }
    if(co.getSeat(S)>co.getSeatLimit(S))
    {
    System.out.println("Seats for the course "+S+" is exhausted:");
    continue;
    }
      System.out.println("Enter the Record for student :"+(k+1));
      students[k].setCourse(S);
    students[k].inputData();
    L.add(students[k]);
    k++;
    System.out.println(" Want to add another record yes/no:");
    choice1 = in.next();
    }while("YES".equalsIgnoreCase(choice1));
    n=k;
    System.out.println(" Intial List Size is: "+L.size());
    System.out.println("Press 1 to display Records of all STUDENTS:");
    System.out.println("Press 2 to see the result of all STUDENTS:");
    ch = in.nextInt();
    switch(ch)
    {
    case 1:
    for(int i=0;i<L.size();i++)
    L.get(i).displayAll();
    break;
    case 2:
        for(int i=0;i<L.size();i++)
        L.get(i).resultBuild();
        break;
        default:
        System.out.println("Wrong choice:");
        break;
        }
        System.out.println(" Intial List Size is: "+L.size());
        System.out.println(" Total free memory is: "+r.freeMemory()+" Bytes");
        System.out.println(" Want to search by Admission Date(dd/mm/yyyy)? yes/no:");
        String choice = in.next();
        if("YES".equalsIgnoreCase(choice))
        {
        int d=0,m=0,y=0;
         
          do {
fg=true;
int i=0;
int j=0;

String temp ="";
System.out.println("Enter the admission date(dd/mm/yyyy):");
d1=in.next();
while(d1.charAt(i)!='/' && i<d1.length())
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
fg=false;
break;
}
} //d extraction
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);
  if(fg==true && j<=2 && d1.charAt(i)=='/' && j >0 )
{
j=0;
d = Integer.valueOf(temp);
i=i+1;
temp="";
while(d1.charAt(i)!='/' && i<d1.length() )
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
//j++;
fg=false;
break;
}
} //m extraction
m = Integer.valueOf(temp);
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);

if(fg==true && j<=2 && d1.charAt(i)=='/' && j >0 && m <=12 && 1<=m)
{
j=0;
//m = Integer.valueOf(temp);
//System.out.println(d);
i=i+1;
temp="";
while(i<d1.length())
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
fg=false;
i++;
break;
//j++;

}
} //y extraction
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);
if(fg==true && j==4)
{
y = Integer.valueOf(temp);
fg=true;
}//end of else of y
else
fg=false;
if(fg==true)
{
if(y%400==0)
    lp=true;
else if (y%100==0)
    lp=false;
else if (y%4==0)
    lp=true;
else
    lp=false;
   
if(lp==false)
{
if(d>mdays[m-1] || d < 1 )
{
System.out.println("Invalid d of a m:");
fg=false;
}
}//non-lp y checking
else
{
if(d>ldays[m-1] || d < 1)
{
System.out.println("Invalid d of a m:");
fg=false;
}
} //lp y checking
} // checking for vaild date in months
}while(fg==false);
d1="";
d1=d+"/"+m+"/"+y;
for(int i=0;i<L.size();i++)
        {
        if(L.get(i).searchByAdmissionDate().equals(d1))
        L.get(i).displayAll();
        }
       
}
        System.out.println("Press 1 to display Records on basis of Departments:");
    System.out.println("Press 2 to display Records on basis of Scoring Criteria:");
    ch = in.nextInt();
    switch(ch)
    {
    case 1:
    System.out.println("Enter the Department name:");
    String dept = in.next();
    for(int i=0;i<L.size();i++)
        if(L.get(i).deptName().equals(dept))
        {
        System.out.println("Department name: "+dept+" Student Name: "+L.get(i).getName());
        }
    break;
    case 2:
    System.out.println("Enter the cut-off percentage:");
    double per = in.nextDouble();
    extendStudent S = new extendStudent();
    for(int i=0;i<n-1;i++)
    {
    for(int j=0;j<n-i-1;j++)
    {
    if(L.get(j).getPercentage()<L.get(j+1).getPercentage())
    {
    S=L.get(j);
    L.set(j,L.get(j+1));
    L.set(j+1,S);
    }
    }
    }
    System.out.println(" Sorted List is:");
    for(int i=0;i<n;i++)
    {
    System.out.println("Department name: "+L.get(i).deptName()+" Student Name: "+L.get(i).getName()+" Percentage Marks: "+L.get(i).getPercentage());
      }
   
    System.out.println("List after imposing the cut-off percentage:");
    for(int i=0;i<L.size();i++)
    {
    if(L.get(i).getPercentage()>=per)
      System.out.println("Department name: "+L.get(i).deptName()+" Student Name: "+L.get(i).getName()+" Percentage Marks: "+L.get(i).getPercentage());
    else
    {
    L.remove(i);
    r.gc();
    }
    }
   
    break;
    default:
    System.out.println("Wrong choice:");
    break;
    }
        in.close();
        System.out.println(" List Size after elimination is: "+L.size());
       
        System.out.println(" Total free memory after removing unwanted objects: "+r.freeMemory()+" Bytes");
    }
    }
class StudentRecord
{

StudentRecord()
{
roll=0;
name="";
co="";
for(int i:marks)
marks[i]=0;
}
public String searchByAdmissionDate()
{
return (dte);
}
public String getName()
{
return name;
}
public void resultBuild()
{
int sum=0;
System.out.println("----------------------------------------------------");
System.out.println("Name :"+name);
System.out.println("Course :"+co);
System.out.println("Roll Number :"+roll);
System.out.println("----------------------------------------------------");
for(int i=0;i<5;i++)
{
System.out.println("Subject "+(i+1)+" "+marks[i]);
sum +=marks[i];
}
System.out.println("Total Marks: "+sum);
System.out.println("Percentage: "+sum/5);
percentage= sum/5;
System.out.println("");
}
public void inputData()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name:");
name=in.nextLine();
System.out.println("Enter the roll number:");
roll=in.nextInt();
do {
flag=true;
int i=0;
int j=0;
String temp ="";
System.out.println("Enter the admission date(dd/mm/yyyy):");
dte=in.next();
while(dte.charAt(i)!='/' && i<dte.length())
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
flag=false;
break;
}
} //day extraction
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);
  if(flag==true && j<=2 && dte.charAt(i)=='/' && j >0 )
{
j=0;
day = Integer.valueOf(temp);
i=i+1;
temp="";
while(dte.charAt(i)!='/' && i<dte.length() )
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
//j++;
flag=false;
break;
}
} //month extraction
month = Integer.valueOf(temp);
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);

if(flag==true && j<=2 && dte.charAt(i)=='/' && j >0 && month <=12 && 1<=month)
{
j=0;
//month = Integer.valueOf(temp);
//System.out.println(day);
i=i+1;
temp="";
while(i<dte.length())
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
flag=false;
i++;
break;
//j++;

}
} //year extraction
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);
if(flag==true && j==4)
{
year = Integer.valueOf(temp);
flag=true;
}//end of else of year
else
flag=false;
if(flag==true)
{
if(year%400==0)
    leap=true;
else if (year%100==0)
    leap=false;
else if (year%4==0)
    leap=true;
else
    leap=false;
   
if(leap==false)
{
if(day>mdays[month-1] || day < 1 )
{
System.out.println("Invalid day of a month:");
flag=false;
}
}//non-leap year checking
else
{
if(day>ldays[month-1] || day < 1)
{
System.out.println("Invalid day of a month:");
flag=false;
}
} //leap year checking
} // checking for vaild date in months
}while(flag==false);
dte="";
dte=day+"/"+month+"/"+year;
System.out.println("Enter the marks:");
for(int i=0;i<5;i++)
{
System.out.println("Enter the marks in Subject:"+(i+1));
marks[i]=in.nextInt();
}
}
public void setCourse(String S)
{
co=S;
}
public void displayAll()
{
System.out.println("Roll No. = "+roll+" Name= "+name+" Course ="+co);
System.out.println("Admission date: "+dte);
for(int i=0;i<5;i++)
{
System.out.println("Marks in Subject "+(i+1)+"= "+marks[i]);
}
System.out.println("");
}
public double getPercentage()
{
return percentage;
}
private int roll;
private String name;
private String co;
private String dte;
private boolean flag=true;
private boolean leap;
private double percentage;
private int day;
private int month;
private int year;
int[] marks = new int[5];
int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
}
class extendStudent extends StudentRecord
{
public extendStudent()
{
super();
Department="";
}
public void inputData()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the department:");
Department = in.nextLine();
super.inputData();
}
public String deptName()
{
return Department;
}
public double getPercentage()
{
return super.getPercentage();
}

private String Department;
}
class Course
{
Course()
{
seats=new int[3];
seats[0]=0;
seats[1]=0;
seats[2]=0;
}
public String getCourse(String S)
{
for(int i=0;i<3;i++)
if(S.equalsIgnoreCase(course[i]))
{
seats[i]++;
return course[i];
}
return "XX";
}
public int getSeat(String S)
{
for(int i=0;i<3;i++)
{
if(S.equalsIgnoreCase(course[i]))
return seats[i];
}
return -9;
}
public int getSeatLimit(String S)
{
for(int i=0;i<3;i++)
{
if(S.equalsIgnoreCase(course[i]))
return fixSeat[i];
}
return -9;
}
private static String[] course = {"Graduation", "PG", "PhD"};
private static int[] fixSeat={3,2,1};
private int[] seats;
}