Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Saturday, September 1, 2007

tic tac toe (tic-tac-toe) in C

tic tac toe in C
============


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


int rowcol[9]={1,2,3,4,5,6,7,8,9};

void main(){

int gdriver,gmode,errorcode;
int is;
int i,temp;
int bool;
char ch[2];
bool=0;
gdriver=EGA;
gmode=EGAHI;
/*initialize the graphics driver*/
initgraph(&gdriver,&gmode,"");
/* error handling for graphics driver */
errorcode = graphresult();
if ( errorcode != grOk ) {
printf("Graphics Error \n %s",grapherrormsg(errorcode));
printf("\n Press any key to halt:");
getch();
exit(1);
}

setbkcolor(11);
setcolor(1);
settextstyle(0,0,2);
outtextxy(40,40,"WELCOME TO THE GAME OF SUNCHOKRI");
/*
for(is=1000;is>500;is--){
sound(is);
delay(250);
}
nosound();
*/

setcolor(12);

outtextxy(150,120,"BY ----");
/* preloader logic in c programming */
setcolor(1);
i=0;
while(i!=600){
preloader(i);
delay(300);
i++;
}


sleep(2);
cleardevice();
mainscreen();
positionofx();
chanceofplayer(bool);

/*if q is pressed quit the game */
while((*ch=getch())!='q') {
/*if n is pressed new game*/
if(*ch=='n'){
for(is=0;is<9;is++){
rowcol[is]=is+1;
}
bool=0;
cleardevice();
mainscreen();
positionofx();
chanceofplayer(bool);
}

temp=atoi(ch) ;
if (temp==0){
}
else {

if (rowcol[temp-1]==11 rowcol[temp-1]==22){
}
else {
if(bool==0){
rowcol[temp-1]=11;
}
else
{
rowcol[temp-1]=22;
}
cleardevice();
mainscreen();
positionofx();
if(bool==0){
bool=1;
}
else
{
bool=0;
}
chanceofplayer(bool);
}
}
}
/*
getch(); */

/*closing or deallocating graphic driver*/
closegraph();
}

/*main background screen*/
mainscreen() {
setbkcolor(12);
rectangle(10,10,620,250);
setcolor(14);
settextstyle(0,0,4);
outtextxy(35,300,"S U N C H O K R I");
setcolor(9);
settextstyle(0,0,2);
outtextxy(105,220,"Player 1 :11 Player 2 :22");
setcolor(15);
settextstyle(0,0,1);
outtextxy(0,0,"Author: ----SG");
/* small rectagle for sunchokri.. */
setcolor(WHITE);
rectangle(20,20,300,200);
rectangle(50,50,90,80);
rectangle(100,50,140,80);
rectangle(150,50,190,80);

rectangle(50,120,90,90);
rectangle(100,120,140,90);
rectangle(150,120,190,90);

rectangle(50,160,90,130);
rectangle(100,160,140,130);
rectangle(150,160,190,130);


outtextxy(399,60,"q TO QUIT GAME");
outtextxy(399,80,"n FOR NEW GAME");
}

/* logic of various functions in c */
preloader(int i) {
outtextxy(i,300,"");
}

positionofx(){
char x[10];
setcolor(YELLOW);

itoa(rowcol[8],x,10);
/*last row */
outtextxy(165,140,x);
itoa(rowcol[7],x,10);
outtextxy(115,140,x);
itoa(rowcol[6],x,10);
outtextxy(65,140,x);

/*second last row */
itoa(rowcol[5],x,10);
outtextxy(165,100,x);
itoa(rowcol[4],x,10);
outtextxy(115,100,x);
itoa(rowcol[3],x,10);
outtextxy(65,100,x);

/*first row */
itoa(rowcol[2],x,10);
outtextxy(165,60,x);
itoa(rowcol[1],x,10);
outtextxy(115,60,x);
itoa(rowcol[0],x,10);
outtextxy(65,60,x);
}

chanceofplayer(int bool1 ) {
char s[1];
if(bool1==0){
bool1=1; }
else
{
bool1=2; }

itoa(bool1,s,10);
setcolor(4);
outtextxy(33,33,"Turn of player : ");
outtextxy(173,33,s);

/* logic for winning the game */
if((rowcol[0]==11 && rowcol[1]==11 && rowcol[2]==11)(rowcol[3]==11 && rowcol[4]==11 && rowcol[5]==11)(rowcol[6]==11&&rowcol[7]==11&&rowcol[8]==11)(rowcol[0]==11&&rowcol[4]==11&&rowcol[8]==11)(rowcol[2]==11&&rowcol[4]==11&&rowcol[6]==11)){
outtextxy(399,100, "1 WINS");
sound(350);
delay(2000);
nosound();
}
else if((rowcol[0]==22&&rowcol[1]==22&&rowcol[2]==22)(rowcol[3]==22&&rowcol[4]==22&&rowcol[5]==22)(rowcol[6]==22&&rowcol[7]==22&&rowcol[8]==22)(rowcol[0]==22&&rowcol[4]==22&&rowcol[8]==22)(rowcol[2]==22&&rowcol[4]==22&&rowcol[6]==22)){
outtextxy(399,100, "2 WINS");
sound(350);
delay(2000);
nosound();
}
else if((rowcol[0]==22&&rowcol[3]==22&&rowcol[6]==22)(rowcol[1]==22&&rowcol[4]==22&&rowcol[7]==22)(rowcol[2]==22&&rowcol[5]==22&&rowcol[8]==22)){
outtextxy(399,100, "2 WINS");
sound(350);
delay(2000);
nosound();
}
else if((rowcol[0]==11&&rowcol[3]==11&&rowcol[6]==11)(rowcol[1]==11&&rowcol[4]==11&&rowcol[7]==11)(rowcol[2]==11&&rowcol[5]==11&&rowcol[8]==11)){
outtextxy(399,100, "1 WINS");
sound(350);
delay(2000);
nosound();
}
else{
outtextxy(399,100,"NO RESULT");
}
}

