WELCOME IN THE SEA OF C

Here you find some good quality programs.
If any one want any type of help in C and C++ then you can mail me.
Google

Saturday, August 2, 2008

Different operation on LINK LIST:

'/' is used instead of '<' and '>'.

#include/stdio.h/
#include/conio.h/
#include/alloc.h/

struct node
{
int data;
struct node *link;
}*start;

void append(struct node **,int);
void addatbeg(struct node **,int);
void addafter(struct node *,int,int);
void reverse(struct node **);
void getdata();
void display(struct node *);

void main()
{
struct node *p;
clrscr();
p=NULL;

append(&p,2);
append(&p,25);
append(&p,8);

display(p);

addatbeg(&p,10);
addatbeg(&p,15);
display(p);

addafter(p,4,20);
display(p);

reverse(&p);
display(p);

getdata();

getch();
}

void append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
{
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r=malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;

}
}

void addatbeg(struct node **q,int num)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp;
}

void addafter(struct node *q,int loc,int num)
{
struct node *temp,*r;
int i;
temp=q;
for(i=0;ilink;
if(temp==NULL)
{
printf("\nthere are less than %d elements in list",loc);
return;
}
}
r=malloc(sizeof(struct node));
r->data=num;
r->link=temp->link;
temp->link=r;
}

void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->link;
}
}

void reverse(struct node **q)
{
struct node *t,*r,*s;
t=*q;
r=NULL;
while(t!=NULL)
{
s=r;
r=t;
t=t->link;
r->link=s;
}
*q=r;
}

void getdata()
{
int val,n;
char ch;
struct node *new;
new=NULL;
do
{
printf("\nenter a value");
scanf("%d",&val);
append(&new,val);
display(new);
printf("any more nodes(Y/N)");
ch=getche();
}while(ch=='y' || ch=='Y');
start=new;
}

Tuesday, April 29, 2008

A TYPICAL PATTERN:

A triangular pattern
"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
void main()
{
int i,j,k=1,n,p;
printf("enter the no of rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<(n-i);j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%d",k++);
for(j=(i-1);j>=1;j--)
{
p=k-2;
k=k-1;
printf("%d",p);
}
printf("\n");
}
getch();
}

Wednesday, March 12, 2008

A CHALLENGING GAME:

There are 21 matchsticks, you have to choose matchsticks between 1 to 4 and then computer will choose, and the one who takes the last matchstick will lose........just try it!!!!!!

"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
void game(int);
void check(int);

void main()
{
static int s=21;
clrscr();
printf("NO of matchstick is %d\n",s);
game(s);

getch();
}

void game(int s)
{

int n=1,m=3,x=4,y=2,p,choice;
printf("\nenter your choice between 1 and 4 is \n");
scanf("%d",&p);
s=s-p;
if(s>1 && s<=5)
{
check(s);
printf("\n1.want to play again");
printf("\n2.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
main();
break;
case 2:
exit();
}
}
else
if(p==1)
{
printf("computer choice is %d",x);
s=s-x;
printf("No of Matchstick left is %d",s);
if(s>0)
{
game(s);
}
}

if(p==4)
{

printf("computer choice is %d",n);
s=s-n;
printf("No of Matchstick left is %d",s);
if(s>0)
{
game(s);
}
}
else
if(p==2)
{

printf("computer choice is %d",m);
s=s-m;
printf("No of Matchstick left is %d",s);
if(s>0)
{
game(s);
}}

if(p==3)
{

printf("computer choice is %d",y);
s=s-y;
printf("No of Matchstick left is %d",s);
if(s>0)
{
game(s);
}}
else
printf("enter correct choice");
}


void check(int s)
{

switch(s)
{
case 5:
printf("Computer choice is 4 \n");
printf("Computer Wins");
printf("\n You Lose");
break;

case 4:
printf("Computer choice is 3 \n");
printf("Computer Wins");
printf("\n You Lose");
break;

case 3:
printf("Computer choice is 2 \n");
printf("Computer Wins");
printf("\n You Lose");
break;

case 2:
printf("Computer choice is 1 \n");
printf("Computer Wins");
printf("\n You Lose");
break;
}
}

Friday, February 29, 2008

Square Game:

"Its a challenge!!!!!"

"/" is used instead of "<" and ">".

#include/stdio.h/
#include/conio.h/
#include/graphics.h/
#include/dos.h/
#include/math.h/
#include/stdlib.h/

