Amazon Affiliate

Tuesday 17 April 2012

PROGRAM FOR NEWTON RAPHSON METHOD IN C..


//Program for newton raphson method in c.....

include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x1,x2,xn,fx1,fx2,fxn,dx2,dxn,con=-99;
int i=0;
clrscr();

printf("enter the value of x1:");
scanf("%f",&x1);

printf("enter the value of x2:");
scanf("%f",&x2);

fx1=pow(x1,3)+3*x1*x1-3;
fx2=pow(x2,3)+3*x2*x2-3;
printf("fx1 : %.4f\nfx2 : %.4f\n",fx1,fx2);

if(x1>x2)
dx2=3*x2*x2+6*x2;
else
{
dx2=3*x1*x1+6*x1;
x2=x1;
fx2=fx1;
}

while(fabs(con-x2)>.0001)
{
i=i+1;
if(i>15)
goto last;
con=x2;
xn=x2-(fx2/dx2);
fxn=pow(xn,3)+3*xn*xn-3;
dxn=3*xn*xn+6*xn;
printf("x%d : %f\tfx%d : %f\n",i+1,xn,i+1,fxn);
x2=xn;
fx2=fxn;
dx2=dxn;
}
last:
printf("Root of equation : %f",xn);
getch();
}

PROGRAM TO ADD,SUBTRACT,MULTIPICATIONS AND TRASPOSE OF TWO MATRICES USING FUNCTIONS..


/*Program To Add,Subtract,Multipications and Traspose of Two Matrices using functions.. */

#include <stdio.h>
#include <conio.h>

#include <math.h>


void add(int a[3][3],int b[3][3]);
void subt(int c[3][3],int d[3][3]);
void mult(int e[3][3],int f[3][3]);
void tranp(int g[3][3]);

void main()
{
int i,j,opt;
int x[3][3],y[3][3];
clrscr();
printf("This Program is Used to Perform ADDITION,SUBTRACTION,MULTIPICATION and TRANSPOSE on Two Matrices using functions\n");
printf("\n\tEnter The Value Of First Matrix:");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
scanf("%d",&x[i][j]);
}
}

printf("\nEnter The Value Of Second Matrix:");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
scanf("%d",&y[i][j]);
}
}



printf("\nSelect One Option given below:\n");
printf("\n\t1.ADDITION");
printf("\n\t2.SUBTRACTION");
printf("\n\t3.MULTIPICATION");
printf("\n\t4.TRANSPOSE");
printf("\n\tEnter Your value:");
scanf("%d",&opt);

switch(opt)
{
case 1:
add(x,y);
break;
case 2:
subt(x,y);
break;
case 3:
mult(x,y);
break;
case 4:
tranp(x);
break;
}
getch();
}
void add(int a[3][3],int b[3][3])
{
int z[3][3];
int i,j;
printf("\n");
for(i=1;i<=3;i++)
{

for(j=1;j<=3;j++)
{
z[i][j]=a[i][j]+b[i][j];
printf("\t %d",z[i][j]);
}
printf("\n");
}
}







void subt(int c[3][3],int d[3][3])
{
int z[3][3];
int i,j;
printf("\n");
for(i=1;i<=3;i++)
{

for(j=1;j<=3;j++)
{
z[i][j]=c[i][j]-d[i][j];
printf("\t %d",z[i][j]);
}
printf("\n");
}
}

void mult(int e[3][3],int f[3][3])
{
int z[3][3];
int i,j,k;
printf("\n");
for(i=1;i<=3;i++)
{

for(j=1;j<=3;j++)
{
z[i][j]=0;
for(k=1;k<=3;k++)
{

z[i][j]=e[i][k]*f[k][j]+z[i][j];
printf("\t %d",z[i][j]);
}
}
printf("\n");
}
}

void tranp(int g[3][3])
{
int z[3][3];
int i,j;




printf("\n");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
z[i][j]=g[j][i];
printf("\t %d",z[i][j]);
}
printf("\n");
}
}

PROGRAM FOR NEWTON BACKWARD INTERPOLATION METHOD..

/program for newton backward interpolation formula





