Saturday, November 1, 2008

Newtons Differentation.

void main()
{
int a[10][10],n,i,j,g,x,k;
float h,sum=0.0;
cout<<"Enter the value of n:";
cin>>n;
cout<<"Enter the values of X & Y in array:";
for(i=0;i<2;i++)
{
for(j=0;j{
cin>>a[j][i];
}
}

cout<<"Enter the common difference:";
cin>>h;

cout<<"Enter the value of X on which we have to find the solution:";
cinn>>x;

//make the forward difference table.
j=2;
g=n-1;
while(j < =n)
{
for(i=0; i < g;i++)
a[i][j]=a[i+1][j-1] - a[i][j-1];
j++; g--;
}


//searching that number in array at what position.
j=-1;
for(i=0; i < n; i++)
{
if( a[i][0]==x)
{
j++;
cout << j;
exit( 0 );
}
else
j++;
}

//now searching where it is in upper part or lower part and then computing it.

if( j < n/2)
{
k=n-j;
g=1;
for(i=j, l=2; l < =k; l++)
{
if(g%2==0)
{
sum - =(a[i][l])/g;
g++;
}
else
{
sum + = (a[i][l])/g;
g++;
}
}
}

else
{
g=1;
for(i=j-1, l=2; l < = j+1 ;l++, i--)
{
sum + = (a[i][l])/g;
g++;
}
}

sum=sum/h;

cout<<"Sum is:";
cout << sum;
getch();
}