Social Icons

Sunday, 4 May 2014

Monte Carlo Simulation

N=input('enter no of dats');
r=4;
c=0;
for i=1:N
    x=r*rand(1);
    y=r*rand(1);
    if((x^2+y^2)<=r^2)
        c=c+1;
       
    end;
end;
n=c;
pi=(4*n)/N

Script For inverse of a matrix in Matlab

x=input('Enter a matrix 3*3')
y=input('Enter another 3*3 matrix')
s=x+y
 det=s(1,1)*(s(2,2)*s(3,3)-s(2,3)*s(3,2))-s(1,2)*(s(2,1)*s(3,3)-s(3,1)*s(2,3))+s(1,3)*(s(2,1)*s(3,2)-s(3,1)*s(2,2))
adj=[[(s(2,2)*s(3,3)-s(3,2)*s(2,3));-(s(2,1)*s(3,3)-s(3,1)*s(2,3));(s(2,1)*s(3,2)-s(3,1)*s(2,2))] [-(s(1,2)*s(3,3)-s(3,2)*s(1,3));(s(1,1)*s(3,3)-s(3,1)*s(1,3));-(s(1,1)*s(3,2)-s(3,1)*s(1,2))] [(s(2,2)*s(1,3)-s(1,2)*s(2,3));-(s(1,1)*s(2,3)-s(2,1)*s(1,3));(s(1,1)*s(2,2)-s(2,1)*s(1,2))]]
inverse=(1./det).*adj

Saturday, 3 May 2014

Symbol Table Creation


To generate tokens
%%
{letter}({letter}|{digit})*       printf(“id: %s\n”, yytext);
%%
--------------------

1.
%{
#include<string.h>
%}
%%
(int|float|double|long|short|return|include|char) { printf("\n< Keyword,%s >",yytext);func(yytext,"Keyword");}
[\"] { printf("\n< Operator,%s >",yytext);func(yytext,"Operator"); }
['"'][\\a-zA-Z0-9|' ']+['"'] { printf("\n< id,%s >",yytext); func(yytext,"String");}

\+\+ { printf("\n< %s >",yytext);func(yytext,"Operator"); }
\-\- { printf("\n< %s >",yytext); func(yytext,"Operator");}
\=\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\!\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\+\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\-\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
[/|'.']+ { printf("\n< %s >",yytext); func(yytext,"Operator");}
(printf|scanf) { printf("\n< function,%s >",yytext);func(yytext,"Function");}

[0-99]+ { printf("\n<Number,%s >",yytext);func(yytext,"Number"); }
[a-zA-Z|_][0-99|a-zA-Z|_]* { printf("\n< variable,%s>",yytext); func(yytext,"variable");}
['+'|\-|'='|':'|'%'|'&'|'('|')'|'{'|'}'|','|'#'|'!'|'@'|'$'|'*'|'<'|'>'|'?'] { printf("\n< %s >",yytext);func(yytext,"Operator");}

[';']+ { printf("\n< %s >",yytext);func(yytext,"Operator");}
\n func2();
%%
struct entry
{
char name[10];
char token[10];
}st[10];
int i=0;
func(char n[10],char t[10])
{
int j,flag=0;
for(j=0;j<i;j++)
{
if(strcmp(n,st[j].name)==0)
flag=1;
}
if(flag==0)
{
strcpy(st[i].name,n);
strcpy(st[i].token,t);
i++;
}
}
func2()
{
int k;
printf("\n===========Symbol Table==================\n");
for(k=0;k<=i;k++)
printf("%s   %s\n",st[k].name,st[k].token);
}



Friday, 2 May 2014

Bouncing Sphere in Opengl

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
float x=2,y=0,d=0;
void display()
{      
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
   
   
     
      glPushMatrix();
      glTranslatef(0.0,-2.0+y,0.0);
      glRotatef(x,1.0,0.0,0.0);
      glutSolidSphere(0.5,30,40);
     
      glPopMatrix();
      glFlush();

 glutPostRedisplay();
 }
void reshape(GLint w, GLint h) {

  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
 
    glOrtho(-2.5, 2.5, -2.5/aspect, 2.5/aspect, -10.0, 10.0);
  } else {
 
    glOrtho(-2.5*aspect, 2.5*aspect, -2.5, 2.5, -10.0, 10.0);
  }

}



void timer(int v)
{
x=x+0.03;
     

 if(d==0)
  {glColor3f(1.0,0.0,0.0);
   y=y+0.03;
   if(y>4)
    d=1;
  }
   if(d==1)
  {
  glColor3f(1.0,1.0,0.0);
   y=y-0.03;
   if(y<-0.8)
    d=0;
  }


 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Cyan Shapes in Yellow Light");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);

  glutMainLoop();
}

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management