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

Wednesday, February 11, 2009

C++:: Friend Function

A Function Friendly To Two Classes


#include /iostream.h/

class class2; //forward declaration

class class1
{
int x;
public:
void setvalue(int i)
{
x=i;
}
friend void max(class1,class2); //common friend function
};

class class2
{
int a;
public:
void setvalue(int i)
{
a=i;
}
friend void max(class1,class2); //common friend function
};

void max(class1 m,class2 n) //definition of friend
{
if(m.x>=n.a)
cout<< "m is max:" << m.x;
else
cout<< "n is max:" <<>
}

int main()
{
class1 c1;
class2 c2;

c1.setvalue(10);
c2.setvalue(20);

max(c1,c2); //greater then
return 0;
}

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