shared files program in C

shared files program in C

/* shared file example 1 */


#include <fcntl.h>
#include <stdio.h>


main()
{
int fp;
char buff[20];
fp=open("testfile",O_RDONLY);
if(fork()==0)
{
read(fp,buff,10);
buff[10]=0;
puts(buff);
}

else
{
wait(0);
read(fp,buff,10);
buff[10]=0;
puts(buff);
}

}

/*shared file example 2 */

#include <fcntl.h>
#include <stdio.h>


main()
{
FILE *fp;
char buff[20];
fp=fopen("testfile","r");
if(fork()==0)
{
fgets(buff,10,fp);
buff[10]=0;
puts(buff);
}

else
{
wait(0);
fgets(buff,10,fp);
buff[10]=0;
puts(buff);

}

}

/*lock file */


#include <fcntl.h>
#include <stdio.h>


main()
{
int fd;
fd=open("Lockset",O_RDWR);
lockf(fd,F_LOCK,0);
printf("Process %d locked the file \n",getpid());
if(fork()==0)
{
lockf(fd,F_LOCK,0);
printf("Process %d locked the file \n",getpid());
printf("Child Process is over\n");
}

else
{
sleep(5);
printf("Process %d is over\n",getpid());

}

}

semaphore program in C

Semaphore program in C

/* basic semaphore example 1 */

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>


main()
{
int sid;
int key,nsem;
key=(key_t)0x30;
nsem=1;
sid=semget(key,nsem,IPC_CREAT0777);
printf("\nSemaphore was created with the id = %d\n",sid);

}

/* basic semaphore example 2*/

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>


main()
{
int sid;
int key,nsem;
key=(key_t)0x30;
nsem=1;
sid=semget(key,nsem,IPC_CREAT0666);
printf("\nSemaphore was created with the id = %d\n",sid);
sleep(15);
semctl(sid,0,IPC_RMID,0);
}


/* readers */

#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <stdio.h>


void sem_wait (int);
void sem_signal(int);


main(){
int rdcnt,wrt,mutex;
key_t rdkey=0x20;
key_t wrkey=0x30 ;

rdcnt = semget(rdkey,1,0666IPC_CREAT);
wrt = semget(wrkey,1,0666IPC_CREAT);
mutex = semget(0x40 ,1,0666IPC_CREAT);

semctl(mutex,0,SETVAL,1);
printf("\nStarting to Read.....");
sem_wait(mutex);
sem_signal(rdcnt);
sleep(10);
printf("\nReading Finished");
sem_wait(rdcnt);
sem_signal(mutex);

}


void sem_wait(int id){
struct sembuf buf = { 0,-1,0};
semop(id,&buf,1);
}

void sem_signal(int id){
struct sembuf buf = { 0,1,0};
semop(id,&buf,1);


}

screen saver in C random circles lines - c program

screen saver in C random circles lines - c program

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


char ch;
/****************************************/
/* for line */
int lrandx, lrandy, lrandxx, lrandyy;
/* for rectangle */
int rrandx, rrandy, rrandxx, rrandyy;
/* for circle*/
int crandx, crandy, cradius;
/* random colour and shape generation */
int shape;
int col;

int gdriver,gmode,errorcode;
/*****************************************/
void main(){
gdriver=EGA;
gmode=EGAHI;
/*initialize the graphics driver*/
initgraph(&gdriver,&gmode,"");
/* error handling for graphics driver */
errorcode = graphresult();
if (errorcode != grOk ) {
printf("Graphics Error \n %s",grapherrormsg(errorcode));
printf("\n Press any key to halt:");
getch();
exit(1);
}
/* setting initiation of text and background color */
initialscreen();
while (!kbhit()) /* do nothing */ {
sleep(0.5);
mainscreen();
/*
outtextxy(133,44,"Sss");
*/
}
/*closing or deallocating graphic driver*/
closegraph();
}


/*main background screen*/
mainscreen() {
shape=rand()%3;
col=rand() % 14;
if (shape==0) {
rrandx=rand() % 600;
rrandy=rand() % 600;
rrandxx=rand() % 800;
rrandyy=rand() % 800;
setcolor(col);
rectangle(rrandx, rrandy, rrandxx, rrandyy);
}
else if (shape==1) {
lrandx=rand() % 200;
lrandy=rand() % 200;
lrandxx=rand() % 200;
lrandyy=rand() % 200;
setcolor(col);
line(lrandx, lrandy, lrandxx, lrandyy);
}
else {
crandx=rand() % 600;
crandy=rand() % 600;
cradius=rand() % 100;
setcolor(col);
circle(crandx, crandy, cradius);
}
return 0;
}

initialscreen() {
setbkcolor(4);
setcolor(1);
settextstyle(0,0,2);
outtextxy(40,40,"SCREEN SAVER...");
setcolor(11);
outtextxy(150,120,"---- computer bapu");
sleep(2);
cleardevice();
return 0;
}

character generation in C - program

Character generation in C - program

#include <graphics.h>
#include <conio.h>
#include <math.h>
void generatecharback();
void generatechar(char);
void main()
{
int gdriver, gmode;

gdriver=DETECT;
gmode=EGAHI;
initgraph(&gdriver,&gmode,"");
setbkcolor(4);
generatecharback();
generatechar('B');
outtextxy(203, 200, "char generation");
getch();
closegraph();
}

