Loading

Archive for 15 Sept 2012

C - Program for Character Generation

C - Program for Character Generation


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,j;
int a[20][20]=
{{0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0},
{0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0}};


initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<19;i++)
{
for(j=0;j<19;j++)
{
if(a[i][j]==1)
putpixel(100+j,200+i,WHITE);
}
}
getch();
}

Posted in | Leave a comment

C - Program for Implementation of Mid-Point Ellipse Drawing Algorithm

C - Program for Implementation of Mid-Point Ellipse Drawing Algorithm



#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <dos.h>

int main(void)
{
int gd=DETECT,gm;
int cenx,ceny;
float Pk,a,b,x,y;
clrscr();

printf("\n\n Enter 'a' and 'b': ");
scanf("%f%f",&a,&b);

initgraph(&gd,&gm,"c:\\tc\\bgi");


cenx=getmaxx()/2;
ceny=getmaxy()/2;

Pk=b*b-b*a*a+0.25*a*a;
x=0;
y=b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);

while (2*x*b*b <= 2*y*a*a)
{
if (Pk<0)
{
x=x+1;
y=y;
Pk=Pk+2*x*b*b+3*b*b;
}
else
{
x=x+1;
y=y-1;
Pk=Pk+2*x*b*b+3*b*b-2*y*a*a+2*a*a;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}


Pk=(x+0.5)*(x+0.5)*b*b+(y-1)*(y-1)*a*a-a*a*b*b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
while (y>0)
{
if (Pk>0)
{
x=x;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a;
}
else
{
x=x+1;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a+2*x*b*b+2*b*b;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}
gotoxy(1,25);
printf ("\npress any key to exit.");
getch();
closegraph();
return 0;
}

Posted in | Leave a comment

C - Program for Liang Barsky Line Clipping Algorithm

C - Program for Liang Barsky Line Clipping Algorithm



#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");

if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||
( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );

}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}

if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;

x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );

printf("Original line cordinates\n");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordinate are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax);

printf("New coordinates are \n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();

}
}
}

Posted in | 1 Comment

C - Program for Cohen Sutherland Line Clipping Algorithm

C - Program for Cohen Sutherland Line Clipping Algorithm



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

typedef unsigned int outcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };

void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{

int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;

code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);

if(accept) line(x0,y0,x1,y1);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

}


int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;

if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;

return(code);
}


main()
{

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;

clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");

printf("\n\n\tEnter the co-ordinates of Line :");

printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);

printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);


printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();


lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();

}

Posted in | Leave a comment

C - Program to Implement Flood Fill Algorithm

C - Program to Implement Flood Fill Algorithm



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

void fill_right(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);

}
}

void fill_left(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);

fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);

}
}


void main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}

a[n][0]=a[0][0];
a[n][1]=a[0][1];

printf("\n\n\tEnter the seed pt. : ");
scanf("%d%d",&x,&y);


cleardevice();
setcolor(WHITE);

for(i=0;i<n;i++) /*- draw poly -*/
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}

fill_right(x,y);
fill_left(x-1,y);

getch();
}

/*SAMPLE INPUT*/
/*Enter the number of edges of polygon 4

X0 Y0 = 50 50
X1 Y1 = 200 50
X2 Y2 = 200 300
X3 Y3 = 50 300

Enter the seed point 100 100*/

Posted in | Leave a comment

C - Program to Implement Boundary Fill Algorithm

C - Program to Implement Boundary Fill Algorithm



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



void fill_right(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}


void fill_left(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);

fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
}
delay(1);
}


void main()
{
int x,y,n,i;
int gd=DETECT,gm;
clrscr();

initgraph(&gd,&gm,"c:\\tc\\bgi");



/*- draw object -*/

line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);

/*- set seed point -*/
x = 100; y = 100;

fill_right(x,y);
fill_left(x-1,y);

getch();
}

Posted in | Leave a comment

C - Program to Implement Bezier Curve Drawing Algorithm

C - Program to Implement Bezier Curve Drawing Algorithm


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

int x,y,z;

void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];
for(i=0;i<4;i++) { c[i][0]=0; c[i][1]=0; }
printf("\n\n Enter four points : \n\n");

for(i=0; i<4; i++)
{
printf("\t X%d Y%d : ",i,i);
scanf("%d %d",&c[i][0],&c[i][1]);
}

c[4][0]=c[0][0];
c[4][1]=c[0][1];

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");

ymax = 480;

setcolor(13);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}

setcolor(3);
n=3;

