Program for calculating power of a number

/* Problem : sol4_3_2.c
Solution : */




#include
#include


void main()
{
double power(double,int);
double n,ans;
int p;
clrscr();

printf("\n\n\n\t\t\t Program for calculating power of a number");


printf("\n\n\n\t\t\t Enter the base number = ");
scanf("%lf",&n);

printf("\n\t\t\t Enter the power = ");
scanf("%d",&p);

ans=power(n,p);

printf("\n\n\t\t\t Result of %.2lf to power %d is %.2lf",n,p,ans);

getch();

}

double power(double nn,int pp)
{
if(pp==0)
return (1);
else
return(nn*power(nn,pp-1));
}

Related Links :

Program for calculating sum of first 'n' number

#include
#include

void main()
{
int sum();
int n,ans ;
clrscr();
printf("\n\n\n\t\t Program for calculating sum of first 'n' number");


printf("\n\n\n\t\t\t Sum of how many numbers you want ? = ");
scanf("%d",&n);

ans=sum(n);

printf("\n\n\t\t\t Sum of first %d number(1 to %d) is %d",n,n,ans);

getch();

}

int sum(int n)
{
if(n==0)
return(0);
else
return(n+sum(n-1));

}

Related Links :

Program for searching a character in a string

/* Problem : sol5_1_3.c
Solution : */



#include
#include

void main()
{
char str[20],ch;
int cnt=0,i;
clrscr();

printf("\n\n\n\t\t Program for searching a character in a string");

printf("\n\n\n\t\t\t Enter a string = ");
scanf("%s",&str);

printf("\n\t\t\t Enter the character to search = ");
flushall();
scanf("%c",&ch);

for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
cnt++;

}

if(cnt==0)
printf("\n\n\t\t\t The character '%c' is not found in the string...",ch);
else
printf("\n\n\t\t\t The character '%c' is occuring %d times in the string.",ch,cnt);


getch();

}

Related Links :

Program for printing Pascal's Triangle

void main()
{
int arr[10][10];
int i,j,k;
clrscr();
printf("\n\n\n\t\t\t Program for printing Pascal's Triangle");
for(i=0;i<10;i++)
{
j=1;
arr[i][0]=1;
arr[i][i]=1;

while(j<=i)
{
arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
j++;
}
}
for(i=0;i<10;i++)
{
j=10;
while(j>i)
{
printf(" ");
j--;
}
for(k=0;k<=i;k++)
{
printf("%4d",arr[i][k]);

}
printf("\n\n");
}
getch();
}

Related Links :

Controling digital circuit through computer

#include
#include
#include
#include
#include
#include
#define port 0x0378

char ch;
void display(int,int,int,int);
void dispbutton(int);
void click(int,int,int,int);
void status(int,int,int,int);
void reset(int*,int*,int*,int*);

int x,y,button;
union REGS i,o;

initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}

void showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}

void getmousepos(int *button, int *x,int *y)
{
i.x.ax =3;
int86(0x33,&i,&o);
*button =o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}

void hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
}


void main()
{clrscr();
int s1=0,s2=0,s3=0,s4=0;
int gdriver=DETECT,gmode,ercode;
initgraph(&gdriver,&gmode,"");
ercode=graphresult();
if(ercode!=0){printf("error code:%s",grapherrormsg(ercode));
getch();}
display(s1,s2,s3,s4);
initmouse();showmouseptr();
while(ch!=27)
{getmousepos(&button,&x,&y);
if(button==1)
{if(x>80&&x<180&&y>100&&y<140)ch='1';
if(x>200&&x<300&&y>100&&y<140)ch='2';
if(x>320&&x<420&&y>100&&y<140)ch='3';
if(x>440&&x<540&&y>100&&y<140)ch='4';
if(x>440&&x<540&&y>300&&y<340)ch=32;
if(x>80&&x<180&&y>300&&y<340)ch=27;

}


if(kbhit())ch=getch();
switch(ch)
{case '1':{s1=!s1;
click(s1,s2,s3,s4);
outportb(port,1);delay(500);outport(port,0);
ch='0';break;
}
case '2':{s2=!s2;
click(s1,s2,s3,s4);
outportb(port,2);delay(500);outport(port,0);
ch='0';break;
}
case '3':{s3=!s3;
click(s1,s2,s3,s4);
outportb(port,4);delay(500);outport(port,0);
ch='0';break;
}
case '4':{s4=!s4;
click(s1,s2,s3,s4);
outportb(port,8);delay(500);outport(port,0);
ch='0';break;
}
case 32:{click(s1,s2,s3,s4);
reset(&s1,&s2,&s3,&s4);
ch='0';break;
}
case 27: {click(s1,s2,s3,s4);
reset(&s1,&s2,&s3,&s4);
closegraph();exit(0);}
}

}
}

