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

Sunday, September 21, 2008

Linked List::

These function show different operation perform on Linked list.

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

#include/stdio.h/
#include/conio.h/
#include/alloch.h/

struct
{
int data; /*defining structure*/
struct node *link;
};

/*add a node at the end of a linked list*/
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;
}
}

/* add a new node at the beginning of the linked list */
void addatbeg(struct node **q,int num)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp;
}

/* add a new node after the specified numbers nodes */
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("there are %d element in the list",loc);
return;
}
}
r=malloc(sizeof(struct node));
r->data=num;
r->link=temp->link;
temp->link=r;
}

/* display the content of the linked list */
void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d",q->data);
q=q->link;
}
}

/* counts the number of nodes present in the linked list */
int count(struct node *q)
{
int c=0;
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}

/* delete the specified node from the linked list */
void delete(struct node **q,int num)
{
struct node *old,*temp;
temp=*q;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==*q)
*q=temp->link;
else
old->link=temp->link;
free(temp);
return;
}
else
{
old=temp;
temp=temp->link;
}
}
printf("element %d not found",num);
}

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();
}

Wednesday, February 20, 2008

A Funny Loop:

This will make you laugh....its a joke

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

#include/stdio.h/
#include/conio.h/
int main(void)
{
int c;
for(c=1;c<=100,c++)
printf("my name is...");
return 0;
getch();
}

Monday, February 11, 2008

Decimal To Binary

This program convert a decimal number into binary number.

"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
#include/alloc.h/
struct node
{
int data;
struct node *link;
};
void atbeg(struct node**,int);
void display(struct node*);
int count(struct node*);
void main()
{
int n,i;
struct node *p;
p=NULL;
clrscr();
printf("enter the decimal number:");
scanf("%d",&n);
while(n!=0)
{
i=n%2;
atbeg(&p,i);
n=n/2;
}
display(p);
printf("\nnumber of elements=%d",count(p));
getch();
}
void atbeg(struct node **q,int i)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=i;
temp->link=*q;
*q=temp;
}
void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d",q->data);
q=q->link;
}
}
int count(struct node *q)
{
int c=0;
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}

Wednesday, January 30, 2008

Number having special property:

Square of 12 is 144.21,which is reverse of 12 has a square 441,which is reverse of 144.This program search foe such numbers in the range 10 to 100.


"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
void main()
{
unsigned long num,rnum,square,rsquare,n2,num2;
int d1,d2,d3,d4,d5;
for(num=10;num<=100;num++)
{ num2=num;
d1=num2%10;
num2=num2/10;
d2=num2%10;
num2=num2/10;
d3=num2%10;
num2=num2/10;
d4=num2%10;
num2=num2/10;
d5=num2;
rnum=d5+d4*10+d3*100+d2*1000L+d1*10000L;
while(rnum%10==0)
rnum=rnum/10;
square=num*num; r
square=rnum*rnum;
d1=square%10;
square=square/10;
d2=square%10;
square=square/10;
d3=square%10;
square=square/10;
d4=square%10;
square=square/10;
d5=square;
n2=d5+d4*10+d3*100+d2*1000L+d1*10000L;
while(n2%10==0)
n2=n2/10;
if(rsquare==n2)
printf("\n%lu",num);
} }

Tuesday, January 22, 2008

Prime Factor Generator

Generates the prime factors of a given number

"/" is used instead of "<" and ">."
#include/iostream.h/
#include/iomanip.h/
#include/conio.h/
#include/math.h/
void main()
{ clrscr();
unsigned long int x;
char c='y';
do
{
cout<<"enter the no....."< cin>>x;
cout<<"Factors are...";
do
{
for(unsigned long i=2;i<=x;i++)
{
long f=x%i;
if(f==0)
{
cout<<<" ";
x=x/i;
break;
} } }
while(x!=1);
getch();
cout<<"continue...(y/n)....";
cin>>c;
cout<}
while(c=='y');
}

Monday, January 21, 2008

MATRIX Screensaver