void generatecharback(){
int i=0;
int j=0;
for(i=0;i<=7;i++){
for(j=0;j<=7;j++){
putpixel(i+100,j+100,8);
}
}
}
void generatechar() {
int charray[1][65]={{1,1,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0}};
int i,j,k;
k=0;
for(i=0;i<=7;i++){
for(j=0;j<=7;j++){
if(charray[0][k]==1){
putpixel(j+100,i+100,WHITE);
}
k++;
}
}
}

forking i.e examples in C (fork program)

forking i.e example in C (fork program)
===============================


/*forking example 1 */
#include<sys/types.h>
main()
{
int i;
int j;
fork();

printf("\n%d\n",getpid());
printf("\n%d\n",getppid());
getchar();

}

/*forking example 2 */
main()
{
int i=fork();
if(i==0)
{
printf("I'm the child.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());
}
else
{
printf("I'm the Parent.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());
wait();
}
exit(0);
}


/*forking example 3 */

main()
{
int i=fork();
if(i==0)
{
printf("I'm the child.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());

sleep(5);

printf("I'm the child.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());
}
else
{
sleep(2);
printf("I'm the Parent.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());
}
}

/*forking example 4 */

#include<sys/types.h>
main()
{
int j;
int pid=fork();
if(pid==0)
for(j=0;j<30000;j++)
printf("\n%10d\tChild",j);

else
for(j=0;j<30000;j++)
printf("\n%10d\tParent",j);
}


/*forking example 5*/

#include<sys/types.h>
main()
{
int j;
if(fork()==0)
{
printf("\nFirst Child Executing");
for(j=0;j<10000;j++)
;
printf("\nFirst Child is dying now\n" );
}
else
{
if(fork()==0)
{
printf("Second Child Executing");
for(j=0;j<10000;j++)
;
printf("\nSecond Child is dying now\n" );
}
else
{

printf("\nI'm the BOSS here\n");
printf("\nThe first child to die is %d\n",wait(0));

for(j=0;j<10000;j++)
;
printf("\nThe Second child to die is %d\n",wait(0));

printf("But nothing is in my hand!\n");
}

}

}

/*forking example 6*/

main()
{
int pid;
int i=10;
pid=fork();
if(pid==0)
{
printf("\nThe value of I in child is \t%d\n",i);
i+=10;
printf("\nThe (inc)value of I in child is \t%d\n",i);

}

else
{
wait(0);
printf("\nThe value of I in parent is \t%d\n",i);
}
}

/*forking example 7*/

main()
{
int i=10;
if(fork()==0)
printf("\nThe address of i in child is %X \n",&i);
else
printf("\nThe address of i in parent is %X \n",&i);

}


/*forking example 8*/

#include<sys/types.h>
main()
{
if(fork()>0)
{
printf("I'm the parent\n");
sleep(10);
}


}


Monday, August 27, 2007

PREFIX TO INFIX in C

PREFIX TO INFIX in C

#include<stdio.h>
struct stack
{
char data[7][16];
int tos;
}s;


main()
{
char prefix[20];
clrscr();
printf("PLEASE ENTER THE PREFIX EXPRESSION:");
scanf("%s",prefix);
preinf(prefix);
getch();
}

preinf(char prefix[])
{
int length,i,len;
char str1[20];
length=strlen(prefix);
for(i=length;i>=0;i--)
{
switch(prefix[i])
{
case '+':
case '-':
case '*':
case '/':
case '^':
{
str1[0]='(';
str1[1]='\0';
strcat(str1,pop());
len=strlen(str1);
str1[len]=prefix[i];
str1[++len]='\0';
strcat(str1,pop());
len=strlen(str1);
str1[len]=')';
str1[++len]='\0';
push(str1);
break;
}
default:
{
str1[0]=prefix[i];
str1[1]='\0';
push(str1);
}
}
}
printf("\nTHE INFIX EXPRESSION IS:%s",pop());
}

push(char string[])
{
strcpy(s.data[s.tos],string);
s.tos++;
}

pop()
{
s.tos--;
return(s.data[s.tos]);
}

PROGRAMME TO CONVERT INFIX EXPRESSION TO POSTFIX

/* PROGRAMME TO CONVERT INFIX EXPRESSION TO POSTFIX
EXPRESSION */


#include<stdio.h>
# define size 20
struct stk
{
char ele[size];
int tos;
};
typedef struct stk stack;
stack s;
main()
{
int len;
char infix[20],ch;
clrscr();
printf("PLEASE ENTER INFIX EMPRESSION:");
scanf("%s",infix);
len=strlen(infix);
infix[len]=')';
infix[++len]='\0';
printf("\n THE POSTFIX EXPRESSION IS:");
infpre(infix);
getch();
}

infpre(char infix[20])
{
int length,i,a,b;
char ch;
s.tos=0;
s.ele[s.tos]='(';
length=strlen(infix);
for(i=0;i<length;i++)
{
switch(infix[i])
{
case '*':
case '+':
case '-':
case '/':
case '^':
case '(':
case '$':
{
a=isp(s.ele[s.tos]);
b=icp(infix[i]);
if(a>=b)
{
ch=pop();
printf("%c",ch);
s.tos++;
push(infix[i]);
}
else
{
s.tos++;
push(infix[i]);

}
break;
}
case ')':
{
ch=pop();
while(ch!='(')
{
printf("%c",ch);
ch=pop();
}
break;
}
default:
printf("%c",infix[i]);
}}}

push(char ch)
{
s.ele[s.tos]=ch;
}

pop()
{
char ch;
ch=s.ele[s.tos];
s.tos--;
return(ch);
}

int isp(char a)
{
switch(a)
{
case '^':return(3);
case '*':
case '/':return(2);
case '+':
case '-':return(1);
case '(':return(0);
case '$':return(5);
}}

int icp(char a)
{
switch(a)
{
case '^':return(4);
case '*':
case '/':return(2);
case '+':
case '-':return(1);
case ')':return(4);
case '$':return(5);
}}

PREFIX TO POSTFIX in C

/* PREFIX TO POSTFIX in C*/

#include<stdio.h>
#include<string.h>
#define size 10
char ch;
struct stk
{
char ele[size];
int top;
}s;
main()
{
int length;
char infix[20];
clrscr();
printf("PLEASE ENTER THE PREFIX EXPRESSION:");
scanf("%s",infix);
length=strlen(infix);
infix[length]=')';
infix[++length]='\0';
printf("\nTHE POSTFIX MESSAGE IS:");
infpost(infix);
getch();
}
infpost(char infix[])
{
int a,b,length,i,count=0;
s.top=0;
s.ele[s.top]='(';
s.top++;
length=strlen(infix);
for(i=0;i<length;i++)
{
switch(infix[i])
{
case '*':
case '^':
case '/':
case '+':
case '-':
case '(':
{
a=isp(s.ele[s.top]);
b=icp(infix[i]);
/*printf("\nisp=%d icp=%d",a,b);
getch();*/
if(a>=b)
{
ch=pop();
printf("%c",ch);
s.top++;
push(infix[i]);
if(i==10)
break;
if(s.top==2)
{
s.top--;
ch=pop();
printf("%c",ch);
s.ele[1]=s.ele[2];
s.top=1;
}
}
else
{
if(count!=0) s.top++;
push(infix[i]);
count++;
break;
}}
case ')':
{
ch=pop();
while(ch!='(')
{
printf("%c",ch);
ch=pop();
}
break;
}
default:
printf("%c",infix[i]);
} } }


int push(char ch)
{
if(s.top==size-1)
{
printf("stack full \n");
return(0);
}
s.ele[s.top]=ch;
return(1);
}
int pop()
{
if(s.ele[s.top]==-1)
{
printf("stack empty");
return(0);
}
ch=s.ele[s.top];
s.top--;
return(ch);
}
int isp(char a)
{
switch(a)
{
case '^': return(3);
case '*':
case '/': return(2);
case '+':
case '-':return(1);
case '(':return(0);
}
}

int icp(char a)
{
switch(a)
{
case '^': return(4);
case '*':
case '/': return(2);
case '+':
case '-':return(1);
case ')':return(4);
}
}

PROGRAMME TO CONVERT INFIX TO PREFIX

/* PROGRAMME TO CONVERT INFIX TO PREFIX */


#include<stdio.h>
# define size 20
struct stk
{
char ele[size];
int tos;
};
typedef struct stk stack;
stack s;
main()
{
int len;
char infix[20],ch;
clrscr();
printf("PLEASE ENTER INFIX EMPRESSION:");
scanf("%s",infix);
len=strlen(infix);
infix[len]=')';
infix[++len]='\0';
printf("\n THE PREFIX EXPRESSION IS:");
infpre(infix);
getch();
}

infpre(char infix[20])
{
int length,i,a,b,len;
char ch,prefix[20],temp[20];
prefix[0]='\0';
s.tos=0;
s.ele[s.tos]='(';
length=strlen(infix);
for(i=0;i<length;i++)
{
switch(infix[i])
{
case '*':
case '+':
case '-':
case '/':
case '^':
case '(':
{
a=isp(s.ele[s.tos]);
b=icp(infix[i]);
if(a>=b)
{
ch=pop();
temp[0]=ch;
temp[1]='\0';
strcat(temp,prefix);
strcpy(prefix,temp);
s.tos++;
push(infix[i]);
}
else
{
s.tos++;
push(infix[i]);
}
break;
}
case ')':
{
ch=pop();
while(ch!='(')
{
temp[0]=ch;
temp[1]='\0';
strcat(temp,prefix);
strcpy(prefix,temp);
ch=pop();
}
break;
}
default:
{
len=strlen(prefix);
prefix[len]=infix[i];
prefix[++len]='\0';
}
}}
printf("%s",temp);
}

push(char ch)
{
s.ele[s.tos]=ch;
}

pop()
{
char ch;
ch=s.ele[s.tos];
s.tos--;
return(ch);
}

int isp(char a)
{
switch(a)
{
case '^':return(3);
case '*':
case '/':return(2);
case '+':
case '-':return(1);
case '(':return(0);
}}

int icp(char a)
{
switch(a)
{
case '^':return(4);
case '*':
case '/':return(2);
case '+':
case '-':return(1);
case ')':return(4);
}}

FORMING A TREE USING PREFIX EXPRESSION

/* FORMING A TREE USING PREFIX EXPRESSION AND THEN PREFORM
ALL THE TRAVERSALS(RECURSIVE) */


#include<stdio.h>
#include<ctype.h>
struct tree
{
char data;
struct tree *left,*right;
};
typedef struct tree btree;
char prefix[20];
btree *stack[20];
int top;
main()
{
btree *create(void);
void inorder(btree *temp);
void preorder(btree *temp);
void postorder(btree *temp);
void push(btree *temp);
btree *pop(void);
btree *root;
clrscr();
printf("PLEASE ENTER THE PREFIX EXPRESSION:");
gets(prefix);
root=create();
printf("\nTHE INORDER TRAVERSAL IS:");
inorder(root);
printf("\nTHE PREORDER TRAVERSAL IS:");
preorder(root);
printf("\nTHE POSTORDER TRAVERSAL IS:");
postorder(root);
getch();
}

btree *create(void)
{
btree *head,*temp,*temp1;
int i=0,n;
top=-1;
head=(btree*)malloc(sizeof(btree));
temp=head;
temp->data=prefix[i];
i++;
temp->right=temp->left=NULL;
push(temp);
n=strlen(prefix);
while(top!=-1)
{
while(!isalnum(prefix[i]))
{
temp=pop();
temp1=(btree*)malloc(sizeof(btree));
temp1->right=temp1->left=NULL;
temp->left=temp1;
temp1->data=prefix[i];
i++;
push(temp);
push(temp1);
}
temp1=(btree*)malloc(sizeof(btree));
temp=pop();
temp->left=temp1;
temp1->data=prefix[i];
temp1->left=temp1->right=NULL;
push(temp);
do {
temp=pop();
i++;
temp1=(btree*)malloc(sizeof(btree));
temp->right=temp1;
temp1->data=prefix[i];
temp1->right=temp1->left=NULL;
if(!isalpha(prefix[i]))
{i++;
push(temp1);
break;}
}while(top!=-1);
}
return(head);
}

void push(btree *temp)
{
top++;
stack[top]=temp;
return;
}
btree *pop(void)
{
return(stack[top--]);
}

void inorder(btree *temp)
{
if(temp)
{
inorder(temp->left);
putch(temp->data);
inorder(temp->right);
}
return;
}

void preorder(btree *temp)
{
if(temp)
{
putch(temp->data);
preorder(temp->left);
preorder(temp->right);
}
return;
}

void postorder(btree *temp)
{
if(temp)
{
postorder(temp->left);
postorder(temp->right);
putch(temp->data);
}
return;
}

PROGRAM TO SWAP TWO NUMBERS WITHOUT USING A THIRD VARIABLE IN C PROGRAMMING

PROGRAM TO SWAP TWO NUMBERS WITHOUT USING A THIRD VARIABLE IN C PROGRAMMING

This eveyone computer science student must have encountered.

void main()
{
int a,b;
printf("PLEASE ENTER THE TWO NUMBERS:");
scanf("%d %d",&a,&b);
/*little bit of maths*/
a=a+b;
b=a-b;
a=a-b;
printf("THE SWAPPED NUMBERS ARE:");
printf("%d %d",a,b);
getch();
}

Stack implementation using Array - c program (stack operation)

Stack implementation using Array - c program (stack operation)
===================================================


#include <stdio.h>
#include <conio.h>
#define size 10

int top = -1;
int stack[size];
int flag = 0;

void push (int *,int);
int pop (int *);
int peep (int *,int);
int update (int *,int);
void display (int *);

void main ()
{
int info,popped_element,position,data,pos_update,update_element,update_data;
char ch;

clrscr ();

do
{
printf("\n\ni<-Insert(push), d<-Delete(pop),p<-peep,u<-update, q<-Quit\n");
fflush (stdin);
printf("\n\nEnter your choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
printf("\n\nEnter the element you want to push: ");
scanf("%d",&info);
push (stack,info);
display (stack);
break;
}

case 'd':
{
popped_element = pop(stack);

if (flag == 0)
printf("\n\nThe element popped is %d",popped_element);
if (flag == 1)
printf("\nStack Underflow");
display (stack);
break;
}

case 'p':
{
printf("\n\nEnter the position of the stack from which you want to peep the element: ");
scanf("%d",&position);
data = peep(stack,position);
if (flag == 1)
printf("\nInvalid choice\n");
else
printf("The element peeped is %d",data);
display (stack);
break;
}

case 'u':
{
printf("Enter the position of the stack from which you want to update the data: ");
scanf("%d",&pos_update);
update_element = update(stack,pos_update);
if (flag == 1)
printf("Data can't be updated due to some reason");
else
printf("The data updated is %d",update_element);
display (stack);
break;
}

case 'q':
exit(0);
}
}while (ch != 'q');

getch ();
}

void push (int stack[],int info)
{
if (top == size-1)
printf("\nStack Overflow");
else
{
top++;
stack[top] = info;
}
}

int pop (int stack[])
{
int popped_element;
if (top == -1)
flag = 1;
else
{
flag = 0;
popped_element = stack[top];
top--;
}
return (popped_element);
}

void display (int stack[])
{
int i;
printf("\n\nThe contents of the stack are:\n");
for (i = top ; i >= 0 ; i--)
printf("%d ",stack[i]);
}

int peep (int stack[],int i)
{
int data;
if (top - i + 1 < 0)
flag = 1;
else
{
flag = 0;
data = stack[top-i+1];
}
return (data);
}

int update(int stack[],int pos)
{
int old_data;
if (top - pos + 1 < 0)
flag = 1;
else
{
flag = 0;
old_data = stack[top-pos+1];
printf("Enter the data you want to insert: ");
scanf("%d",&stack[top-pos+1]);
}
return (old_data);
}


Stack implementation using link list - c program
==============================

#include <stdio.h>
#include <conio.h>
struct Stack
{
int info;
struct Stack *next;
};
typedef struct Stack node;
node *first = NULL;
node *push (node *);
node *pop (node *);
void display (node *);

void main ()
{
char ch;

clrscr ();

do
{
fflush (stdin);
printf("\n\ni<-insert(push), d<-delete(pop), q<-quit\n");
printf("\n\nEnter the choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
first = push (first);
printf("\n\nAfter the push operation the stack is as follows:\n");
display (first);
break;
}

case 'd':
{
first = pop (first);
printf("\n\nAfter the pop operation the stack is as follows:\n");
display (first);
break;
}

case 'q':
exit (0);
}
}while (ch != 'q');

getch ();
}

node *push(node *first)
{
node *data;
data = (node *)malloc(sizeof(node));
printf("\nEnter the data to be inserted: ");
scanf("%d",&data->info);
data->next = first;
first = data;
return (first);
}

node *pop (node *first)
{
node *temp;
if (first == NULL)
printf("\n\nSTACK IS EMPTY\n");
else
{
temp = (node *)malloc(sizeof(node));
temp = first;
first = first->next;
printf("\nThe element deleted is %d",temp->info);
free (temp);
}
return (first);
}

void display (node *first)
{
while (first != NULL)
{
printf("%d ",first->info);
first = first->next;
}
}

PROGRAMME TO ENTER GRAPHICS MODE AND PRINT SOMETHING

PROGRAMME TO ENTER GRAPHICS MODE AND PRINT SOMETHING

#include<stdio.h>
#include<graphics.h>
main()
{
int ga=DETECT,gm;
initgraph(&ga,&gm," ");
settextstyle(0,0,100);
outtextxy(70,150,"PRAGATI PRAKASHAN");
getch();
closegraph();
}

PROGRAMME TO REVERSE A DOUBLY LINKED LINK LIST

/* PROGRAMME TO REVERSE A DOUBLY LINKED LINK LIST */


#include<stdio.h>
struct llist
{
int val;
struct llist *next,*prev;
};
typedef struct llist list;

main()
{
list *create(void);
void reverse(list *temp);
list *root;
clrscr();
printf("PLEASE ENTER THE LIST,ENTER -99 TO END:");
root=create();
reverse(root);
getch();
}

list *create(void)
{
list *head,*temp,*temp1;
int data;
head=(list*)malloc(sizeof(list));
temp=head;
head->prev=NULL;
do
{
scanf("%d",&data);
if(data==-99)
return(head);
temp1=(list*)malloc(sizeof(list));
temp1->val=data;
temp->next=temp1;
temp1->prev=temp;
temp1->next=NULL;
temp=temp1;
}while(1);
}

void reverse(list *head)
{
list *t1,*t2,*t3;
t1=head->next;
t2=t1->next;
t3=head;
do
{
t1->next=t3;
t1->prev=NULL;
head->next=t2;
t3->prev=t1;
t3=t1;
t1=t2;
t2=t2->next;
}while(t1!=NULL);
printf("\nTHE REVERSED LIST IS:");
t1=t3;
while(t1!=head)
{
printf(" %d",t1->val);
t1=t1->next;
}
return;
}

Link list programs in C (single, doubly and circular link list)

Link list programs in C (single, doubly and circular link list)
--------------------------------------------------------------------------------------



Singly link list
=================


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

struct list
{
int info;
struct list *next;
};
typedef struct list node;

void main ()
{
node *first;
int choice;
node *create (node *);
node *insert (node *);
node *del (node *);
void display (node *);

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

node *create (node *first)
{
node *new1 = NULL,*l;
int i;
first = NULL;
printf("\n\nTHE INPUT IS:\n");
printf("-------------\n\n");
while(1)
{
printf("\nEnter -1 to break.....\n");
printf("Enter the node you want to enter in ascending order: ");
scanf("%d",&i);

if (i == -1)
break;

else
{
new1 = ((node *) malloc (sizeof (node)));
new1 -> info = i;
if(first == NULL)
{
first = l = new1;
}
else
{
l -> next = new1;
l = new1;
}
l -> next = NULL;
}
}
return (first);
}

void display (node *first)
{
printf("\n\nTHE OUTPUT IS:\n");
printf("--------------\n\n");
while (first != NULL)
{
printf("%d -> ",first -> info);
first = first -> next;
}
printf("NULL");
}

node *insert (node *first)
{
node *l, *new1;
l = first;
new1 = (node *) malloc (sizeof (node *));
printf("Enter the node: ");
scanf("%d",&new1 -> info);
if (first == NULL)
{
first = new1;
first -> next = NULL;
}
else
{
if (new1 -> info < first -> info)
{
new1 -> next = first;
first = new1;
}
else
{
while (l -> next -> info < new1 -> info && l -> next != NULL)
l = l -> next;
new1 -> next = l -> next;
l -> next = new1;
l = new1;
}
}
display (first);
return (first);
}

node *del (node *first)
{
int n;
node *l = first, *new1, *temp;
printf("Enter the information: ");
scanf("%d",&n);
if (first == NULL)
printf("\nLIST IS EMPTY\n");
else
{
if (first -> info == n)
{
temp = first;
first = first -> next;
free (temp);
}
else
{
while (l -> next -> info != n && l -> next != NULL)
l = l -> next;
if (l -> next == NULL)
printf("\nINFORMATION IS NOT PRESENT\n");
else
{
temp = l -> next;
l -> next = l -> next -> next;
free(temp);
}
}
}
display (first);
return (first);
}

Doubly Link List
===============

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

struct Double
{
int info;
struct Double *next;
struct Double *previous;
};
typedef struct Double node;

node *create(node *);
node *insert(node *);
node *del(node *);
void display (node *);

node *first = NULL;

void main ()
{
int choice;

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

node *create (node *first)
{
node *new1,*l = NULL;
int i;
//first -> next = first -> previous = NULL;
while (1)
{
printf("Enter the information(-1 to Exit): ");
scanf("%d",&i);
if (i == -1)
break;
new1 = (node *)malloc(sizeof(node));
new1 -> info = i;
if (first == NULL)
{
first = l = new1;
first -> next = first -> previous = NULL;
}
else
{
new1 -> previous = l;
l -> next = new1;
l = new1;
}
l -> next = NULL;
}
return (first);
}

void display (node *first)
{
node *l;
l = first;
printf("\n\n\t\tDOUBLY LINK LIST:\n");
printf("\t\t-----------------\n");
printf("NULL");
for (l = first ; l != NULL ; l = l -> next)
printf(" <- %d -> ",l -> info);
printf("NULL");
}

node *insert (node *first)
{
node *new1,*l=NULL;
new1 = (node *)malloc(sizeof(node));
printf("\n\nEnter the node you want to insert: ");
scanf("%d",&new1->info);
if (first == NULL)
{
printf("Empty list. New list will be created.");
first = new1;
first->next = NULL;
first->previous = NULL;
}
else
{
if (new1->info < first->info)
{
new1->next = first;
new1->previous = NULL;
first->previous = new1;
first = new1;
}
else
{
l = first;
while (l->next->info < new1->info && l->next != NULL)
l = l->next;
new1->next = l->next;
l->next = new1;
new1->previous = l;
l = new1;
}
}
display (first);
return (first);
}

node *del (node *first)
{
int del_info;
node *l=NULL,*temp;
printf("\n\nEnter the information to be deleted: ");
scanf("%d",&del_info);
if (first == NULL)
printf("The List is Empty.");
else
{
if (first->info == del_info)
{
temp = first;
first = first->next;
first->previous = NULL;
free(temp);
}
else
{
l = first;
while (l->info < del_info && l->next != NULL)
l = l->next;
l->previous->next = l->next;
l->next->previous = l->previous;
}
}
display (first);
return (first);
}

Circular link list
=================


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

///////////////////////STRUCTURE DEFINITION////////////////////////////////
struct cir_link_list
{
int info;
struct cir_link_list *next;
};
typedef struct cir_link_list node;

///////////////////////VARIABLE DECLARATION////////////////////////////////
node *first=NULL;
int info;

///////////////////////FUNCTION DECLARATION////////////////////////////////
node *create (node *);
void display (node *);
node *insert (node *);
node *del (node *);
int count = 0;

///////////////////////VOID MAIN () DEFINITION/////////////////////////////
void main ()
{
int choice;

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

///////////////////////CREATION OF CIRCULAR LINK LIST//////////////////////
node *create (node *first)
{
int i;
node *new1,*l=NULL;

while (1)
{
printf("Enter -1 to break........\n");
printf("Enter the node: ");
scanf("%d",&i);
if (i == -1)
{
l->next=first;
break;
}
else
{
new1 = ((node *)malloc(sizeof(node)));
new1 -> info = i;
if (first == NULL)
{
first = l = new1;
}
else
{
l->next=new1;
l=new1;
}
}
count++;
}
return (first);
}

///////////////////////DISPLAY OF CIRCULAR LINK LIST///////////////////////
void display (node *first)
{
int i;
for (i=0 ; i< count ; i++)
{
printf("%d -> ",first->info);
first = first -> next;
}
}

node *insert (node *first)
{
int i = 0;
node *l, *new1;
l = first;
new1 = (node *) malloc (sizeof (node *));
printf("\nEnter the node: ");
scanf("%d",&new1 -> info);
if (first == NULL)
{
first = new1;
first -> next = first;
}
else
{
if (new1 -> info < first -> info)
{
new1 -> next = first;
first = new1;
}
else
{
while (l -> next -> info < new1 -> info && l -> next != first && i <= count)
{
l = l -> next;
i++;
}
if (l -> next == first i > count)
{
new1 -> next = l -> next;
l -> next = new1;
l = new1;
}
new1 -> next = l -> next;
l -> next = new1;
}
}
count++;
display (first);
return (first);
}

node *del (node *first)
{
int n,i=0;
node *l = first, *new1, *temp;
printf("\nEnter the information: ");
scanf("%d",&n);
if (first == NULL)
printf("\nLIST IS EMPTY\n");
else
{
if (first -> info == n)
{
temp = first;
first = first -> next;
free (temp);
}
else
{
while (l -> next -> info != n && l -> next != first && i <= count)
{
l = l -> next;
i++;
}
if (l -> next == first i > count)
{
printf("\nINFORMATION IS NOT PRESENT\n");
goto jmp;
}
else
{
temp = l -> next;
l -> next = l -> next -> next;
free(temp);
}
}
}
count --;
jmp:
display (first);
return (first);
}

insert and delete queue in C++ (queue operation)

insert and delete queue in C++ (queue operation)
=======================================


# include<iostream.h>
# include<conio.h>
# include<string.h>
# include<ctype.h>
# include<process.h>
# define size 10
class Queue
{
public: int rear, front;
int ch;
int q[size];

public: Queue()
{
rear = front = 0;
}
void Insert_queue();
void Delete_queue();
void Display_queue();

};

// Function to create queue
void Queue :: Insert_queue()
{
cout<<"\n Input the element :";
cin>>ch;
if(rear < size)
{
rear ++;
q[rear] = ch ;
if(front == 0)
front = 1;
}
else
cout<<"\nOverflow";
}

// Function to perform delete operation
void Queue :: Delete_queue() //char q[], char ch)
{
if (front == 0)
{
cout<<"\nUnderflow";
return ;
}
else
{
ch = q[front];
cout<<"\nElement deleted :"<<ch;
}
if(front == rear)
{
front = 0;
rear = 0;
}
else
front = front + 1;
}

// Output function
void Queue :: Display_queue() //char q[])
{
if (front == 0)
return;
for( int i = front ; i <= rear; i++)
cout<<" "<<q[i];
}

//Function main
void main()
{
Queue Q;
int k = 0;
char choice;

do
{
cout<<"\nInsert->i Delete->d Quit->q:";
cout<<"\nInput the choice : ";
do
{
cin>>choice;
choice = tolower(choice);
}
while(strchr("idq",choice)==NULL);
cout<<"Your choice is ->"<<choice;
switch(choice)
{
case 'i' :
Q.Insert_queue();
cout<<"\nQueue after inserting ";
Q.Display_queue();
break;

case 'd' :
Q.Delete_queue();
cout<<"\nQueue content after deleteion is as follows:\n";
Q.Display_queue();
break;
case 'q':
k = 1;
}
}
while(!k);
}

Demonstrate use of mouse in C (assembly)

Demonstrate use of mouse in C (assembly)

/* PROGRAMME TO DEMONSTRATE USE OF MOUSE IN C LANGUAGE */
/* FORMING OUR OWN CURSOR */

#include<stdio.h>
#include<graphics.h>
#include<dos.h>
union REGS inregs;
struct SREGS sinregs;
static int mask[]={
0xe1ff,
0xe1ff,
0xe1ff,
0xe1ff,
0xe1ff,
0xe000,
0xe000,
0xe000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x1e00,
0x1200,
0x1200,
0x1200,
0x13ff,
0x1249,
0x1249,
0xf249,
0x9001,
0x9001,
0x9001,
0x8001,
0x8001,
0x8001,
0xffff
};
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
cleardevice();
inregs.x.ax=0;
int86(0x33,&inregs,&inregs);
inregs.x.ax=1;
int86(0x33,&inregs,&inregs);
inregs.x.ax=9;
inregs.x.bx=5;
inregs.x.cx=0;
inregs.x.dx=(int)mask;
segread(&sinregs);
sinregs.es=sinregs.ds;
int86x(0x33,&inregs,&inregs,&sinregs);
getch();
closegraph();
}

Circular queue in C - program

Circular queue in C (program)
========================


#include <stdio.h>
#include <conio.h>
#define size 10

int queue[size];
int front = 0;
int rear = 0;
int flag = 0;

int insert (int []);
int del (int []);
void display (int []);

void main ()
{
char ch;
int insert_element,del_element;

clrscr ();

do
{
fflush (stdin);
printf("\n\ni<-insert, d<-delete, q<-quit\n");
printf("\n\nEnter your choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
if (flag == 1)
printf("\nOVERFLOW\n");
else
{
insert_element = insert(queue);
printf("\n\nElement inserted is %d",insert_element);
printf("\n\nAfter the insertion the contents of the queue are:\n");
display (queue);
}
break;
}

case 'd':
{
del_element = del (queue);
if (flag == 1)
printf("\nUNDERFLOW\n");
else
{
printf("\n\nThe element deleted is %d",del_element);
printf("\n\nAfter the deletion the contents of the queue are:\n");
display (queue);
}
break;
}

case 'q':
exit (0);
}
}while (ch != 'q');

getch ();
}

int insert (int cq[])
{
int info;
printf("Enter the information you want to insert: ");
scanf("%d",&info);
if (rear == size)
rear = 1;
else
rear = rear + 1;
if (front == rear)
{
flag = 1;
return;
}
cq[rear] = info;
if (front == 0)
front = 1;
return (cq[rear]);
}

int del (int cq[])
{
int info;
if (front == 0)
{
flag = 1;
return;
}
info = cq[front];
if (front == rear)
{
front = rear = 0;
return (info);
}
if (front == size)
front = 1;
else
front = front + 1;
return (info);
}

void display (int cq[])
{
int i;
for (i = front ; i <= rear ; i++)
printf("%d ",cq[i]);
}

Saturday, August 25, 2007

point clipping algorithm in C

point clipping algorithm in C


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

void initgraph1() {
int gd, gm;

gd=DETECT;
initgraph(&gd,&gm,"");
}

void main() {
int xmin, xmax, ymin, ymax;
int x[20],y[20],n,i;
clrscr();
printf("\tProgram for point clipping\n");
printf("\nEnter xmin, xmax, ymin, ymax");

scanf("\n %d", &xmin);
scanf("\n %d", &xmax);
scanf("\n %d", &ymin);
scanf("\n %d", &ymax);
printf("\n Enter no of point to be clipped");
scanf("\n %d",&n);
for(i=0;i<n;i++){
printf("Enter x for point %d :",i+1);
scanf("%d",&x[i]);
printf("\nEnter y for point %d :",i+1);
scanf("%d",&y[i]);
}

initgraph1();
/*before clipping */
outtextxy(0,0, "BEFORE CLIPPING");
rectangle(xmin, ymin, xmax, ymax);
for(i=0;i<n;i++) {
putpixel(x[i],y[i],1);
}
outtextxy(50,50, "presss a key to continue");
getch();
/*after clipping */
cleardevice();
outtextxy(0,0, "AFTER CLIPPING");
rectangle(xmin, ymin, xmax, ymax);
for(i=0;i<n;i++) {
if ((x[i] >= xmin && x[i] <=xmax) ( y[i] >=ymin && y[i] <=ymax)) {
putpixel(x[i],y[i],1);

}
else {
/*clipping */

}
}

getch();
closegraph();

}