Friday, August 22, 2014

C Code for Newton's Forward Difference Interpolation

#include<stdio.h>
#include<math.h>
int main()
{
  float x[25], y[25], ques, ans, a, b, h;
  int i, j , num;

  printf("Enter the Number of elements:");
  scanf("%d",&num);
  printf("Enter the elements of x:");
  for(i=1 ;i<=num; i++)
   {
        scanf("%f",&x[i]);
   }
   printf("Enter the elements of y:");
   for(i=1;i<=num;i++)
   {
    scanf("%f",&y[i]);
   }

  h=x[2]-x[1];
  printf("Enter the value you want to find: ");
  scanf("%f", &ques);
  a=(ques-x[1])/h;
  b=1;
  ans=y[1];

    for(i=1; i<=(num-1); i++)
    {
       for(j=1; j<=(num-i); j++)
        {
              y[j]=y[j+1]-y[j];
        }
        b=b*(a-i+1)/i;
        ans=ans+b*y[1];
    }
   printf("Answer is %6.5f", ans);
   return 0;
}

C Code for Lagrange's Interpolation Formula

#include<stdio.h>
#include<math.h>

int main()
{
  float x[10],y[10], temp, ans[10], sum, point;
  int i,j,k=0,num;

  printf("Enter your points : ");
  scanf("%d",&num);

  for(i=0; i<num; i++)
  {
    printf("x%d is: ", i);
    scanf("%f",&x[i]);
    printf("y%d is: ", i);
    scanf("%f",&y[i]);
  }

  printf("Enter your interpolation number: ");
  scanf("%f",&point);

  for(i=0;i<num;i++)
  {
    temp = 1;
    k = i;
    for(j=0;j<num;j++)
    {
      if(k==j)
      {
        continue;
      }
      else
      {
        temp = temp * ((point-x[j])/(x[k]-x[j]));
      }
    }
    ans[i]=y[i]*temp;
  }

  for(i=0;i<num;i++)
  {
     sum = sum + ans[i];
  }

  printf("f(%.1f) = %f ", point, sum);
  return 0;
}

Monday, June 9, 2014

UVa Solution 900 - Brick Wall Patterns

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long a[105], i;

void fib()
{
    a[0]=1;
    a[1]=1;
    for(i=2; i<51; i++)
    {
        a[i]= a[i-1]+a[i-2];
    }
    return;
}

int main()
{
    fib();
    long long n;
    while(scanf("%lld", &n)==1)
    {
        if(n==0)
        {
            break;
        }
        printf("%lld\n", a[n]);
    }
    return 0;
}

UVa Solution 11715 - Car

#include<iostream>
#include<cstdio>
#include<cmath>
int main()
{
    double u, v, s, t, a;
    int test=1, n;
    while(scanf("%d", &n)==1)
    {
        if(n==0)
        {
            break;
        }
        if(n==1)
        {
            scanf("%lf%lf%lf", &u, &v, &t);
            s=(u+v)*t/2;
            a=(v-u)/t;
            printf("Case %d: %0.3lf %0.3lf\n", test, s, a);
        }
        else if(n==2)
        {
            scanf("%lf%lf%lf", &u, &v, &a);
            t=(v-u)/a;
            s=(u+v)*t/2;
            printf("Case %d: %0.3lf %0.3lf\n", test, s, t);
        }
        else if(n==3)
        {
            scanf("%lf%lf%lf", &u, &a, &s);
            v=sqrt((u*u)+(2*a*s));
            t=(v-u)/a;
            printf("Case %d: %0.3lf %0.3lf\n", test, v, t);
        }
        else if(n==4)
        {
            scanf("%lf%lf%lf", &v, &a, &s);
            u=sqrt((v*v)-(2*a*s));
            t=(v-u)/a;
            printf("Case %d: %0.3lf %0.3lf\n", test, u, t);
        }
        test++;
    }
    return 0;
}

Saturday, April 12, 2014

UVa Solution 11000 - Bee


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int main()
{
    long long n, a, b, fib, arr[105];
    while(scanf("%lld", &n)==1)
    {
        if(n==-1)
        {
            break;
        }
        a=1;
        b=1;
        for(int i=0; i<=n; i++)
        {
            fib=a+b;
            arr[i]=fib-1;
            a=b;
            b=fib;
        }
        printf("%lld %lld\n", arr[n-1], arr[n]);
    }
    return 0;
}

Friday, April 11, 2014

UVa Solution 12592 - Slogan Learning of Princess


#include<iostream>
#include<cstdio>
#include<map>
#include<sstream>
#include<cstring>
#include<istream>
using namespace std;
map<string, string>M;

int main()
{
    string s1, s2;
    int test, n;
    cin >> test;
    cin.ignore();
    while(test--)
    {
        getline(cin, s1);
        getline(cin, s2);
        M[s1] = s2;
    }
    cin>>n;
    cin.ignore();
    while(n--)
    {
        string s3;
        getline(cin, s3);
        cout << M[s3] <<endl;
    }
    return 0;
}

Wednesday, April 9, 2014

UVa Solution 10282 - Babelfish

#include<iostream>
#include<cstdio>
#include<map>
#include<sstream>
using namespace std;
#define MAX 1005
map<string, string>M;
char line[MAX];

int main()
{
    string s1, s2;
    while(gets(line))
    {
        if(line[0]=='\0') break;
        stringstream ss(line);
        ss >> s1;
        ss >> s2;
        M[s2] = s1;
    }
    string s3;
    while(cin >> s3)
    {
        if(M[s3].length()==0)
        {
          cout << "eh" << endl;
        }
        else
        {
          cout << M[s3] <<endl;
        }
    }
    return 0;
}

Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...