#include/dos.h/
#include /graphics.h/
#include /stdlib.h/
#include /stdio.h/
#include /conio.h/
#include/time.h/
#define n 1
void cls256(int r,int b,int g)
{
struct palettetype pal;
getpalette(&pal);
for (int j=0; j<16; j++)
{
setrgbpalette(pal.colors[j], j*r, j*b, j*g);
}
}
void main()
{
int gdriver , gmode ;
gdriver = DETECT;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
int i,j,k,l,x,y,y1,sz,rn[16],cl;
char str[]=" ";
j=0;
cl=15;
//
getch();
randomize();
cls256(1,4,1);
while(1)
{
back: cl=15;
l=0;
if(kbhit())
{
closegraph();
exit(1);
}
for(j=0;j<16;j++)
{
rn[j]= random(400)+11;
if(rn[j]==32)
{
rn[j]+=1;
}
x=random(600);
sz=random(5)+1;
//
sz=2;
y=random(200);
y1=y;
}
for(i=0;i<16;i++)
{
k=0;
j-=1;
y1=y;
for(cl=j;cl<16;cl++)
{
setcolor(cl);
settextstyle(0,0,sz);
sprintf(str,"%c",(char)(rn[k]));
outtextxy(x,y1,str);
delay(3);
k+=1; y1+=sz*10;
}
}
y=y1-sz*10;
for(i=0;i<16;i++)
{
y1=y;
k=15;
j=15-i;
for(cl=j;cl>=0;cl--)
{
setcolor(cl);
settextstyle(0,0,sz);
sprintf(str,"%c",(char)(rn[k]));
//outtextxy(x,y1,str);
y1-=sz*10;
k--;
delay(2);
}
}
}
}

A 2D Animation of a Man