#include
#include
#include
#define maxn 100
#define order 4
void main()
{
float ax[maxn+1],ay[maxn+1],diff[maxn+1][order+1],nr=1,dr=1,x,p,h,yp;
int n,i,j,k;
clrscr();
cout<<"enter the value of n\n";
cin>>n;
cout<<"enter the values in form x,y\n";
for(i=1;i<=n;i++)
{cin>>ax[i]>>ay[i];}
cout<<"enter the value of x for which value of y is wanted\n";
cin>>x;
h=ax[2]-ax[1];
for(i=n;i>=1;i--)
{diff[i][1]=ay[i]-ay[i-1];}
for(j=2;j<=order;j++)
{
for(i=n;i>j;i--)
{diff[i][j]=diff[i][j-1]-diff[i-1][j-1];}
}
i=n;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=order;k++)
{
nr*=p+k-1;
dr*=k;
yp+=(nr/dr)*diff[i][k];
}
cout<<"when x = "<<X<<" padding-top:2px;? width:700px; height:21px; y="<<yp;
getch();
}

PROGRAM THAT USES PUSH AND POP METHOD OF STACK CLASS IN C++..

Program that uses push and pop method of stack class in c++.....

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<graphics.h>
#define MAX 5
class stack
{
  int top,i;
  int arr[MAX],x;
  char ch;
  public :
       stack();//DEFUALT ZERO CONSTRUCTOR
  void creat();
  void push();
  void pop();
  void deewan();
  void outof();
};
    stack::stack()//DEFINE CONSTRUCTOR
    {
     top=-1; //INITILISE THE STACK POINTER
    }
    void stack::deewan()
 {
    clrscr();
     while(1)
    {
    cleardevice();//CLEAR THE GRAPHICS SCREEN
    gotoxy(4,3);
    cout<<"::GIVEN FUNCTIONS::==> ";
    gotoxy(27,6);
    cout<<"C :: CREAT A NEW FRESH STACK::::::::";
    gotoxy(27,8);
    cout<<"P :: PUSH OPRATION PERFORM ON STACK::";
    gotoxy(27,10);
    cout<<"O :: POP OPRATION PERFORM ON STACK:::";
    gotoxy(27,12);
    cout<<"E : EXIT:::::::::::::::::::::::::::::";
    gotoxy(3,25);
    cout<<"ENTER YOUR RIGHT CHOICE :-> ";
    cin>>ch;
    switch(ch)
    {
      case 'c':case 'C':
              clrscr();
              creat();
              break;
      case 'e':case 'E':
              outof();
              break;
      case 'p':case 'P':
              push();
              break;
      case 'o':case 'O':
              pop();
              break;
      default :
              cleardevice();//CLEAR THE GRAPHICS SCREEN
              gotoxy(10,5);
              cout<<"\n::CHOOSE RIGHT THINGS::\n";
              break;
    }
    gotoxy(3,25);
    getche();
 }
}

    void stack :: creat()
        {
            if(top<MAX-1)
            {
            champak:
            cleardevice();//clear the graphics screen
            cout<<"::ENTER THE ELEMENT IN STACK:: ";
            for(i=0;i<MAX;i++)
            {
                 cin>>arr[i];
                 top++;
            }
            cleardevice();
            gotoxy(2,2);
            cout<<"::THE CURRENT STATUS OF STACK IS::" ;
            setcolor(4);
            setbkcolor(2);
            int r=0,b=0,j=0;
            for(i=0;i<MAX;i++,r=r+30,b=b+30)
             {
                 rectangle(165,45,207,75+b);
                 gotoxy(22,4+j);
                 cout<<arr[i];
                 j=j+2;
             }
             }
             else
             {
              top=-1;
              goto champak;
              }
      }



  void stack::push()
    {
      cleardevice();
      if(top>=MAX-1)
      {
        setcolor(15);
        setbkcolor(4);
        gotoxy(5,5);
        cout<<"::STACK IS FULL i.e.OVERFLOW !!!!!!.............." ;
      }
    else
    {         setcolor(15);
          setbkcolor(4);
          gotoxy(5,2);
          cout<<"::ENTER AN ELEMENT TO BE PUSH:: ";
          cin>>x;
          arr[++top]=x;
          cleardevice();
          gotoxy(2,1);
          cout<<"::AFTER PUSH AN ELEMENT:: ";
          gotoxy(2,2);
          cout<<"::THE CURRENT STATUS OF STACK IS::";
          int r=0,b=0,j=0;
          for(i=0;i<=top;i++,r=r+30,b=b+30)
              {
            rectangle(165,45,207,75+b);
            gotoxy(22,4+j);
            cout<<arr[i];
            j=j+2;
         }
  }
 }
 void stack::pop()
 {
    int i,b=0,j=0,r=0;
    cleardevice();
    gotoxy(5,1);
    if(top<=-1)
    cout<<"\n::UNDERFLOW CONDITION IS APPLY:: ";
    else
    {
    cout<<"::AFTER THE POP OPRATION THE ELEMENT OF STACK ARE::";
    for(i=0;i<top;i++,r=r+30,b=b+30)
         {
            rectangle(165,45,207,75+b);
            gotoxy(22,4+j);
            cout<<arr[i];
            j=j+2;
         }
            cout<<"\n\n\n\t::AND THE DELETED ELEMENT IS:::::::";
            rectangle(165,95+b,207,95+b+30);
            gotoxy(22,4+j+3);
            cout<<arr[top];
            j=j+2;
    }
    --top;
 }

 void stack::outof()
 {
  exit(0);
 }
