Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Saturday, April 1, 2017

Some common Numerical Method Programs for 3rd Semester BSc. CSIT students

comments 0 Blogger Comments
Pro tip: If you are searching for a speciic program, hit Ctrl+F to search faster. This is quite a long list.

1. Program to find Rootof a fuction by using Bisection method
/*program to find Rootof a fuction by using Bisection method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
#define E 0.00001
#define f(x) x*x-4*x-10
void main()
{
 clrscr();
   int count=0;
   float x0,x1,x2,f0,f1,f2;
   printf("Enter the value of x1\n");
   scanf("%f",&x1);
   printf("Enter the value of x2\n");
   scanf("%f",&x2);
   f1=f(x1);
   f2=f(x2);
   if (f1*f2>0)
   printf("Solution does not exit");
   else
   {
 begin:
 x0=(x1+x2)/2;
 f0=f(x0);
 if((f1*f0)<0)
  x2=x0;
      else
      {
 x1=x0;
  f1=f0;
      }
      if((fabs(x2-x1)/x1)<E)
      {
 x0=(x1+x2)/2;
  printf("\nRoor=%f",x0);
  printf("\nFuctional value =%f",count);
  printf("\nIteration steps =%d",count);
      }
      else
      {
 count ++;
  goto begin;
      }
   }
   getch();
}
  

2. Program to find Rootof a fuction by using Newton Raption method
/*program to find Rootof a fuction by using Newton Raption method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
#define E 0.0001
#define f(x)  x*x*x-3*x-2
#define fd(x)  3*x*x-3
void main()
{
    clrscr();
    int count=0;
    float x0,x1,fx,fdx;
    printf("\nEnter the initial value of x0\n");
    scanf("%f",&x0);
begin:
    printf("\nStep %d",count+1);
    fx=f(x0);
    printf("\nvalue of fx%d=%f",count,fx);
    fdx=fd(x0);
    printf("\nvalue of fdx%d= %f",count,fdx);
    x1=x0-(fx/fdx);
    if((fabs(x1-x0)/x1)<E)
    {
        printf("\nRoot=%f",x1);
        printf("\nThe fuction value=%f",f(x1));
        printf("\nNo. of Iritation steps=%d",count);
    }
    else
    {
        x0=x1;
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It takes more than 10 steps So solution does not coverse");
        }
    }
    getche();
}

  

2. Program to Rootof a fuction by using Newton Raption method (Alternate)
/*program to find Rootof a fuction by using Newton Raption method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
#define E 0.0001
#define f(x) x*x-3*x+2
#define fd(x) 2*x-3
void main()
{
    clrscr();
    int count=0;
    float x0,x1,fx,fdx;
    printf("\nEnter the initial value of x0");
    scanf("%f",&x0);
begin:
    fx=f(x0);
    fdx=fd(x0);
    x1=x0-(fx/fdx);
    if((fabs(x1-x0)/x1)<E)
    {
        printf("\nRoot=%f",x1);
        printf("\nThe fuction value=%f",f(x1));
        printf("\nNo. of Iritation steps=%d",count);
    }
    else
    {
        x0=x1;
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It takes more than 10 steps So solution does not coverse");
        }
    }
    getche();
}

  

3. Program to find Root of a fuction by using Secant method
/*program to find Root of a fuction by using Secant method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
#define E 0.0001
#define f(x) x*x-4*x-10
void main()
{
    clrscr();
    int count=0;
    float x1,x2,x3,f1,f2;
    printf("\nEnter the two value of x1 and x2\n");
    scanf("%f%f",&x1,&x2);
begin:
    f1=f(x1);
    f2=f(x2);
    x3=x2-f2*(x2-x1)/(f2-f1);
    if((fabs(x3-x2)/x2)<E)
    {
        printf("\nRoot=%f",x3);
        printf("\nNo. of Iteration steps=%d",count);
    }
    else
    {
        x1=x2;
        x2=x3;
        f1=f2;
        f2=f(x3);
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It solution does not coverge");
        }

    }
    getche();
}

  

4. Program to find Rootof a fuction by using Fixed point method
/*program to find Rootof a fuction by using Fixed point method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
#define E 0.001
#define g(x) exp(x)/3
void main()
{
    clrscr();
    int count=0;
    float x0,x1,error;
    printf("\nEnter the initial guess value of x0\n");
    scanf("%f",&x0);
begin:
    x1=g(x0);
    error=fabs(x1-x0);
    if(error<=E)
    {
        printf("\nThe root is %f",x1);
        printf("\nThe error value %f",error);
        printf("\nThe iteration steps is %d",count);
    }
    else
    {
        x0=x1;
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It solution does not coverge");
        }

    }
    getch();
}

  

5. Demonstrate Lagranges Interpolation
//Lagranges interpolation
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
void main()
{
    clrscr();
    int i,j,n;
    float x[MAX],f[MAX],sum=0,pro,xp,fp;
    printf("Enter the number of data points:\n");
    printf("\nn= ");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        printf("\nInput the data points for x[%d]&f[%d]",i,i);
        printf("\nx[%d]= ",i);
        scanf("%f",&x[i]);
        printf("\nf[%d]= ",i);
        scanf("%f",&f[i]);
    }
    printf("\nInput the specied value of x:");
    scanf("%f",&xp);
    for(i=0; i<=n; i++)
    {
        pro=1;
        for(j=0; j<=n; j++)
        {
            if(i!=j)
                pro=pro*(xp-x[j])/(x[i]-x[j]);
        }
        sum=sum+pro*f[i];
    }
    fp=sum;
    printf("\nThe required functional value at %f=%f",xp,fp);
    getch();
}

  

6. Demonstrate Newton Interpolation
// NEWTON INTERPOLATION
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 10
void main()
{
    int i,j,n;
    float xp,fp,pro,sum,a[MAX],f[MAX],x[MAX],d[MAX][MAX];
    printf("\nInput the number of data points:");
    printf("\nn=");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        printf("\nInput the data of x[%d]&f[%d]:",i,i);
        printf("\nx[%d]=",i);
        scanf("%f",&x[i]);
        printf("\nf[%d]=",i);
        scanf("%f",&f[i]);
    }
    for(i=1; i<=n; i++)
        d[i][1]=f[i];
    for(j=2; j<=n; j++)
        for(i=1; i<=n-j+1; i++)
            d[i][j]=(d[i+1][j-1]-d[i][j-1])/(x[i+j-1]-x[i]);
    for(j=1; j<=n; j++)
        a[j]=d[1][j];
    printf("\nInput the value of xp point:");
    printf("\nxp=");
    scanf("%f",&xp);
    sum=a[1];
    for(i=2; i<=n; i++)
    {
        pro= 1.0;
        for(j=1; j<=i-1; j++)
            pro=pro*(xp-x[j]);
        sum=sum+a[i]*pro;
    }
    fp=sum;
    printf("\n At xp=%f is %f",xp,fp);
    getch();
}
  

7. Demonstrate Least Square Method
// Least square method
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 10
void main()
{
    int i,n;
    float a0,a1;
    float x[MAX],y[MAX],sumx=0.0,sumy=0.0,sumxy=0.0,sumxsq=0.0;
    printf("***Least square method***" );
    printf("\nInput the number of data points:");
    printf("\nn=");
    scanf("%d",&n);
    printf("Enter the data sets one after another:");
    for(i=0; i<n; i++)
    {
        // printf("\nInput the data of x[%d]&f[%d]:",i,i);
        printf("\nx[%d]=",i);
        scanf("%f",&x[i]);
        printf("\ny[%d]=",i);
        scanf("%f",&y[i]);
        sumx+=x[i];
        sumy+=y[i];
        sumxy+=x[i]*y[i];
        sumxsq+=x[i]*x[i];
    }
    a0=(sumy*sumxsq-sumx*sumxy)/(n*sumxsq-pow(sumx,2));
    a1=(n*sumxy-sumx*sumy)/(n*sumxsq-pow(sumx,2));
    printf("The equation of line:\n");
    printf("\n y=%fx+%f",a1,a0);
    getch();
}
  

8. Program to find Integration of given function by using Trapezoidal rule
/*program to find Integration of given funtion by using Trapezoidal rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x+1
void main()
{
    clrscr();
    float a,b,h,It;
    printf("\nEnter initial value of X:");
    scanf("%f",&a);
    printf("\nEnter  Final  value of X:");
    scanf("%f",&b);
    h=(b-a)/2;
    It =h*(f(a)+f(b));
    printf("\nThe integration value of function :%f",It);
    getch();
}

  

9. Program to find Integration of given tabulated data using Composite Trapezoidal rule*
/*program to find Integration of given tabulated data using  Composit Trapezoidal rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 15
void main()
{
    clrscr();
    int n,n1,n2,i;
    float a,b,h,sum,ict,x[max],y[max];
    printf("\nEnter the number of data points:");
    printf("\nn=");
    scanf("%d",&n);
    printf("\nInput the values set by set");
    for(i=1; i<=n; i++)
    {
        printf("\nX%i=",i);
        scanf("%f",&x[i]);
        printf("\nF(x%i)=",i);
        scanf("%f",&y[i]);
    }
    printf("\nInput the initial value.");
    printf("\na=");
    scanf("%f",&a);
    printf("\nInput the final value.");
    printf("\nb=");
    scanf("%f",&b);
    h=x[2]-x[1];
    n1= (int)(fabs(a-x[1])/h+1.5);
    n2= (int)(fabs(b-x[1])/h+1.5) ;
    sum=0.0;
    for(i=n1; i<=n2; i++)
        sum =sum+y[i]+y[i+1];
    ict= sum*h/2.0;
    printf("\nIntegral from %f to %f is %f\n",a,b,ict);
    getche();
}
  

10. Program to find Integration of given funtion by using Composite Trapezoidal rule
/*program to find Integration of given funtion by using composit Trapezoidal rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    clrscr();
    int i,n;
    float a,b,h,sum,Ict;
    float F(float x);
    printf("Enter the initial value of a:");
    scanf("%f",&a);
    printf("\nEnter the Final  value of b: ");
    scanf("%f",&b);
    printf("\nEnter the segment width:");
    scanf("%f",&h);
    n=(b-a)/h;
    sum =(F(a)+F(b))/2.0;
    for(i=1; i<n-1; i++)
    {
        sum= sum +F(a+i*h);
    }
    Ict=sum*h;
    printf("\nThe integration between %f and %f at h=%f is %f",a,b,h,Ict);
    getch();
}
float F(float x)
{
    float f;
    f=1-exp(-x/2.0) ;
    return (f);
}

  

11. Program to find Integration of given funtion by using Simson's 1/3 rule
/*program to find Integration of given funtion by using Simson's 1/3 rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) 1-exp(-(x)/2.0)
void main()
{
 clrscr();
   int n,m,i;
   float a,b,h,sum,Ics,x,f1,f2,f3;
   printf("Enter initial value of X:");
   printf("\na=");scanf("%f",&a);
   printf("\nEnter  Final  value of X:");
   printf("\nb=");scanf("%f",&b);
   printf("\nEnter number of segments (Even number):");
   printf("\nN=");scanf("%d",&n);
   h=(b-a)/n;
   m=n/2;
   sum =0.0;
   x=a;
   f1=F(x);
   for(i=1;i<m;i++)
   {
    f2=F(x+h);
      f3=F(x+2*h);
      sum=sum+f1+4*f2+f3;
      f1=f3;
      x=x+2*h;
   }
   Ics=sum *h/3.0;
   printf("\nIntegral from %f to %f\n",a,b);
   printf("When h=%f is %f \n",h,Ics);
   getch();
}
  

12. Program to find Integration of given funtion by using Simson's 1/8 rule
/*program to find Integration of given funtion by using Simson's 1/8 rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) (x*x*x+1)
void main()
{
    clrscr();
    float a,b,h,Is;
    printf("\nEnter initial value of X:");
    scanf("%f",&a);
    printf("\nEnter  Final  value of X:");
    scanf("%f",&b);
    h=(b-a)/3;
    Is=3*h*(F(a)+3*F(a+h)+3*F(a+2*h)+F(b))/8;
    printf("\nIntegral from %f to %f\n",a,b);
    printf("Integration is %f \n",Is);
    getch();
}

  

13. Program to solve Integration equation by using Gauss Integration Method for n=2 interpolating point
/*program to solve Integration equation by using gauss integration method for
n=2 interpolating point*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) exp(x)
void main()
{
    clrscr();
    int n=2,i;
    float a,b,w[2],z[2],x[2],Ig =0.0;
    printf("\nInput initial value of x:");
    printf("\na=");
    scanf("%f",&a);
    printf("\nInput final value of x:");
    printf("\nb=");
    scanf("%f",&b);
    w[0]=1.0;
    w[1]=1.0;
    z[0]=-0.5773502;
    z[1]=0.5773502;
    for (i=0; i<n; i++)
        x[i]=((b-a)/2)* z[i]+((b+a)/2);
    for(i=0; i<n; i++)
        Ig +=((b-a)/2)*(w[i]*f(z[i]));
    printf("\nIntegral from %f to %f is %f\n",a,b,Ig);
    getche();
}

  

14. Program to solve Integration equation by using gauss integration method for n=3 interpolating point
/*program to solve Integration equation by using gauss integration method for
n=3 interpolating point*/
#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
#include&lt;math.h&gt;
#define f(x) exp(x)
void main()
{
    clrscr();
    int n=3,i;
    float a,b,w[3],z[3],x[3],Ig =0.0000;
    printf("\nInput initial value of x:");
    printf("\na=");
    scanf("%f",&amp;a);
    printf("\nInput final value of x:");
    printf("\nb=");
    scanf("%f",&amp;b);
    w[0]=0.55556;
    w[1]=0.88889;
    w[2]=0.55556;
    z[0]=-0.77460;
    z[1]=0.00;
    z[2]=0.77460;
    for (i=0; i&lt;n; i++)
        x[i]=((b-a)/2)* z[i]+((b+a)/2);
    for(i=0; i&lt;n; i++)
        Ig +=((b-a)/2)*(w[i]*f(z[i]));
    printf("\nIntegral from %f to %f is %f\n",a,b,Ig);
    getche();
}
  