#include/graphics.h/
#include/stdio.h/
#include/conio.h/
#include/math.h/
void main()
{
int n,i,j,k,gd=DETECT,gm,tx,ty;
int x,y,x1,y1,x2,y2;
int xo,yo,xmax,ymax;
float theta;
int temp,xi[20];
int a[20][2];
int b[20][2];
float sx,sy,clope[20];
clrscr();
printf("enter edges");
scanf("%d",&n);
printf("enter coordinates");
for(i=0;i<=n;i++)
{
printf("tx%d/t ty%d",i,i);
scanf("%d%d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
initgraph(&gd,&gm," ");
line(320,0,320,480);
line(0,240,640,240);
xo=320,yo=240;
ymax=480;
for(i=0;i
{
x1=xo+a[i][0];
y1=ymax-(yo+a[i][1]);
x2=xo+a[i+1][0];
y2=ymax-(yo+a[i+1][1]);
line(x1,y1,x2,y2);
}
printf("tx\nty");
scanf("%d%d",&tx,&ty);
for(i=0;i
{
b[i][0]=a[i][0]+tx;
b[i][1]=a[i][1]+ty;
}
b[n][0]=b[0][0];
b[n][1]=b[0][1];
for(i=0;i
{
x1=xo+b[i][0];
y1=ymax-(yo+b[i][1]);
x2=xo+b[i+1][0];
y2=ymax-(yo+b[i+1][1]);
line(x1,y1,x2,y2);
}
getch();
// rotation
printf("angle");
scanf("%f",θ);
theta=(theta*3.14)/180;
for(i=0;i
{
b[i][0]=(int)(a[i][0]*cos(theta)-(a[i][1]*sin(theta)));
b[i][1]=(int)(a[i][0]*sin(theta)+(a[i][1]*cos(theta)));
}
b[n][0]=b[0][0];
b[n][1]=b[0][1];
for(i=0;i
{
x1=xo+b[i][0];
y1=ymax-(yo+b[i][1]);
x2=xo+b[i+1][0];
y2=ymax-(yo+b[i+1][1]);
line(x1,y1,x2,y2);
}
getch();
// scaling
printf("enter scaling sx,sy");
scanf("%f%f",&sx,&sy);
for(i=0;i
{
b[i][0]=(int)a[i][0]*sx;
b[i][1]=(int)a[i][1]*sy;
}
b[n][0]=b[0][0];
b[n][1]=b[0][1];
for(i=0;i
{
x1=b[i][0];
y1=ymax-b[i][1];
x2=b[i+1][0];
y2=ymax-b[i+1][1];
line(x1,y1,x2,y2);
}
getch();

Program to Count CAPITAL Alphabets

This program will count the number of capital alphabets in the given string.
"/" is used instead of "<" and ">'.
#include/stdio.h/
#include/conio.h/
#include/string.h/
void main()
{
static char *str[]={Enter any string};
printf("No. of capitals=%d",capcount(s,n)); /*n=no of string*/
}
capcount(s,x)
char **s;
int c;
{
int i,cap=0;
char *t;
for(i=0;i=65 && *t<=90) cap++; t++; } } return(cap); }

Sunday, January 20, 2008

Smiling Face Program

/* Program to fill the entire screen with smiling face */
"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
main()
{
int r,c;
clrscr();

for(r=0;r<=24;r++) /*fills rows 0to 24*/ for(c=0;c<=79;c++) /*fills colums 0 to 79*/ printf("%c",1); printf("\n\n\nPress any key to exit"); getch(); }

Typical Pattern Program

/* Program to produce a typical pattern */

"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
main()
{
int i=1,x=71,blanks=0,j,val,k;
clrscr();
while(i<=7) { j=65; /*ASCII value of A*/ val=x; while(j<=val) { printf("%c",j); j++; } if(i==1) val--; k=1; while(k<=blanks) { printf(" "); k++; } blanks=2*i-1; while(val>=65)
{
printf("%c",val);
val--;
}
printf("\n");
x--;
i++;
}
printf("\n\n\nPress any key to exit");
getch();
}

ARMSTRONG NUMBER

/* Generate all ARMSTRONG number between 1 t0 500 */
An Armstrong number is that whose sum of cube of each digit is equal to the number.
"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
main()
{
int i=1,a,b,c;
clrscr();
printf("Armstrong number are\n");
while(i<=500) { a=i%10; /*extract last digit*/ b=i%100; b=(b-a)/10; /*extract second digit*/ c=i/100; /*extract first digit*/ if((a*a*a)+(b*b*b)+(c*c*c)==i) printf("%d",i); i++; } printf("\n\n\nPress any key to exit"); getch(); }

Saturday, January 19, 2008

This Program Will Show You The Day on 1st January Of Any Year

Just Enter Year and you will get the day on 1st January of that year
"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
main()
{
int leapdays,firstday,yr;
long int normaldays,totaldays;
clrscr();
printf("enter year");
scanf("%ld",&yr);
normaldays=(yr-1)*365L;
leapdays=(yr-1)/4-(yr-1)/100+(yr-1)/400;
totaldays=normaldays+leapdays;
firstday=totaldays%7;
if(firstday==0)
printf("\nMonday");
if(firstday==1)
printf("\nTuesday");
if(firstday==2)
printf("\nWedneshday");
if(firstday==3)
printf("\nThursday");
if(firstday==4)
printf("\nFriday");
if(firstday==5)
printf("\nSaturday");
if(firstday==6)
printf("\nSunday");
printf("\n\n\nPress any key to exit");
getch();
}

Thursday, January 17, 2008

This is a Eight Queen problem solver.

Well are u fond of playing chess,ever thought of placing eight queens on one board so that none is cross by other?
The program below gives you 92 solutions on a board of 64 squares,wanna try...
"/" is used instead of "<" and ">".
#include /iostream/
#include /stdlib.h/
using namespace std;

int *board;
int bsize;
bool valid( int row, int col ){
//Check the diagnols
// This checks all diagnols comming from the point the function was passed
int calcRow, calcCol;
calcRow = row;
calcCol = col;
while ( calcRow <= bsize && calcCol <= bsize ){ if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow++;
calcCol++;
}
calcRow = row;
calcCol = col;
while ( calcRow >= 0 && calcCol >= 0 ){
if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow--;
calcCol--;
}
calcRow = row;
calcCol = col;
while ( calcRow <= bsize && calcCol >= 0 ){
if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow++;
calcCol--;
}
calcRow = row;
calcCol = col;
while ( calcRow >= 0 && calcCol <= bsize ){ if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow--;
calcCol++;
}

//See if there is a queen in any of the horz or vert directions.
/*calcRow = row;
calcCol = 0;
while ( calcCol <= bsize ){ if( board[calcRow] == calcCol ) return false; //Found a queen
calcCol++;
}*/
calcRow = 0;
calcCol = col;
while ( calcRow <= bsize ){ if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow++;
}

return true;
}

void print(){
// Keep track of the number of solutions
static int solcount = 1;
cout << "Solution #" << style="font-weight: bold;"> // Print Q for a queen or X for a blank spot

for( int j = 0; j <= bsize; j++)
{
for( int i = 0; i <= bsize; i++)
{
if( board[j] == i ) cout << " Q"; else cout << " X";
}
cout << 10 ="=" style="font-weight: bold;"> // Pause every 10 solutions
system("pause");
system("cls");
// Clear the console
}

}
bool move( int row )
//PRECONDITION: The board variable is competely initiazed to -1.
//POSTCONDITION: A board array that has a record of X amount of valid queen placements.
// Where X is equal to the board size.
{

// If the row is greater then bsize that means that it finished one solution
// So it will print the solution and return false to the previous call.
if( row > bsize )
{
print();
cout << style="font-weight: bold;"> // Go through each column on the board and check if a queen can be placed there.
for( int col = 0l; col <= bsize; col++ )
{
if( valid( row, col ) )
{ board[row] = col; if( move( row + 1 ) ) return true;
} }
// If we could not find a valid spot, reset the board array to -1 and return false.
board[row] = -1;
return false;
}

int main(){
//Get the board size from the user
cout << "How big do you want the board to be? : "; cin >> bsize;
bsize--;
// Create new board with specified size
board = new int[bsize];

// Make sure a board was actually created and Windows didnt lie.
if( board == NULL ) {
cout << "ERROR: Memory allocation failed!"<< style="font-weight: bold;"> // Initialize entire board to -1
for( int i = 0; i <= bsize; i++ ) board[i] = -1;
// Clear console
system("cls");
// Find all posible solutions for the board
move(0);
//Clean up board
if( board == NULL ) delete[] board;
return 0;

}

Monday, January 14, 2008

My First Encounter with C

/* Well What would this do? Ever Played Snake, try this one*/

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

#include /stdio.h/
#include /conio.h/
#include /graphics.h/
#include /alloc.h/
#include /dos.h/
#include /stdlib.h/
#define up 72
#define down 80
#define left 75
#define right 77
#define pause 25
#define space 57
#define settings 31
struct body
{
int x,y;
struct body *next;
};
typedef struct body body;
void create_snake(body*);
void show_settings();
void draw_borders();
void draw_egg();
void draw_snake();
void status_bar();
void draw_head();
void erase_tail();
void disp_gameover();
int getkey();
int check_snake(int,int);
body *head,*tail;
int dir=1;
int speed=100;
int leftx,rightx,topx,bottomx;
int color = RED,fil_style=1,bor_color=BLACK,size = 10;
int score=0,egg_drawn=0,jump = 9;
int preveggx,preveggy;
void far *egg;
char scr[15];
int eggx,eggy;
void main()
{
int gm=DETECT,gd;
int key=0,num;
struct time tim;
void move_snake_right();
void move_snake_down();
void move_snake_left();
void move_snake_up();
initgraph(&gm,&gm,"g:\tc\bgi");
leftx=(size*2-1);
rightx=getmaxx()-(size*2);
topx=(size*2-1);
bottomx = getmaxy()-40;
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(10,10,size/3,size/2);
egg = farmalloc(imagesize(10-size/2,10-size/2,10+size/2,10+size/2));
if(egg ==NULL)
{
printf("Insufficient Memory!");
exit(0);
}
getimage(10-size/2,10-size/2,10+size/2,10+size/2,egg);
draw_borders();
create_snake(head);
start:
fflush(stdin);
flushall();
while(!kbhit())
{
delay(speed);
if(egg_drawn==0)
{
status_bar();
eggx=eggy=0;
repeat:
gettime(&tim);
num = (int)(tim.ti_hund)*(int)(tim.ti_sec);
num = num%(rightx-leftx)/size;
eggx=num;
num = (int)(tim.ti_hund)*(int)tim.ti_sec;
num = num%(bottomx-topx)/size;
eggy=num;
if(check_snake(eggx,eggy))
goto repeat;
eggx =leftx+(eggx*size);
eggy =topx+(eggy*size);
preveggx =leftx+(eggx*size);
preveggy =topx+(eggy*size);
}
draw_egg(eggx,eggy);
switch(dir)
{
case 1:move_snake_right();
break;
case 2:move_snake_down();
break;
case 3:move_snake_left();
break;
case 4:move_snake_up();
}
}
key = getkey();
switch(key)
{
case up:if(dir!=2)
dir = 4;
break;
case down:if(dir!=4)
dir = 2;
break;
case right:if(dir!=3)
dir = 1;
break;
case left:if(dir!=1)
dir = 3;
break;
case pause:getch();
break;
case settings:show_settings();
setfillstyle(EMPTY_FILL,color);
bar(leftx,topx,rightx,bottomx);
draw_snake();
break;
case 1:closegraph();
exit(0);
}
goto start;
}
int getkey()
{
union REGS i,o;
i.h.ah = 0;
int86(0x16,&i,&o);
return o.h.ah;
}
void draw_borders()
{
setfillstyle(SOLID_FILL,BLUE);
floodfill(300,300,1);
setfillstyle(SOLID_FILL,BLACK);
setcolor(BLACK);
rectangle(leftx,topx,rightx,bottomx);
setfillstyle(SOLID_FILL,BLACK);
floodfill(300,300,BLACK);
rectangle(leftx,bottomx+5,rightx,getmaxy()-5);
}
void create_snake()
{
body *x;
setfillstyle(fil_style,color);
setcolor(bor_color);
if((x = malloc(sizeof(body)))==NULL)
{
printf("Short of memory!");
return;
}
x->x = size*3+leftx;
x->y = topx;
head = x;
bar(x->x,x->y,x->x+size,x->y+size);
rectangle(x->x,x->y,x->x+size,x->y+size);
if((x->next = malloc(sizeof(body)))==NULL)
{
printf("Short of memory!");
return;
}
x = x->next;
x->x = size*2+leftx;
x->y = topx;
bar(x->x,x->y,x->x+size,x->y+size);
rectangle(x->x,x->y,x->x+size,x->y+size);
if((x->next = malloc(sizeof(body)))==NULL)
{
printf("Short of memory!");
return;
}
x = x->next;
x->x = size+leftx;
x->y = topx;
bar(x->x,x->y,x->x+size,x->y+size);
rectangle(x->x,x->y,x->x+size,x->y+size);
if((x->next = malloc(sizeof(body)))==NULL)
{
printf("Short of memory!");
return;
}
x = x->next;
x->x = leftx;
x->y = topx;
bar(x->x,x->y,x->x+size,x->y+size);
rectangle(x->x,x->y,x->x+size,x->y+size);
x->next = NULL;
tail = x;
}
void move_snake_right()
{
body *x;
int nextx,nexty;
nexty = head->y;
if(head->x+size>=rightx)
nextx = leftx;
else
nextx = head->x+size;
if(check_snake(nextx,nexty)==1)
{
disp_gameover();
}
if(nextx == eggx&&nexty == eggy)
{
egg_drawn=0;
score+=jump;
x = malloc(sizeof(body));
if(x==NULL)
{
printf("Insufficient Memory!");
exit(0);
}
x->x = nextx;
x->y = nexty;
x->next = head;
head = x;
draw_head();
}
else
{
erase_tail();
x = head;
while((x->next)->next !=NULL)
x = x->next;
x->next = NULL;
tail->x = nextx;
tail->y = nexty;
tail->next = head;
head = tail;
tail = x;
draw_head();
}
}
void move_snake_left()
{
body *x;
int nextx,nexty;
if(head->x-sizex-size;
nexty = head->y;
if(check_snake(nextx,nexty)==1)
{
disp_gameover();
}
if(nextx==eggx&&nexty==eggy)
{
egg_drawn=0;
score+=jump;
x = malloc(sizeof(body));
if(x==NULL)
{
printf("Insufficient Memory!");
exit(0);
}
x->x = nextx;
x->y = nexty;
x->next = head;
head = x;
draw_head();
}
else
{
erase_tail();
x = head;
while((x->next)->next !=NULL)
x = x->next;
x->next = NULL;
tail->x = nextx;
tail->y = nexty;
tail->next = head;
head = tail;
tail = x;
draw_head();
}

}
void move_snake_up()
{
body *x;
int nextx,nexty;
if(head->y-sizey-size;
nextx = head->x;
if(check_snake(nextx,nexty)==1)
{
disp_gameover();
}
if(nextx==eggx&&nexty==eggy)
{
egg_drawn=0;
score+=jump;
x = malloc(sizeof(body));
if(x==NULL)
{
printf("Insufficient Memory!");
exit(0);
}
x->x = nextx;
x->y = nexty;
x->next = head;
head = x;
draw_head();
}
else
{
erase_tail();
x = head;
while((x->next)->next !=NULL)
x = x->next;
x->next = NULL;
tail->x = nextx;
tail->y = nexty;
tail->next = head;
head = tail;
tail = x;
draw_head();
}
}
void move_snake_down()
{
body *x;
int nextx,nexty;
if(head->y+size>=bottomx)
nexty = topx;
else
nexty = head->y+size;
nextx = head->x;
if(check_snake(nextx,nexty)==1)
{
disp_gameover();
}
if(nextx==eggx&&nexty==eggy)
{
egg_drawn=0;
score+=jump;
x = malloc(sizeof(body));
if(x==NULL)
{
printf("Insufficient Memory!");
exit(0);
}
x->x = nextx;
x->y = nexty;
x->next = head;
head = x;
draw_head();
}
else
{
erase_tail();
x = head;
while((x->next)->next !=NULL)
x = x->next;
x->next = NULL;
tail->x = nextx;
tail->y = nexty;
tail->next = head;
head = tail;
tail = x;
draw_head();
}

}
void draw_egg(int x,int y)
{
putimage(x,y,egg,COPY_PUT);
egg_drawn = 1;
}

void status_bar()
{
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(leftx+1,bottomx+4,rightx-1,getmaxy()-6);
sprintf(scr,"Score:%d",score);
setcolor(RED);
settextstyle(2,0,6);
outtextxy(leftx+5,bottomx+10,scr);
outtextxy(rightx-75,bottomx+10,"S");
setcolor(DARKGRAY);
outtextxy(rightx-66,bottomx+10,"ettings");
setcolor(RED);
outtextxy(rightx-150,bottomx+10,"P");
setcolor(DARKGRAY);
outtextxy(rightx-142,bottomx+10,"ause");
}
void draw_head()
{
setfillstyle(fil_style,color);
bar(head->x,head->y,head->x+size,head->y+size);
setcolor(bor_color);
rectangle(head->x,head->y,head->x+size,head->y+size);
}
void erase_tail()
{
setfillstyle(EMPTY_FILL,color);
bar(tail->x,tail->y,tail->x+size,tail->y+size);
}
int check_snake(int x,int y)
{
body *z;
z = head;
while(z->next!=NULL)
{
if(z->x==x&&z->y==y)
return 1;
z=z->next;
}
if(z->x==x&&z->y==y)
return 1;
return 0;
}
void disp_gameover()
{
int x1,x2,y1,y2;
x1 = getmaxx()/2-100;
y1 = getmaxy()/2-65;
x2 = getmaxx()/2+100;
y2 = getmaxy()/2+65;
setfillstyle(SOLID_FILL,9);
bar(x1,y1,x2-30,y2-75);
setcolor(YELLOW);
settextstyle(3,0,3);
outtextxy(x1+10,y1+10,"GAME OVER!!");
getch();
closegraph();
exit(0);
}
void show_settings()
{
int key=0,maxspeed=25,least = 145;
int x1,y1,x2,y2,width;
int no,i;
setfillstyle(SOLID_FILL,LIGHTGRAY);
x1 = (getmaxx()+1)/2-150;
x2 = (getmaxx()+1)/2+150;
y1 = (getmaxy()+1)/2-150;
y2 = (getmaxy()+1)/2+150;
setcolor(BLUE);
bar3d(x1,y1,x2,y2,20,1);
settextstyle(2,0,6);
setcolor(RED);
outtextxy(x1+20,y1+100,"Speed:");
outtextxy(x1+85,y1+95,"-");
outtextxy(x2-10,y1+95,"+");
outtextxy(x1+20,y1+150," Use the left and right");
outtextxy(x1+20,y1+180,"arrow keys to increase and ");
outtextxy(x1+20,y1+210," decrease the speed ");
outtextxy(x1+20,y1+240,"Press escape to return");
outtextxy(x1+20,y1+270," to the game");
setcolor(BLUE);
line(x1+100,y1+120,x2-20,y1+120);
line(x1+100,y1+120,x1+100,y1+90);
line(x2-20,y1+120,x2-20,y1+90);
width = ((x2-20)-(x1+100))/8;
start:
setfillstyle(SOLID_FILL,LIGHTGRAY);
for(i=1;i<=no;i++) { bar(x1+100+1,y1+90+1,x1+100+(i*width)-1,y1+120-1); setcolor(LIGHTGRAY); rectangle(x1+100+1,y1+90+1,x1+100+(i*width)-1,y1+120-1); } no = (speed-25)/15; setfillstyle(SOLID_FILL,BLUE); for(i=1;i<=no;i++) { bar(x1+100+1,y1+90+1,x1+100+(i*width)-1,y1+120-1); setcolor(LIGHTGRAY); rectangle(x1+100+1,y1+90+1,x1+100+(i*width)-1,y1+120-1); } def: key = getkey(); switch(key) { case right:if(speedmaxspeed)
speed-=25;
goto start;
case 1:return;
default:goto def;
}
}
void draw_snake()
{
body *x;
x = head;
setfillstyle(SOLID_FILL,color);
setcolor(bor_color);
while(x->next!=NULL)
{
bar(x->x,x->y,x->x+size,x->y+size);
rectangle(x->x,x->y,x->x+size,x->y+size);
x = x->next;
}
}