void display(int s1,int s2,int s3,int s4)
{setbkcolor(9);setcolor(1);
rectangle(5,5,635,475);rectangle(10,10,630,470);
dispbutton(1);dispbutton(2);dispbutton(3);dispbutton(4);
dispbutton(5);dispbutton(6);
status(s1,s2,s3,s4);
setcolor(1);
outtextxy(100,115,"SWITCH 1");
outtextxy(220,115,"SWITCH 2");
outtextxy(340,115,"SWITCH 3");
outtextxy(460,115,"SWITCH 4");
outtextxy(115,315,"EXIT");
outtextxy(470,315,"RESET");
}

void dispbutton(int n)
{int x1,y1,x2,y2;
if(n==1){x1=80;y1=100;x2=180;y2=140;}
if(n==2){x1=200;y1=100;x2=300;y2=140;}
if(n==3){x1=320;y1=100;x2=420;y2=140;}
if(n==4){x1=440;y1=100;x2=540;y2=140;}
if(n==5){x1=80;y1=300;x2=180;y2=340;}
if(n==6){x1=440;y1=300;x2=540;y2=340;}
setfillstyle(SOLID_FILL,7);
bar(x1,y1,x2,y2);
setcolor(15);
line(x1,y1,x2,y1);line(x1,y1,x1,y2);
setcolor(8);
line(x2,y1,x2,y2);line(x1,y2,x2,y2);
}

void click(int s1,int s2,int s3,int s4)
{int x1,y1,x2,y2;
if(ch=='1'){x1=80;y1=100;x2=180;y2=140;}
if(ch=='2'){x1=200;y1=100;x2=300;y2=140;}
if(ch=='3'){x1=320;y1=100;x2=420;y2=140;}
if(ch=='4'){x1=440;y1=100;x2=540;y2=140;}
if(ch==27){x1=80;y1=300;x2=180;y2=340;}
if(ch==32){x1=440;y1=300;x2=540;y2=340;}
hidemouseptr();
setcolor(15);line(x2,y1,x2,y2);line(x1,y2,x2,y2);
setcolor(8);line(x1,y1,x2,y1);line(x1,y1,x1,y2);
sound(50);delay(75);nosound();
setcolor(15);line(x1,y1,x2,y1);line(x1,y1,x1,y2);
setcolor(8);line(x2,y1,x2,y2);line(x1,y2,x2,y2);
showmouseptr();
status(s1,s2,s3,s4);

}

void status(int s1,int s2,int s3,int s4)
{setcolor(4);setfillstyle(SOLID_FILL,4);
circle(130,200,10);
circle(250,200,10);
circle(370,200,10);
circle(490,200,10);
if(s1==1)floodfill(130,200,4);
else {setcolor(0);setfillstyle(SOLID_FILL,0);circle(130,200,10);
floodfill(130,200,0);
setcolor(4);circle(130,200,10);setfillstyle(SOLID_FILL,4);
}
if(s2==1)floodfill(250,200,4);
else {setcolor(0);setfillstyle(SOLID_FILL,0);circle(250,200,10);
floodfill(250,200,0);
setcolor(4);circle(250,200,10);setfillstyle(SOLID_FILL,4);
}
if(s3==1)floodfill(370,200,4);
else {setcolor(0);setfillstyle(SOLID_FILL,0);circle(370,200,10);
floodfill(370,200,0);
setcolor(4);circle(370,200,10);setfillstyle(SOLID_FILL,4);
}
if(s4==1)floodfill(490,200,4);
else {setcolor(0);setfillstyle(SOLID_FILL,0);circle(490,200,10);
floodfill(490,200,0);
setcolor(4);circle(490,200,10);
}
}