15. Demonstrate Euler Method
//Eluer method
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define f(x,y) 2*y/x
void main()
{
    clrscr();
    int i,n;
    float x,y,h,xp,dy;
    printf("Input initial value of x and y");
    scanf ("%f%f",&x,&y);
    printf("Input X-value at which Y is required :\n");
    scanf("%f",&xp);
    printf("Input  step size:\n");
    scanf("%f",&h);
    n=((xp-x)/h+0.5);
    for (i=1; i<=n; i++)
    {
        dy=h*f(x,y);
        x=x+h;
        y=y+dy;
        printf("x=%f and y=%f\n",x,y);
    }
    printf("\n Value of y at x =%f is %f",x,y);
    getche();
}

  

16. Demonstrate Heuns Method
// Heuns method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 15
void main()
{
    clrscr();
    int n,i;
    float x,y,xp,h,m1,m2;
    float func(float,float);
    printf("\n***Solution of Heuns method***");
    printf("\nEnter the initial value of x \n");
    scanf("%f",&x);
    printf("\nEnter the initial value of y \n");
    scanf("%f",&y);
    printf("\nInput x at which y is required");
    scanf("%f",&xp);
    printf("\nInput step size");
    scanf("%f",&h);
    n=(int)((xp-x)/h+0.5);
    for(i=1; i<=n; i++)
    {
        m1=func(x,y);
        m2=func(x+h,y+m1*h);
        x=x+h;
        y=y+0.5*h*(m1+m2);
        printf("%5d %10.6f %10.6f\n",i,x,y);
    }
    printf("\nThe value of y at x=%f is %f\n",x,y);
    getch();
}
float func(float x,float y)
{
    float f;
    f=2.0*y/x;
    return(f);
}

  

