Tuesday, 4 March 2014

Horizontal Line And Vertical Line of Center Of Screen Using C

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm,maxx,maxy,midx,midy;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
        maxx=getmaxx();
        maxy=getmaxy();
        midx=maxx/2;
        midy=maxy/2;
        line(0,midy,midx,maxy);
        line(midx,0,midx,maxy);
getch();
closegraph();
}


////////////////////////////OUTPUT//////////////////////////////



Midpoint Circle Drawing Algorithm Using C

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,p;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
printf("Enter xc and yc:");
scanf("%d%d",&xc,&yc);
printf("Enter radius:");
scanf("%d",&r);
x=0;
y=r;
p=1.25-r;
do
{
putpixel(xc+x,yc+y,1);
putpixel(xc+y,yc+x,2);
putpixel(xc-x,yc+y,1);
putpixel(xc+y,yc-x,2);
putpixel(xc-y,yc-x,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc-y,1);
putpixel(xc-y,yc+x,2);
if(p<0)
{
                        p=p+(2*x)+1;
}
else
{
        p=p+(2*x)+1-(2*y);
y=y-1;
}
x=x+1;
}
while(x<y)
       putpixel(xc+x,yc+y,2);
getch();
closegraph();
}

Bresenham Circle Drawing Algorithm Using C

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,p;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
printf("Enter xc and yc:");
scanf("%d%d",&xc,&yc);
printf("Enter radius:");
scanf("%d",&r);
x=0;
y=r;
p=3-2*r;
do
{
                delay(25);
putpixel(xc+x,yc+y,1);
putpixel(xc+y,yc+x,2);
putpixel(xc-x,yc+y,1);
putpixel(xc+y,yc-x,2);
putpixel(xc-y,yc-x,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc-y,1);
putpixel(xc-y,yc+x,2);
if(p<0) {
p=p+4*x+6;
}
else
{
p=p+4*(x-y)+10;
y=y-1;
}
x=x+1;
}
while(x<y)
         putpixel(xc+x,yc+y,2);
getch();
closegraph();
}



Bresenham Line Drawing Algorithm Using C

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
void main()
{
      int gd = DETECT, gm;
      int dx, dy, p, end;
      float x1, x2, y1, y2, x, y;
      initgraph(&gd, &gm, "c:\tc\bgi");
      printf("Enter Value of X1: ");
      scanf("%f", &x1);
      printf("Enter Value of Y1: ");
      scanf("%f", &y1);
      printf("Enter Value of X2: ");
      scanf("%f", &x2);
      printf("Enter Value of Y2: ");
      scanf("%f", &y2);
      dx = abs(x1 - x2);
      dy = abs(y1 - y2);
      p = 2 * dy - dx;
      if(x1 > x2)
      {
            x = x2;
            y = y2;
            end = x1;
      }
      else
      {
            x = x1;
            y = y1;
            end = x2;
      }
      putpixel(x, y, 10);
      while(x < end)
      {
            x = x + 1;
            if(p < 0)
            {
                  p = p + 2 * dy;
            }
            else
            {
                  y = y + 1;
                  p = p + 2 * (dy - dx);
            }
            putpixel(x, y, 10);
      }
      getch();
      closegraph();
}

Tuesday, 11 February 2014

Caesar Cipher Algorithm Using c

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
int i,l;
char pt[10],ct[10];
printf("Enter plain text: ");
gets(pt);
l=strlen(pt);
for(i=0;i<l;i++)
               {
ct[i]=pt[i]+3;
if(ct[i]>122)
{
ct[i]=ct[i]-26;
}
}
ct[i]=NULL;
printf("\ncipher text is: ");
puts(ct);

for(i=0;i {
pt[i]=ct[i]-3;
if(ct[i]
<97)
{
ct[i]=ct[i]+26;
}
}
printf("\n plain text is: ");
puts(pt);
getch();
}

Monday, 27 January 2014

Binary Search Tree Operations Using c

