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


Use Of Common Friend Function To Exchange The Private Value Of Two Classes.


#include /iostream.h/

class class2; //forward declaration

class class1
{
int val1;
public:
void indata(int a)
{
val1=a;
}
void display(void)
{
cout<< val1 << "\n";
}
friend void exchange(class1 &,class2 &); //common friend function
};

class class2
{
int val2;
public:
void indata(int a)
{
val2=a;
}
void display(void)
{
cout<< val2 << "\n";
}
friend void exchange(class1 &,class2 &); //common friend function
};

void exchange(class1 & x,class2 & y) //definition of friend
{
int temp=x.val1;
x.val1=y.val2;
y.val2=temp;
}

int main()
{
class1 c1;
class2 c2;

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

cout<< "value before exchange" << "\n";
c1.display();
c2.display();

exchange(c1,c2); //swapping
cout<< "value after exchange" << "\n";
c1.display();
c2.display();

return 0;
}

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