17. Demonstrate Runge-Kutta Method
#include <stdio.h>
#include <conio.h>
#include <math.h>
//#define f(x,y) (2.0*y/x)
void main()
{
  clrscr();
  int n,i;
  float x,y,xp,h;
  float m1,m2,m3,m4;
  float func(float , float );
  printf("Input initial value of x and y");
  scanf ("%f%f",&x,&y);
  printf("Input X-value at which Y is required :\n");
  scanf("%f",&xp);
  printf("Input  step size:\n");
  scanf("%f",&h);
  n=(int)((xp-x)/h+0.5);
  for (i=1;i<=n;i++)
  {
    m1=func(x,y);
      m2=func(x+0.5*h,y+0.5+m1*h);
      m3=func(x+0.5*h,y+0.5+m2*h);
      m4=func(x+h,y+m3*h);
      x=x+h;
      y=y+(m1+2.0*m2+2.0*m3+m4)*h/6.0;
      printf("x=%f and y=%f\n",x,y);
  }
  printf("\n Value of y at x=%f is %f",x,y);
    getche();
}
float func(float x, float y)
  {
    float f;
      f=x*x+y*y;
      return(f);
  }
  

18. Runge-Kutta (Alternate)
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define f(x,y) 2.0*y/x
void main()
{
    clrscr();
    int i,n;
    float x,y,xp,h;
    float m1,m2,m3,m4;
    printf("Input initial value of x and y");
    scanf ("%f%f",&x,&y);
    printf("Input X-value at which Y is required :\n");
    scanf("%f",&xp);
    printf("Input  step size:\n");
    scanf("%f",&h);
    n=(int)((xp-x)/h+0.5);
    for (i=1; i<=n; i++)
    {
        m1=f(x,y);
        m2=f(x+0.5*h,y+(m1*h*0.5));
        m3=f(x+0.5*h,y+(m2*h*0.5));
        m4=f(x+h,y+m3*h);
        x=x+h;
        y=y+((m1+2.0*m2+2.0*m3+m4)*h)/6.0;
        printf("x=%f and y=%f\n",x,y);
    }
    printf("\n Value of y at xp =%f is %f",x,y);
    getche();
}

  