/*
In This Binary Search Tree Operations We Do
=>  Insert Element
=> Delete Element
=>  Find Maximum Element
=> Find Minimum Element
=> Search Element
*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<math.h>
#include<time.h>
#define Max 20

struct Node
{
       int val;
       struct Node *lptr;
       struct Node *rptr;
};

int SearchNode(struct Node* n, int x);
struct Node* InsertIntoTree(struct Node* n, int x);
struct Node* DeleteFromTree(struct Node* n, int x);
struct Node* SearchMaxElement(struct Node *n);
struct Node* SearchMinElement(struct Node *n);
void InOrderTraversal(struct Node* n);
void PostOrderTraversal(struct Node* n);
void PreOrderTraversal(struct Node* n);

void main()
{
       struct Node *n=NULL;
       struct Node *Min_Max_Value;
       int i,j,Temp_Val,Flag=1;
       char ch;
       float Traverse_Time;
       clock_t first,second;

       do
       {
        printf("\nMenu....");
        printf("\n1. Insert Element \n2. Delete Element \n3. Find Maximum \n4. Find Minimum \n5. Search Element");
        printf("\n6. InOrder Traversal \n7. PreOrder Traversal \n8. Post Order Traversal \n9. Exit");
        printf("\nEnter Choice : ");
        scanf("%d",&ch);
        clrscr();
        switch(ch)
        {
              case 1:  printf("\nEnter Element to be Inserted : ");
                           scanf("%d",&Temp_Val);
                           n = InsertIntoTree(n,Temp_Val);
                            printf("\nElement Inserted");
                            break;
               case 2: first = clock();
                           printf("\nEnter Element to be deleted : ");
                           scanf("%d",&Temp_Val);
                           n = DeleteFromTree(n,Temp_Val);
                           InOrderTraversal(n);
                           printf("\nElement Deleted");
                           second = clock();
                           Traverse_Time = (second-first)/CLOCKS_PER_SEC;
                           printf("\nTime To Delete Element is %f",Traverse_Time);
                           break;
                case 3: first = clock();
                            Min_Max_Value = SearchMaxElement(n);
                            printf("\nMaximum Element in Tree : %d",Min_Max_Value->val);
                            second = clock();
                            Traverse_Time = (second-first)/CLOCKS_PER_SEC;
                            printf("\nTime To Find Max Element is %f",Traverse_Time);
                            break;
                 case 4: first = clock();
                             Min_Max_Value = SearchMinElement(n);
                             printf("\nMinimum Element in Tree : %d",Min_Max_Value->val);
                             second = clock();
                             Traverse_Time = (second-first)/CLOCKS_PER_SEC;
                             printf("\nTime To Find Min Element is %f",Traverse_Time);
                             break;
                 case 5: first = clock();
                            printf("\nEnter Element to search : ");
                            scanf("%d",&Temp_Val);
                            Flag = SearchNode(n,Temp_Val);
                            if(Flag!=0)
                            printf("\nElement Not Found");
                           else
                           printf("\nElement Found");
                           second = clock();
                           Traverse_Time = (second-first)/CLOCKS_PER_SEC;
                           printf("\nTime To Search Element is %f",Traverse_Time);
                           break;
                case 6: first = clock();
                            printf("\nIn Order Traversal...");
                            InOrderTraversal(n);
                            second = clock();
                            Traverse_Time = (second-first)/CLOCKS_PER_SEC;
                             printf("\nTime To InOrder Traversal is %f",Traverse_Time);
                             break;
                  case 7: first = clock();
                             printf("\nPre Order Traversal...");
                             PreOrderTraversal(n);
                             second = clock();
                             Traverse_Time = (second-first)/CLOCKS_PER_SEC;
                             printf("\nTime To PreOrder Traversal is %f",Traverse_Time);
                             break;
                 case 8: first = clock();
                             printf("\nPost Order Traversal...");
                             PostOrderTraversal(n);
                             second = clock();
                             Traverse_Time = (second-first)/CLOCKS_PER_SEC;
                             printf("\nTime To PostOrder Traversal is %f",Traverse_Time);
                             break;
                            default: exit(0);
                       }
                       printf("\nDo You want to Continue [y/n]...");
       }while(getchar()=='Y' || getchar()=='y');
       getch();
}
int SearchNode(struct Node *n, int x)
{
       if (n->val==x)
           return 0;
       else if(n->lptr==NULL && n->rptr==NULL)
           return 1;
       else if(x < n->val)
              return SearchNode(n->lptr,x);
       else if(x > n->val)
              return SearchNode(n->rptr,x);
}
struct Node *InsertIntoTree(struct Node *n, int x)
{
       struct Node *newNode;
       newNode = (struct Node*)malloc(sizeof(struct Node));

       newNode->val = x;
       newNode->lptr = newNode->rptr = NULL;

       if(n==NULL)
       {
                       n = newNode;
                       n->lptr=NULL;
                       n->rptr=NULL;
       }
       else
       {
                       if(x < n->val)
                                       n->lptr = InsertIntoTree(n->lptr,x);
                       else
                                       n->rptr = InsertIntoTree(n->rptr,x);
       }

       return n;
}

struct Node *DeleteFromTree(struct Node *n, int x)
{

       if(n==NULL)
                       printf("\nNo any element exist in Tree");
       else if(x < n->val)
                       n->lptr = DeleteFromTree(n->lptr,x);
       else if(x > n->val)
                       n->rptr = DeleteFromTree(n->rptr,x);
       else
       {
                       if(n->lptr !=NULL && n->rptr != NULL)
                       {
                                       struct Node *Max_Ele;
                                       Max_Ele = SearchMaxElement(n->lptr);
                                       n->val = Max_Ele->val;
                                       n->lptr = DeleteFromTree(n->lptr,Max_Ele->val);
                       }
                       else
                       {
                                       struct Node *Temp;
                                       Temp = n;

                                       if(n->lptr == NULL)
                                                       n = n->rptr;
                                       else
                                                       n = n->lptr;
                                       free(Temp);
                       }
       }
       return (n);
}
struct Node *SearchMaxElement(struct Node *n)
{
       if (n==NULL || n->rptr == NULL)
                       return n;
       else
                       return SearchMaxElement(n->rptr);
}
struct Node *SearchMinElement(struct Node *n)
{
       if(n==NULL || n->lptr == NULL)
                       return n;
       else
                       return SearchMinElement(n->lptr);
}
void InOrderTraversal(struct Node *n)
{
       struct Node *Cur, *Pre;

       if(n==NULL)
                       return;

       Cur = n;
       while(Cur != NULL)
       {
                       if(Cur->lptr == NULL)
                       {
                                       printf("\t%d",Cur->val);
                                       Cur= Cur->rptr;
                       }
                       else
                       {
                                       Pre = Cur->lptr;
                                       while(Pre->rptr !=NULL && Pre->rptr != Cur)
                                                       Pre = Pre->rptr;

                                       if (Pre->rptr == NULL)
                                       {
                                                       Pre->rptr = Cur;
                                                       Cur = Cur->lptr;
                                       }
                                       else
                                       {
                                                       Pre->rptr = NULL;
                                                       printf("\t%d",Cur->val);
                                                       Cur = Cur->rptr;
                                       }
                       }
       }
}

void PreOrderTraversal(struct Node* n)
{
       if(n!=NULL)
       {
                       printf("\t%d",n->val);
                       PreOrderTraversal(n->lptr);
                       PreOrderTraversal(n->rptr);
       }
}

void PostOrderTraversal(struct Node *n)
{
       if(n!=NULL)
       {
                       PostOrderTraversal(n->lptr);
                       PostOrderTraversal(n->rptr);
                       printf("\t%d",n->val);
       }

}

Friday, 17 January 2014

C Programming of DDA line drawing algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,”c:\\tc\\bgi:”);
printf(“\n Enter the x and y value for starting point:\n”);
scanf(“%d%d”,&x1,&y1);
printf(“\n Enter the x and y value for ending point:\n”);
scanf(“%d%d”,&x2,&y2);
printf(“\n The Line is shown below: \n”);
draw(x1,y1,x2,y2);
getch();
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,1);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
}