int a[4][4]={9,2,7,14,5,1,18,4,6,3,11,16,15,19,13,1024},X[4],Y[4],xcount=3,ycount=3,ax=0,ay=0,nom=0; //nom---> no. of moves
void create(),fun(),disp(int),swap();
char getke();
void main()
{
int gd=DETECT,gm,i,j,k;
initgraph(&gd,&gm,"e:\\tc"); //give correct path(bgi directory)

create();
disp(nom);
fun(); //a[4][4] is an 2-d array in which the elements are stored
getch();
//x[4],y[4] are taken for display purpose where the screen positions are stored
}


char getke() //detecting characters
{
char ch;
ch=getch();
if(ch==27)
return('e'); //for exit
if(ch==0) //procedure for retrieving scan code for usage of arrow keys
{
ch=getch();
if(ch==72)
return('u'); //up arrow
if(ch==80) //down arrow
return('d');
if(ch==75) //left arrow
return('l');
if(ch==77)
return('r'); //right arrow
}

}

void create() //rectangular framework
{
int i,j,x=150,y=150;
rectangle(150,150,350,350);
for(i=150;i<350;i+=50)
{
line(i,150,i,350);
}
for(j=150;j<350;j+=50)
{
line(150,j,350,j);
}

for(i=0;i<4;i++,y+=50)
{
Y[i]= y+25;
X[i]=y+25;
}
}

void fun() //procedure after certain key is detected
{
char ch;
while((ch=getke())!='e')
{
if(ch=='u')
{
ax=1;
ay=0;
if(xcount!=0) //boundary condition
{
nom++;
xcount--;
swap();
}
disp(nom); //used to display the elements (refresh).
}
if(ch=='d')
{
ax=-1;
ay=0;
if(xcount!=3)
{
nom++;
xcount++;
swap();
}
disp(nom);
}

if(ch=='l')
{
ax=0;
ay=1;
if(ycount!=0)
{
nom++;
ycount--;
swap();
}
disp(nom);
}

if(ch=='r')
{
ax=0;
ay=-1;
if(ycount!=3)
{
nom++;
ycount++;
swap();
}
disp(nom);

}
}
}
void disp(int x)
{
char *s,*p;
int i,j;
cleardevice();
rectangle(1,1,639,479);
line(500,0,500,479);
itoa(x,p,10);
outtextxy(510,200,"MOVES = ");
outtextxy(580,200,p);
create();
for(i=0;i<4;i++)
{
for(j=0;j<4;j++) //1024 some element
{
if(a[i][j]!=1024)
{
itoa(a[i][j],s,10); //function which converts intezer to string
outtextxy(X[j],Y[i],s);
}
else
{
setfillstyle(1,2);
bar(X[j]-24,Y[i]-24,X[j]+24,Y[i]+24);
}
}
}
}

void swap()
{
int t;
t=a[xcount+ax][ycount+ay];
a[xcount+ax][ycount+ay]=a[xcount][ycount];
a[xcount][ycount]=t;
}





Tuesday, February 26, 2008

Coloured Rectangle's

This is the simple example of drawing and filling of different images.

"/" is used instead of "<" and ">".

#include/stdio.h/
#include/conio.h/
#include/graphics.h/
void main()
{
int gd=DETECT,gm,maxx,maxy,x=40,y=40,fst;
char str[40];
char *pattern[]={"EMPTY_FILL","SOLID_FILL",
"LINE_FILL","LTSLASH_FILL","SLASH_FILL","BKSLASH_FILL","LTBKSLASH_FILL",
"HATCH_FILL","XHATCH_FILL","INTERLEAVE_FILL","WIDE_DOT_FILL",
"CLOSE_DOT_FILL","USER_FILL"};
initgraph(&gd,&gm,"c:\\tc\\bgi");
maxx=getmaxx();
maxy=getmaxy();
rectangle(0,10,maxx,maxy);

setcolor(WHITE);
outtextxy(175,0,"pre-defined Fill style");

for(fst=0;fst<12;fst++)
{
setfillstyle(fst,MAGENTA);
bar(x,y,x+20,y+20);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,BLUE);
bar(x+40,y,x+60,y+20);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,RED);
bar(x+20,y+20,x+40,y+40);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,GREEN);
bar(x+60,y+20,x+80,y+40);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,MAGENTA);
bar(x,y+40,x+20,y+60);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,BLUE);
bar(x+40,y+40,x+60,y+60);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,RED);
bar(x+20,y+60,x+40,y+80);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,GREEN);
bar(x+60,y+60,x+80,y+80);
rectangle(x,y,x+80,y+80);
itoa(fst,str,10);
outtextxy(x,y+100,str);
outtextxy(x,y+110,pattern[fst]);
x=x+150;
if(x>490)
{
y=y+150;
x=40;
}
}
getch();
closegraph();
restorecrtmode();
}