for(i=0;i<=40;i++)
{
u=(float)i/40.0;
bezier(u,n,c);

if(i==0)
{ moveto(x,ymax-y);}
else
{ lineto(x,ymax-y); }
getch();
}
getch();
}
bezier(u,n,p)
float u;int n; int p[4][3];
{
int j;
float v,b;
float blend(int,int,float);
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{
b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}

float blend(int j,int n,float u)
{
int k;
float v,blend;
v=C(n,j);
for(k=0;k<j;k++)
{ v*=u; }
for(k=1;k<=(n-j);k++)
{ v *= (1-u); }
blend=v;
return(blend);
}

C(int n,int j)
{
int k,a,c;
a=1;
for(k=j+1;k<=n;k++) { a*=k; }
for(k=1;k<=(n-j);k++) { a=a/k; }
c=a;
return(c);
}

Posted in | Leave a comment

C - Program without Main Function


 C - Program without Main Function


#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)int begin()
{
printf(” hello “);
}


Explanation -

 Here we are using preprocessor directive #define with arguments to give an impression that the program runs without main. But in reality it runs with a hidden main function.

The ‘##‘ operator is called the token pasting or token merging operator. That is we can merge two or more characters with it.
NOTE: A Preprocessor is program which processess the source code before compilation.

Look at the 2nd line of program -
#define decode(s,t,u,m,p,e,d) m##s##u##t

What is the preprocessor doing here. The macro decode(s,t,u,m,p,e,d) is being expanded as “msut” (The ## operator merges m,s,u & t into msut). The logic is when you pass (s,t,u,m,p,e,d) as argument it merges the 4th,1st,3rd & the 2nd characters(tokens).

Now look at the third line of the program -
#define begin decode(a,n,i,m,a,t,e)

Here the preprocessor replaces the macro “begin” with the expansion decode(a,n,i,m,a,t,e). According to the macro definition in the previous line the argument must be expanded so that the 4th,1st,3rd & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are ‘m’,’a’,’i’ & ‘n’.

So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on for the compiler. That’s it…

The bottom line is there can never exist a C program without a main function. Here we are just playing a gimmick that makes us beleive the program runs without main function, but actually there exists a hidden main function in the program. Here we are using the proprocessor directive to intelligently replace the word begin” by “main”. In simple words int begin=int main.

Posted in | Leave a comment

C - Program for Guessing Game

C - Program for Guessing Game



#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
void main()
{
int num,guess=-1,tries=0,pass=0;
time_t t;
srand((unsigned)time(&t));
num=rand()%100;
while((guess!=num)&&tries<8)
{
printf("Enter the guess num b/w 0&100
(you have %d tries left out)\n",(8-tries)); 
scanf("%d",&guess);

tries++;
if(guess==num)
{
printf("Hurray you guessed it correctly!!!\n");

pass=1;
}

else if(num< guess)
printf("Your guess is too high\n");

else
printf("Your guess is too low\n");
}

if(pass==0)
printf("Sorry you lost! The correct number is %d\n",num);
} 

Posted in | Leave a comment

Self Destructing Program in C

Self Destructing Program in C


#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
printf("This program will destroy itself if u press any key!!!\n");
getch();
remove(_argv[0]);/*array of pointers to command line arguments*/
}

Posted in | Leave a comment

C - Program for Pigeon Breeding Problem

C - Program for Pigeon Breeding Problem



#include<stdio.h>
#include<process.h>

struct node
{
int age;
struct node *link;
};

typedef struct node* NODE; 
 NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf(“Out of memory\n”);
exit(1);
}
return x;
}

void main()
{

unsigned long int count=1;
unsigned int months,i;
NODE first=getnode();/*this is the intial adult pair*/
first->age=2; /*assume the age of initial adult pair as 2*/
first->link=NULL;
printf(“Enter the no. of months\n”);
scanf(“%u”,&months);
for(i=0;iage>=2)&&(temp->age<=60)) { NODE temp1=getnode(); temp->age+=1;
temp1->age=1;
temp1->link=first;
first=temp1;
temp=temp->link;
++count;
}

else
{
temp->age+=1;
temp=temp->link;
}
}
}
printf(“Total no. of pairs after %u months=%ld\n”,months,count);
}

Posted in | Leave a comment

C - Program to Produce Sound of Specific Frequency

C - Program to Produce Sound of Specific Frequency


#include<dos.h>
 
main()
{
   int a;
 
   for ( a = 200 ; a <= 1000 ; a = a + 20 )
   {
      sound(a);
      delay(25);
   }
   nosound();
 
   return 0;
}

Posted in | Leave a comment

C - Program to Display Time

C - Program to Display Time


#include<stdio.h>
#include<dos.h>
 
main()
{
   struct time t;
 
   gettime(&t);
 
   printf("Current system time is %d : %d : %d\n",t.ti_hour,t.ti_min,t.ti_sec);
 
   return 0;
}

Posted in | Leave a comment

C - Program to Display Date

C - Program to Display Date


#include<stdio.h>
#include<dos.h>
 
main()
{
   struct date d;
 
   getdate(&d);
 
   printf("Current system date is %d/%d/%d\n",d.da_day,d.da_mon,d.da_year);
 
   return 0;
}

Posted in | Leave a comment

C - Program to Calculate Square Root of Given Number

C - Program to Calculate Square Root of Given Number


#include <stdio.h>
#include <math.h>
 
int main()
{
 
  double n, result;
 
  printf("Enter a number to calculate it's square root\n");
  scanf("%lf", &n);
 
  result = sqrt(n);
 
  printf("Square root of %.2lf = %.2lf\n", n, result);
 
  return 0;
}

Posted in | Leave a comment

C - Program to Display Mouse Pointer in Graphics Mode

C - Program to Display Mouse Pointer in Graphics Mode


#include<graphics.h>
#include<conio.h>
#include<dos.h>
 
int initmouse();
void showmouseptr();
 
union REGS i, o;
 
main()
{
   int status, gd = DETECT, gm;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   status = initmouse();
 
   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
      showmouseptr();
 
   getch();
   return 0;
}
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
 
void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}

Posted in | Leave a comment

C Program to Display Mouse Pointer in Textmode

C Program to Display Mouse Pointer in Textmode 


#include<dos.h>
#include<conio.h>
 
int initmouse();
void showmouseptr();
 
union REGS i, o;
 
main()
{
   int status;
 
   status = initmouse();
 
   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
      showmouseptr();
 
   getch();
   return 0;
}
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
 
void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}

Posted in | Leave a comment