void reset(int *s1,int *s2,int *s3,int *s4)
{if(*s1==1)
{outportb(port,1);delay(500);outport(port,0);}
if(*s2==1)
{outportb(port,2);delay(500);outport(port,0);}
if(*s3==1)
{outportb(port,4);delay(500);outport(port,0);}
if(*s4==1)
{outportb(port,8);delay(500);outport(port,0);}
*s1=0;*s2=0;*s3=0;*s4=0;
status(*s1,*s2,*s3,*s4);
}

Related Links :

Calendar Program In C

/*calendar Program In C*/
#include
#include
#include
int getNumberOfDays(int month,int year)
{
switch(month)
{
case 1 : return(31);
case 2 : if(year%4==0)
return(29);
else
return(28);
case 3 : return(31);
case 4 : return(30);
case 5 : return(31);
case 6 : return(30);
case 7 : return(31);
case 8 : return(31);
case 9 : return(30);
case 10: return(31);
case 11: return(30);
case 12: return(31);
default: return(-1);
}
}
char *getName(int odd)
{
switch(odd)
{
case 0 :return("Sunday");
case 1 :return("Monday");
case 2 :return("Tuesday");
case 3 :return("Wednesday");
case 4 :return("Thursday");
case 5 :return("Friday");
case 6 :return("Saturday");
default:return("Error in getName() module.Invalid argument
passed");
}
}
int getOddNumber(int day,int mon,int year)
{
int res=0,t1,t2,y=year;
year = year-1600;
while(year>=100)
{
res=res+5;
year=year-100;
}
res=(res%7);
t1=((year-1)/4);
t2=(year-1)-t1;
t1=(t1*2)+t2;
t1=(t1%7);
res = res+t1;
res=res%7;
t2=0;
for(t1=1;t1 {
t2+=getNumberOfDays(t1,y);
}
t2 = t2+day;
t2 = t2%7;
res = res+t2;
res = res%7;
if(y>2000)
res=res+1;
res = res%7;
return res;
}
char *getWeek(int dd,int mm,int yy)
{
int odd;
if(!(mm>=1 && mm<=12))
{
return("Invalid month value");
}
if(!(dd>=1 && dd<=getNumberOfDays(mm,yy)))
{
return("Invalid date");
}
if(yy>=1600)
{
odd = getOddNumber(dd,mm,yy);
odd=odd%7;
return(getName(odd));
}
else
{
return("
Please give year more than 1600");
}
}
void printMonth(int mon,int year,int x,int y)
{
int nod,odd,cnt,d=1,x1=x,y1=y;
if(!(mon>=1 && mon<=12))
{
printf("
INVALID MONTH");
getch();
return;
}
if(!(year>=1600))
{
printf("
INVALID YEAR");
getch();
return;
}
if(x<=0)
x=wherex();
if(y<=0)
y=wherey();
gotoxy(x,y);
textcolor(RED);
cprintf("S");
textcolor(YELLOW);
cprintf(" M T W T F S");
/* 1234567891234567891234567 */
textcolor(7);
cprintf("");
y++;
nod=getNumberOfDays(mon,year);
odd=getOddNumber(d,mon,year);
switch(odd)
{
case 0 : x=x;
cnt=1;
break;
case 1 : x=x+4;
cnt=2;
break;
case 2 : x=x+8;
cnt=3;
break;
case 3 : x=x+12;
cnt=4;
break;
case 4 : x=x+16;
cnt=5;
break;
case 5 : x=x+20;
cnt=6;
break;
case 6 : x=x+24;
cnt=7;
break;
default : printf("

INVALID DATA FROM THE getOddNumber()
MODULE");
return;
}
gotoxy(25,25);
gotoxy(x,y);
printf("%02d",d);
for(d=2;d<=nod;d++)
{
if(cnt%7==0)
{
y++;
cnt=0;
x=x1-4;
}
x = x+4;
cnt++;
gotoxy(x,y);
printf("%02d",d);
}
}
main()
{
char ch='k';
int dd,mm,yy;
while(ch!='0')
{
clrscr();
printf("




1.Know the day");
printf("
2.Print the month");
printf("
0.EXIT");
printf("

ENTER YOUR CHOICE : ");
flushall();
fflush(stdin);
ch=getche();
clrscr();
switch(ch)
{
case '1': printf("Enter date (DD MM YYYY) : ");
scanf("%d %d %d",&dd,&mm,&yy);
printf("
Day is : %s",getWeek(dd,mm,yy));
flushall();
getch();
break;
case '2' : printf("Enter month and year (MM YYYY) : ");
scanf("%d %d",&mm,&yy);
printf("

");
printMonth(mm,yy,-1,-1);
flushall();
getch();
break;
case '0' : exit(0);
}
}
}

Related Links :

TIC TAC

#include
#include

void Board();
void PlayerX();
void PlayerO();
void Player_win();
void check();
int win=0,wrong_X=0,wrong_O=0,chk=0;

char name_X[30];
char name_O[30];
int pos_for_X[3][3];
int pos_for_O[3][3];
int pos_marked[3][3];

void main()
{
int i,ch,j;
char ans;
/* clrscr();
printf("\n\t\t\t\tTIC TAC TOE");
printf("\n\t\t\t\t");
for(i=1;i<=11;i++)
{
delay(10000);
printf("*");
}*/
do
{
clrscr();
printf("\n\t\t\t\tTIC TAC TOE");
printf("\n\t\t\t\t");
for(i=1;i<=11;i++)
{
delay(10000);
printf("*");
}
printf("\n1.Start The Game");
printf("\n2.Quit The Game");
printf("\nEnter your choice(1-2) : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
chk=0;
win=0;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
pos_for_X[i][j]=0;
pos_for_O[i][j]=0;
pos_marked[i][j]=0;
}
}
printf("\n\n");
clrscr();
printf("\nEnter the name of the player playing for \'X\': ");
fflush(stdin);
gets(name_X);
printf("\nEnter the name of the player playing for \'O\': ");
fflush(stdin);
gets(name_O);
Board();
for(;;)
{
if(win==1)
break;
check();
if(chk==9)
{
printf("\n\t\t\tMATCH DRAWS!!");
printf("\nPress any key....");
break;
}
else
chk=0;
printf("\nTURN FOR %s:",name_X);
PlayerX();
do
{
if(wrong_X!=1)
break;
wrong_X=0;
printf("\nTURN FOR %s:",name_X);
PlayerX();
}while(wrong_X==1);
check();
if(chk==9)
{
printf("\n\t\t\tMATCH DRAWS");
printf("\nPress any key....");
break;
}
else
chk=0;
printf("\nTURN FOR %s:",name_O);
PlayerO();
do
{
if(wrong_O!=1)
break;
wrong_O=0;
printf("\nTURN FOR %s:",name_O);
PlayerO();
}while(wrong_O==1);

}
Board();
if(win!=1)
{
printf("\n\t\t\tMATCH DRAWS!!");
printf("\nPress any key.......");
}
getch();
break;
case 2:
printf("\n\n\n\t\t\tThank You For Playing The Game.");
printf("\n\t\t\t###############################");
getch();
exit(1);
break;
}
printf("\nWant To Play(Y/N) ? ");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
}


void Board()
{
int i,j;
clrscr();
printf("\n\t\t\t\tTIC TAC TOE BOARD");
printf("\n\t\t\t\t*****************");
printf("\n\n\n");
printf("\n\t\t\t 1\t 2\t 3");
for(i=1;i<=3;i++)
{
printf("\n \t\t\t _____________________________");
printf("\n \t\t\tº\t º\t º\t º");
printf("\n\t\t%d\t",i);
for(j=1;j<=3;j++)
{

if(pos_for_X[i][j]==1)
{
printf(" X");
printf(" ");
}
else if(pos_for_O[i][j]==1)
{
printf(" O");
printf(" ");
}
else
{
printf(" ");
continue;
}
}
printf("\n\t\t\tº\t º\t º\t º");
}
printf("\n\t\t\t------------------------------");
Player_win();
}


void PlayerX()
{
int row,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
fflush(stdin);
scanf("%d",&row);
printf("Enter the column no. : ");
fflush(stdin);
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
{
printf("\nWRONG POSITION!! Press any key.....");
wrong_X=1;
getch();
Board();
}
else
{
pos_for_X[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
void PlayerO()
{
int row,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
scanf("%d",&row);
printf("Enter the column no. : ");
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
{
printf("\nWRONG POSITION!! Press any key....");
wrong_O=1;
getch();
Board();
}
else
{
pos_for_O[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
void Player_win()
{
int i;
for(i=1;i<=3;i++)
{
if(pos_for_X[i][1]==1 && pos_for_X[i][2]==1 && pos_for_X[i][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_X[1][i]==1 && pos_for_X[2][i]==1 && pos_for_X[3][i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
if(pos_for_X[1][1]==1 && pos_for_X[2][2]==1 && pos_for_X[3][3]==1)
{
win=1;
printf("\n\nRESULTL: %s wins!!",name_X);
printf("\nPress any key......");
return;
}
else if(pos_for_X[1][3]==1 && pos_for_X[2][2]==1 &&
pos_for_X[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key.....");
return;
}

for(i=1;i<=3;i++)
{
if(pos_for_O[i][1]==1 && pos_for_O[i][2]==1 && pos_for_O[i][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_O[1][i]==1 && pos_for_O[2][i]==1 && pos_for_O[3][i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
if(pos_for_O[1][1]==1 && pos_for_O[2][2]==1 && pos_for_O[3][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
else if(pos_for_O[1][3]==1 && pos_for_O[2][2]==1 &&
pos_for_O[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
void check()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(pos_marked[i][j]==1)
chk++;
else
continue;
}
}
}

Related Links :

JURASSIC PARK Program In C

#include
#include
#include
#include
#include
#include
#include
#include "file2.h"
#include "file3.cpp"
text1()
{
textcolor(0);
textbackground(15);
}
int i=1;
csd();
cgs();
es();
start();
li(int q);
int ch,d=0,q;
void main()
{
clrscr();
text1();
if(i==1)
{
start();
i++;
}
clrscr();
delay(1500);
cout<<"\nJURASSIC PARK CONTROL MODULE VERSION 4.1\n";
delay(500);
cout<<"--> CURRENT STATUS OF DINOSAURS......(1)\n";
delay(500);
cout<<"--> MAP OF JURASSIC PARK.............(2)\n";
delay(500);
cout<<"--> ELECTRICAL SUPPLIES..............(3)\n";
delay(500);
cout<<"--> LOG IN...........................(4)\n";
delay(500);
cout<<"--> EXIT.............................(PRESS ANY OTHER NUMERIC KEY)\n";
delay(500);
cout<<"ENTER CHOICE\n";
cin>>ch;
if (ch>=10)
{
cout<<"DO NOT ENTER LARGE NUMBERS";
delay(600);
main();
}
else if (ch==1)
{
while(d!=1)
{
d=li(q);
}
csd();
main();
}
else if(ch==2)
{
while(d!=1)
{
d=li(q);
}
cgs();
main();
}
else if(ch==3)
{
while(d!=1)
{
d=li(q);
}
es();
main();
}
else if(ch==4)
{
if(d==0)
{
d=li(q);
}
main();
}
else
exit(1);
getch();
}

Related Links :

Telephone Book Project

#include
#include
#include
#include
#include
#define null 0
void delay (int);

struct telephone_book
{
char name[20];
long unsigned int ph_no;
struct telephone_book *next;
};
typedef struct telephone_book T_BOOK;
T_BOOK *list=null;

void main()
{
T_BOOK *get_node(void);
int insert(T_BOOK *);
int display();
int modify(char []);
int del(char []);
int search(char []);
T_BOOK *node;
char name[20];

int choice;
clrscr();
printf("\n\n\n\t\t Telephone book using Link list");

do
{
printf("\n\n\n\t\t\t 1.Add a member");
printf("\n\n\t\t\t 2.Query a member");
printf("\n\n\t\t\t 3.Delete a member");
printf("\n\n\t\t\t 4.Modify a member");
printf("\n\n\t\t\t 5.List all member");
printf("\n\n\t\t\t 6.Exit");
printf("\n\n\n\t\t\t Enter your choice-> ");

scanf("%d",&choice);
fflush(stdin);

switch(choice)
{
case 1: node=get_node();
insert(node);
break;
case 2: printf("\n\n\t\t\t Enter the name to search-> ");
fflush(stdin);
scanf("%[^'\n']",name);
search(name);
break;
case 3: printf("\n\n\t\t\t Enter the name to delete :");
fflush(stdin);
scanf("%[^'\n']",name);
del(name);
break;
case 4: printf("\n\n\t\t\t Enter the name to modify :");
fflush(stdin);
scanf("%[^'\n']",name);
modify(name);
break;
case 5: display();
break;
case 6: exit(); break;
default : printf("\n\n\t\t\t Invalid choice...");break;

}
fflush(stdin);

} while(choice!=6);




getch();
}

T_BOOK *get_node()
{
T_BOOK *newnode;
newnode=(T_BOOK *)malloc(sizeof(T_BOOK));
newnode->next=null;
printf("\n\n\t\t\t Enter Name : ");
fflush(stdin);
scanf("%[^'\n']",newnode->name);
printf("\n\n\t\t\t Enter Phone no. : ");
fflush(stdin);
scanf("%lu",&newnode->ph_no);

return(newnode);



}

int insert(T_BOOK *node)
{
T_BOOK *ptr;
ptr=list;
if(ptr==null)
{
ptr=node;
list=ptr;
printf("\n\n\n\t\t\t New member aded...");
delay(1500);
return 1;
}
else
{
while(ptr->next!=null)
ptr=ptr->next;
ptr->next=node;
printf("\n\n\n\t\t\t New member aded...");
delay(1500);
return 1;
}
}


int display()
{
int i=1;
T_BOOK *ptr;
ptr=list;
printf("\n\n\t\t******* List of all your friends ******");
if(ptr==null)
{
printf("\n\n\t\t\t Phonebook is empty...");
delay(1500);
return 1;
}
while(ptr!=null)
{
printf("\n\n\n\t\t\t(%d) Name : %s",i++,ptr->name);
printf("\n\n\t\t\t Phone No. : %lu",ptr->ph_no);
ptr=ptr->next;
}
printf("\n\n\t\t***************************************");
delay(1500);
return 1;
}

int search(char name[])
{
T_BOOK *ptr;
ptr=list;

while(ptr!=null)
{
if(strcmpi(ptr->name,name)==0)
{
printf("\n\n\t\t\t Phone No. is = %lu",ptr->ph_no);
delay(1500);
return 1;
}
ptr=ptr->next;
}

printf("\n\n\t\t\t Name doesn't found....");
delay(1500);
return 0;
}


int del(char name[])
{
T_BOOK *ptr,*p;
ptr=list;
if(strcmpi(ptr->name,name)==0)
{
list=list->next;
printf("\n\n\n\t\t\t Data deleted...");
delay(1500);

return 1;
}
while(ptr!=null)
{

if(strcmpi(ptr->name,name)==0)
{
p->next=ptr->next;
free(ptr);
printf("\n\n\n\t\t\t Data deleted...");
delay(1500);
return 1;


}
p=ptr;
ptr=ptr->next;


}

printf("\n\n\t\t\t Name doesn't exist....");
delay(1500);
return 0;
}


int modify(char name[])
{
T_BOOK *ptr;
ptr=list;

while(ptr!=null)
{
if(strcmpi(ptr->name,name)==0)
{
printf("\n\n\t\t *** Persent Data *** ");
printf("\n\n\t\t\t Name : %s",ptr->name);
printf("\n\n\t\t\t Phone No. is = %lu",ptr->ph_no);
printf("\n\n\t\t *** Enter New Data ***");
printf("\n\n\n\t\t\t New Name : ");
fflush(stdin);
scanf("%[^'\n']",ptr->name);
printf("\n\n\t\t\t New Number : ");
scanf("%lu",&ptr->ph_no);

printf("\n\n\n\t\t\t Data modified...");
delay(1500);
return 1;

}
ptr=ptr->next;
}


printf("\n\n\t\t\t Name doesn't found....");
delay(1500);
return 0;

}
void delay (int x)
{

for(int i=0;i
}

Related Links :

Pointer types and Arrays

Okay, let's move on. Let us consider why we need to identify the type of variable that a pointer points to, as in:

int *ptr;

One reason for doing this is so that later, once ptr "points to" something, if we write:

*ptr = 2;

the compiler will know how many bytes to copy into that memory location pointed to by ptr. If ptr was declared as pointing to an integer, 2 bytes would be copied, if a long, 4 bytes would be copied. Similarly for floats and doubles the appropriate number will be copied. But, defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code. For example, consider a block in memory consisting if ten integers in a row. That is, 20 bytes of memory are set aside to hold 10 integers.

Now, let's say we point our integer pointer ptr at the first of these integers. Furthermore lets say that integer is located at memory location 100 (decimal). What happens when we write:

ptr + 1;


Because the compiler "knows" this is a pointer (i.e. its value is an address) and that it points to an integer (its current address, 100, is the address of an integer), it adds 2 to ptr instead of 1, so the pointer "points to" the next integer, at memory location 102. Similarly, were the ptr declared as a pointer to a long, it would add 4 to it instead of 1. The same goes for other data types such as floats, doubles, or even user defined data types such as structures. This is obviously not the same kind of "addition" that we normally think of. In C it is referred to as addition using "pointer arithmetic", a term which we will come back to later.

Similarly, since ++ptr and ptr++ are both equivalent to ptr + 1 (though the point in the program when ptr is incremented may be different), incrementing a pointer using the unary ++ operator, either pre- or post-, increments the address it stores by the amount sizeof(type) where "type" is the type of the object pointed to. (i.e. 2 for an integer, 4 for a long, etc.).

Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, this brings up an interesting relationship between arrays and pointers.

Consider the following:

int my_array[] = {1,23,17,4,-5,100};

Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, i.e. using my_array[0] through my_array[5]. But, we could alternatively access them via a pointer as follows:

int *ptr;
ptr = &my_array[0]; /* point our pointer at the first
integer in our array */

And then we could print out our array either using the array notation or by dereferencing our pointer. The following code illustrates this:

----------- Program 2.1 -----------------------------------

/* Program 2.1 from PTRTUT10.HTM 6/13/97 */

#include

int my_array[] = {1,23,17,4,-5,100};
int *ptr;

int main(void)
{
int i;
ptr = &my_array[0]; /* point our pointer to the first
element of the array */
printf("\n\n");
for (i = 0; i < 6; i++)
{
printf("my_array[%d] = %d ",i,my_array[i]); /*<-- A */
printf("ptr + %d = %d\n",i, *(ptr + i)); /*<-- B */
}
return 0;
}

Compile and run the above program and carefully note lines A and B and that the program prints out the same values in either case. Also observe how we dereferenced our pointer in line B, i.e. we first added i to it and then dereferenced the new pointer. Change line B to read:

printf("ptr + %d = %d\n",i, *ptr++);

and run it again... then change it to:

printf("ptr + %d = %d\n",i, *(++ptr));

and try once more. Each time try and predict the outcome and carefully look at the actual outcome.

In C, the standard states that wherever we might use &var_name[0] we can replace that with var_name, thus in our code where we wrote:

ptr = &my_array[0];

we can write:

ptr = my_array;

to achieve the same result.

This leads many texts to state that the name of an array is a pointer. I prefer to mentally think "the name of the array is the address of first element in the array". Many beginners (including myself when I was learning) have a tendency to become confused by thinking of it as a pointer. For example, while we can write

ptr = my_array;

we cannot write

my_array = ptr;

The reason is that while ptr is a variable, my_array is a constant. That is, the location at which the first element of my_array will be stored cannot be changed once my_array[] has been declared.

Earlier when discussing the term "lvalue" I cited K&R-2 where it stated:

"An object is a named region of storage; an lvalue is an expression referring to an object".

This raises an interesting problem. Since my_array is a named region of storage, why is my_array in the above assignment statement not an lvalue? To resolve this problem, some refer to my_array as an "unmodifiable lvalue".

Modify the example program above by changing

ptr = &my_array[0];

to

ptr = my_array;

and run it again to verify the results are identical.

Now, let's delve a little further into the difference between the names ptr and my_array as used above. Some writers will refer to an array's name as a constant pointer. What do we mean by that? Well, to understand the term "constant" in this sense, let's go back to our definition of the term "variable". When we declare a variable we set aside a spot in memory to hold the value of the appropriate type. Once that is done the name of the variable can be interpreted in one of two ways. When used on the left side of the assignment operator, the compiler interprets it as the memory location to which to move that value resulting from evaluation of the right side of the assignment operator. But, when used on the right side of the assignment operator, the name of a variable is interpreted to mean the contents stored at that memory address set aside to hold the value of that variable.

With that in mind, let's now consider the simplest of constants, as in:

int i, k;
i = 2;

Here, while i is a variable and then occupies space in the data portion of memory, 2 is a constant and, as such, instead of setting aside memory in the data segment, it is imbedded directly in the code segment of memory. That is, while writing something like k = i; tells the compiler to create code which at run time will look at memory location &i to determine the value to be moved to k, code created by i = 2; simply puts the 2 in the code and there is no referencing of the data segment. That is, both k and i are objects, but 2 is not an object.

Similarly, in the above, since my_array is a constant, once the compiler establishes where the array itself is to be stored, it "knows" the address of my_array[0] and on seeing:

ptr = my_array;

it simply uses this address as a constant in the code segment and there is no referencing of the data segment beyond that.

This might be a good place explain further the use of the (void *) expression used in Program 1.1 of Chapter 1. As we have seen we can have pointers of various types. So far we have discussed pointers to integers and pointers to characters. In coming chapters we will be learning about pointers to structures and even pointer to pointers.

Also we have learned that on different systems the size of a pointer can vary. As it turns out it is also possible that the size of a pointer can vary depending on the data type of the object to which it points. Thus, as with integers where you can run into trouble attempting to assign a long integer to a variable of type short integer, you can run into trouble attempting to assign the values of pointers of various types to pointer variables of other types.

To minimize this problem, C provides for a pointer of type void. We can declare such a pointer by writing:

void *vptr;

A void pointer is sort of a generic pointer. For example, while C will not permit the comparison of a pointer to type integer with a pointer to type character, for example, either of these can be compared to a void pointer. Of course, as with other variables, casts can be used to convert from one type of pointer to another under the proper circumstances. In Program 1.1. of Chapter 1 I cast the pointers to integers into void pointers to make them compatible with the %p conversion specification. In later chapters other casts will be made for reasons defined therein.

Well, that's a lot of technical stuff to digest and I don't expect a beginner to understand all of it on first reading. With time and experimentation you will want to come back and re-read the first 2 chapters. But for now, let's move on to the relationship between pointers, character arrays, and strings.

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!