void main()
{

  int gd=DETECT,gm;
  initgraph(&gd , &gm, "c:\\tc\\bgi");
  setbkcolor(9);
  stack s;
  s.deewan();
  closegraph();//shut down the graphics system
      }

Saturday 7 April 2012

ALGORITHM OF BRESENHAM’S CIRCLE DRAWING...


ALGORITHM OF BRESENHAM’S CIRCLE DRAWING :
Begin :
            Read r , (xc , yc)
            p = 3-2r , x = 0 , y = r
            while(x <= y)
            {
                  Plot(x,y)
                  x = x+1
                  if (P < 0)
                  {
                          P = P + 4x + 6
                   }
                   else
                   {
                          y = y – 1
                          p = p + 4x -4y +10
                    }
                End of while
End

DERIVATION OF BRESENHAM'S CIRCLE DRAWING...


DERIVATION :
                           Let us consider the circle at origin (0,0) i.e. x2 + y2 = r2 and (xk , yk) be  initial points.
F(x,y) = x2 + y2 – r2
F(N) = (xk + 1)2 + y2k – r2
F(S) = (xk + 1)2 + (yk - 1)2 – r2
Decision parameter (Pk) = F(N) + F(S)
Pk = 2( xk + 1 )2 + y2k + (yk - 1)2 – r2
Initial decision parameter (Po) = 2(1+0)2 + r2 + (r-1)2 – 2r2
Po = 3-2r 
Pk+1 = 2(xk + 2)2 + y2k+1 + (yk+1 -1)2 – 2r2
Pk+1Pk = 2[(xk+2)2 – (xk+1)2] + (y2k+1 – y2k) + (yk+1 -1)2 – (yk-1)2
Pk+1 = pk + 2(2xk+3) + [(yk+1 + yk)(yk+1 - yk)] + [(yk+1 + yk - 2)(yk +1 - yk)]-----------(A)

Case 1 :
 If P is negative
xk+1xk = 1
yk+1yk = 0
Put in (A)
Pk+1 = Pk + 2(2xk + 3)
Pk+1 = Pk + 4xk + 6

Case 2 :
If P is positive
xk+1 xk = 1
yk+1yk = -1
Put in (A)
Pk+1 = Pk + 4xk+6 + (yk+1 +yk)(-1) + (yk+1 +yk -2)(-1)
Pk+1 = Pk + 4xk – 4yk + 10

BRESENHAM'S LINE DRAWING ALGORITHM...


Bresenham’s Line Algorithm :
Ø  Read (Xa , Ya) and (Xb , Yb)
      dY = YbYa
      dX = Xb – Xa
      P = 2dY – dX
      X = Xa, Y = Ya
Ø  While(X <= Xb)
      {
              plotpoint(X,Y)
              X++
              if(P<0)
              P=P + 2dY
              else
              {
                      Y++
                      P = P+2(dY - dX)
              }
         }
Ø     End
                  

DERIVATION OF BRESENHAM’S LINE AGORITHM..


Derivation :
                                Initially we have plotted a point (Xk , Yk) and increase  X by 1 we come to Xk+1.
Decision Parameter(Pk)  - (Xk+1 , Yk+1
                                             - (Xk+1 , Yk)
D1 = Y – Yk
D1 = m(Xk+1)+b – Yk
D2 = Yk+1 – Y
D2 = Yk+1 – [m(Xk+1)+b]
D1-D2 = m(Xk+1) – Y k -  Yk+1 + m(Xk+1) + 2b

D1-D2 = 2m(Xk+1) + 2b – Y k – Yk+1
D1-D2 = 2mXk + 2m – 2Yk + (2b-1)
Put  m = dY/dX
(D1-D2)dX = 2Xk dY – 2Yk dX + 2dY + (2b-1)dX

 Pk = 2Xk dY – 2Yk dX + C
 Where C = 2dY + (2b-1)dX

 Pk+1 = 2Xk+1 dY – 2Yk+1 dX + C
 Pk+1-Pk = 2(Xk+1 - Xk)dY – 2(Yk+1 Yk)dX
 Pk+1 = Pk + 2dY – 2(Yk+1 - Yk)dx    --------------------------     A
  If Pk is negative (P k < 0)
  (D1-D2)dX < 0
   Xk+1 – X k = 1
   Yk+1 – Y k = 0

   Put these value in A
Pk+1 = Pk +2dy
This is the decision parameter for less than zero.
If P is positive (P>=0)

(D1-D2)dx  >= 0
Xk+1 – X k = 1
Yk+1 – Y k = 1
 put these value in A

 Pk+1 = Pk + 2(dY - dY)
This is the decision parameter  for greater than zero.
Initial value of decision parameter (Xo , Yo)
Po = 2Xo dY – 2Yo dX + 2dY + (2b-1)dX      --------------------------  B
Yo = mXo +b
YomXo = b
YodY/dX Xo = b
2Yo – 2Xo dY/dX = 2b
2Yo dx – 2Xo dY - dX = (2b-1)dX
Put the value of (2b-1)dX  in B
Po = 2Xo dY – 2Yo dX + 2dY + 2Yo dx – 2Xo dYdX
Po = 2dY – dX
This is the initial decision parameter.


PROGRAM FOR BRESENHAM'S LINE DRAWING ALGORITHM

//***PROGRAM FOR BRESENHAM'S LINE DRAWING ALGORITHM***//

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

void main()
{
  int gd=DETECT,gm;
  int r,x,y,p,xc=320,yc=240;
  initgraph(&gd,&gm,"c:\\TC\\BGI");
  cleardevice();
  printf("Enter the radius :");
  scanf("%d",&r);
  x=0;
  y=r;
  putpixel(xc+x,yc-y,1);
  p=3-(2*r);
  for(x=0;x<=y;x++)
  {
    if(p<0)
    {
      y=y;
      p=(p+(4*6)+6);
    }
    else
    {
      y=y-1;
      p=p+(4*(x-y)+10);
    }
    putpixel(xc+x,yc-y,4);
    putpixel(xc-x,yc-y,4);
    putpixel(xc+x,yc+y,4);
    putpixel(xc-x,yc+y,4);
    putpixel(xc+y,yc-x,4);
    putpixel(xc-y,yc-x,4);
    putpixel(xc+y,yc+x,4);
    putpixel(xc-y,yc+x,4);
  }
  getch();
  closegraph();
}

PROGRAM FOR DDA LINE DRAWING ALGORITHM..


//***PROGRAM FOR DDA LINE DRAWING ALGORITHM***//

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#include<math.h>
#define ROUND(a) ((int)(a+1))
void lineDDA(int xa,int ya, int xb, int yb)
{
  int dx= xb-xa, dy=yb-ya,steps, k;
  float xIncrement,yIncrement,x=xa,y=ya;
   if(abs(dx)>abs(dy)) steps= abs(dx);
   else steps= abs (dy);
   xIncrement = dx/(float) steps;
   yIncrement = dy/(float) steps;
   putpixel (ROUND(x),ROUND (y),5);
   for (k=0;k<steps;k++)
   {
    x+=xIncrement;
    y+=yIncrement;
    putpixel(ROUND(x),ROUND(y),5);
   }
}
void main()
 {
 int a1,a2,b1,b2;
 int gdriver= DETECT, gmode;
 initgraph (&gdriver,&gmode,"c:\\tc\\bgi");
 printf("ENTER THE STARTING POSITION");
 scanf("%d%d",&a1,&a2);
 printf("ENTER THE ENDING POSITION");
 scanf("%d%d",&b1,&b2);
 lineDDA(a1,b1,a2,b2);
 getch();
 closegraph();
 getch();
  }