Read More

Program to find Root of a fuction by using Newton Raphson method

comments 0 Blogger Comments

Program to find Root of a fuction by using Newton Raphson method
/*program to find Rootof a fuction by using Newton Raphson method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
#define E 0.0001
#define f(x)  x*x*x-3*x-2
#define fd(x)  3*x*x-3
void main()
{
    clrscr();
    int count=0;
    float x0,x1,fx,fdx;
    printf("\nEnter the initial value of x0\n");
    scanf("%f",&x0);
begin:
    printf("\nStep %d",count+1);
    fx=f(x0);
    printf("\nvalue of fx%d=%f",count,fx);
    fdx=fd(x0);
    printf("\nvalue of fdx%d= %f",count,fdx);
    x1=x0-(fx/fdx);
    if((fabs(x1-x0)/x1)<E)
    {
        printf("\nRoot=%f",x1);
        printf("\nThe fuction value=%f",f(x1));
        printf("\nNo. of Iritation steps=%d",count);
    }
    else
    {
        x0=x1;
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It takes more than 10 steps So solution does not coverse");
        }
    }
    getche();
}

  

Read More

Program to find Root of a fuction by using Bisection method

comments 0 Blogger Comments

Program to find Rootof a fuction by using Bisection method
/*program to find Rootof a fuction by using Bisection method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
#define E 0.00001
#define f(x) x*x-4*x-10
void main()
{
  clrscr();
   int count=0;
   float x0,x1,x2,f0,f1,f2;
   printf("Enter the value of x1\n");
   scanf("%f",&x1);
   printf("Enter the value of x2\n");
   scanf("%f",&x2);
   f1=f(x1);
   f2=f(x2);
   if (f1*f2>0)
   printf("Solution does not exit");
   else
   {
  begin:
  x0=(x1+x2)/2;
  f0=f(x0);
  if((f1*f0)<0)
    x2=x0;
      else
      {
  x1=x0;
   f1=f0;
      }
      if((fabs(x2-x1)/x1)<E)
      {
  x0=(x1+x2)/2;
   printf("\nRoor=%f",x0);
   printf("\nFuctional value =%f",count);
   printf("\nIteration steps =%d",count);
      }
      else
      {
  count ++;
   goto begin;
      }
   }
   getch();
}
  

Read More

Friday, September 2, 2016

Implement DFA that accepts only 'int' or 'integer'

comments 0 Blogger Comments
This is a program in Java which maps the DFA for input string where the string must be 'int' or 'integer' to be accepted. It is also an excellent example for DFAs with deadends. Since definition of DFA is a trivial topic for this question, any pre-requisite information about DFA that you might need is available at this site.

Procedure:
  1. Make a state diagram for DFA accepting 'int' or 'integer'

    Edit: I seem to have made a small mistake while making the state diagram. q3 and q7 are actually FINAL STATES and those two states should have double circle instead of single one :)

  2. Trace a state-table with information from (1)
    i n t e g r
    q0 q1 q8 q8 q8 q8 q8
    q1 q8 q2 q8 q8 q8 q8
    q2 q8 q8 q3 q8 q8 q8
    *q3 q8 q8 q8 q4 q8 q8
    q4 q8 q8 q8 q8 q5 q8
    q5 q8 q8 q8 q6 q8 q8
    q6 q8 q8 q8 q8 q8 q7
    *q7 q8 q8 q8 q8 q8 q8
    q8 q8 q8 q8 q8 q8 q8
  3. Write a program to emulate the behavior of given DFA with the rules provided by (2)

SOURCE CODE:

public class DFAwithdeadend {
    public static void main(String[] args) {
        // Implement DFA that accepts 'int' or 'integer'
        int count;
        String state_now="q0",
               final_state1="q3",final_state2="q7";
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter a string to test:");
        String string= sc.nextLine();
        HashMap i = new HashMap();
        HashMap n = new HashMap();
        HashMap t = new HashMap();
        HashMap e = new HashMap();
        HashMap g = new HashMap();
        HashMap r = new HashMap();
        HashMap deadend = new HashMap();
        i.put("q0","q1");
        n.put("q1","q2");
        t.put("q2","q3");
        e.put("q3","q4");
        g.put("q4","q5");
        e.put("q5","q6");
        r.put("q6","q7");
        for(count=0;count<=8;count++){
            String rem_state="q"+count;
            if (count!=0)
                i.put(rem_state,"q8");
            if (count!=1)
                n.put(rem_state,"q8");
            if (count!=2)
                t.put(rem_state,"q8");
            if (count!=3 && count!=5)
                e.put(rem_state,"q8");
            if (count!=4)
                g.put(rem_state,"q8");
            if (count!=6)
                r.put(rem_state,"q8");
            deadend.put(rem_state, "q8");
        }
        System.out.print("Tracing DFA..\nstart:q0");
        for(count=0;count<string.length();count++){
            if(string.charAt(count)=='i'){
              state_now=(String)i.get(state_now);
            }
            else if(string.charAt(count)=='n'){
                state_now=(String)n.get(state_now);
            }
            else if(string.charAt(count)=='t'){
                state_now=(String)t.get(state_now);
            }
            else if(string.charAt(count)=='e'){
                state_now=(String)e.get(state_now);
            }
            else if(string.charAt(count)=='g'){
                state_now=(String)g.get(state_now);
            }
            else if(string.charAt(count)=='r'){
                state_now=(String)r.get(state_now);
            }
            else{
                state_now=(String)deadend.get(state_now);
            }
            System.out.print("-->"+state_now); 
        }
        if(state_now.equals(final_state1)||state_now.equals(final_state2)){
            System.out.println("\nConclusion: This string is accepted in DFA");
        }
        else{
            System.out.println("\nConclusion: This string is not accepted in DFA");
        }
    }
}

OUTPUT:
when string is accepted in DFA:


when string is rejected in DFA:

If you have any doubt, please feel free to inquire.

Read More

Wednesday, August 31, 2016

DFA to accept string with even a and even b

comments 0 Blogger Comments

Procedure:
1. Make a state diagram for DFA having even a and even b

2. Trace a state-table with information from (1)
3. Write a program to emulate the behavior of given DFA with the rules provided by (2)
If you need further elaboration in this topic^(i.e. Procedure), please drop a comment and we'll post DFA state-diagram and state-table also.
public class DfaEvenAB {
    public static void main(String[] args) {
        //Implementing DFA with even a and b
        String state="q0";
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the String:");
       String exp=sc.nextLine();
       HashMap a = new HashMap();
       HashMap b = new HashMap();
       a.put("q0","q1");
       a.put("q1","q0");
       a.put("q2","q3");
       a.put("q3","q2");
       b.put("q0","q3");
       b.put("q1","q2");
       b.put("q2","q1");
       b.put("q3","q0");
       
        System.out.print("Tracing DFA:\nstart=q0");
        for(int i=0;i<exp.length();i++){
            String str=""+exp.charAt(i);
            if(str.equals("a")){
                state=(String)a.get(state);
            }else if(str.equals("b")){
                state=(String)b.get(state);
            }
            System.out.print("-->"+state);
        }
        if(state.equals("q0")){
            System.out.println("\nString Accepted");
        }else{
            System.out.println("\nString not Accepted. Try another String.");
        }
    }
}

Output:


When String is Accepted:

When String is not accepted:
Read More

Tuesday, August 30, 2016

Implement DFA ending with 'abb'

comments 0 Blogger Comments
This is a program in Java which maps the DFA for input string where the ending substring must be 'abb' to be accepted. Since definition of DFA is a trivial topic for this question, any pre-requisite information about DFA that you might need is available at this site.

Procedure:

  1. Make a state diagram for DFA ending with abb
  2. Trace a state-table with information from (1)
  3. Write a program to emulate the behavior of given DFA with the rules provided by (2)
If you need further elaboration in this topic^(i.e. Procedure), please drop a comment and we'll post DFA state-diagram and state-table also.


SOURCE CODE:

public class DfaFinder {
    public static void main(String[] args) {
        // Implement DFA ending with abb
        String state_now="q0";
        Scanner sc= new Scanner(System.in);
        String string= sc.nextLine();
        HashMap a = new HashMap();
        HashMap b = new HashMap();
        a.put("q0","q1");
        a.put("q1","q1");
        a.put("q2","q1");
        a.put("q3","q1");
        b.put("q0","q0");
        b.put("q1","q2");
        b.put("q2","q3");
        b.put("q3","q0");
        System.out.print("Tracing DFA..\nstart:q0");
        for(int i=0;i<string.length();i++){
            if(string.charAt(i)=='a'){
              state_now=(String)a.get(state_now);
            }
            else if(string.charAt(i)=='b'){
                state_now=(String)b.get(state_now);
            }
            System.out.print("-->"+state_now); 
        }
        if(state_now.equals("q3")){
            System.out.println("\nConclusion: This string is accepted in DFA");
        }
        else{
            System.out.println("\nConclusion: This string is not accepted in DFA");
        }
    }
}

OUTPUT:
when string is accepted in DFA:


when string is rejected in DFA:


If you have any doubt, please feel free to inquire.

Read More

Wednesday, August 24, 2016

Fix: HAX Kernel Module is not installed

comments 0 Blogger Comments
 
As versatile and easy-to-use Android studio is, it has a set of its own petty little problems that sometimes end up taking a lot of time to fix. This is because all the fixes are available in documentations, changelogs or, special forums which may not be easily found. One such problem is the "HAX Kernel Module not installed" message that keeps popping when you try to use that emulator you were sure that you built your app to emulate. 
 
Many of us have faced this problem while using Android Studio at one point or the other. This is a definite fix to that problem as cited on one of stackoverflow questions: 

If you are running a modern Intel processor make sure HAXM (Intel® Hardware Accelerated Execution Manager) is installed:
  1. In Android SDK Manager, ensure the option is ticked (and then installed)
  2. Run the HAXM installer via the path below:
    1. your_sdk_folder\extras\intel\Hardware_Accelerated_Execution_Manager\intelhaxm.exe 
    2. or 
    3. your_sdk_folder\extras\intel\Hardware_Accelerated_Execution_Manager\intelhaxm-android.exe
For AMD CPUs (or older Intel CPUs without VT-x technology), you will not be able to install this and the best option is to emulate your apps using Genymotion. See: Intel's HAXM equivalent for AMD on Windows OS

Here is a helpful video to solve this problem: 
;


Read More

Most read this week!