Thursday, 26 December 2013

C++ program to demonstrate the working of friend function

#include <iostream>
using namespace std;
class Distance
{
    private:
        int meter;
    public:
        Distance(): meter(0){ }
        friend int func(Distance);  //friend function
};
int func(Distance d)            //function definition
{
    d.meter=5;         //accessing private data from non-member function
    return d.meter;
}
int main()
{
    Distance D;
    cout<<"Distace: "<<func(D);
    return 0;
}
*************************************
Output
Distance: 5
//////////////////////////////////////////////////////////////////////
C++ Programming friend Function and friend Classes

One of the important concept of OOP is data hiding, i.e., a nonmember function cannot access an object's private or protected data. But, sometimes this restriction may force programmer to write long and complex codes. So, there is mechanism built in C++ programming to access private or protected data from non-member function which is  friend function and friend class.

friend Function in C++

If a function is defined as a friend function then, the private and protected data of class can be accessed from that function. The complier knows a given function is a friend function by its keyword friend. The declaration of friend function should be made inside the body of class (can be anywhere inside class either in private or public section) starting with keyword friend.
class class_name
{
    ......  ....   ........
    friend return_type function_name(argument/s);
    ......  ....   ........
}

Strassen's Method For Matrix Multiplication Using C


#include<stdio.h>
int main(){
  int a[2][2],b[2][2],c[2][2],i,j;
  int m1,m2,m3,m4,m5,m6,m7;

  printf("Enter the 4 elements of first matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("Enter the 4 elements of second matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&b[i][j]);

  printf("\nThe first matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",a[i][j]);
  }

  printf("\nThe second matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",b[i][j]);
  }

  m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
  m2= (a[1][0]+a[1][1])*b[0][0];
  m3= a[0][0]*(b[0][1]-b[1][1]);
  m4= a[1][1]*(b[1][0]-b[0][0]);
  m5= (a[0][0]+a[0][1])*b[1][1];
  m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
  m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

  c[0][0]=m1+m4-m5+m7;
  c[0][1]=m3+m5;
  c[1][0]=m2+m4;
  c[1][1]=m1-m2+m3+m6;

   printf("\nAfter multiplication using \n");
   for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",c[i][j]);
   }

   return 0;
}

C Program to Display same Source Code as Output

#include<stdio.h>

int main(){
    FILE *fp;
    char c;

    fp = fopen(__FILE__,"r");

    do{
         c= getc(fp);
         putchar(c);
    }
    while(c!=EOF);

    fclose(fp);

    return 0;
}
Output :

#include<stdio.h>

int main(){
    FILE *fp;
    char c;

    fp = fopen(__FILE__,"r");

    do{
         c= getc(fp);
         putchar(c);
    }
    while(c!=EOF);

    fclose(fp);

    return 0;
}

C Program to Multiply two Matrices using Recursion

#include<stdio.h>
#define MAX 10

void matMult(int [MAX][MAX],int [MAX][MAX]);
int r1,r2,c1,c2;
int c[MAX][MAX];

int main(){

    int a[MAX][MAX],b[MAX][MAX],i,j,k;

    printf("Enter the row and column of first matrix: ");
    scanf("%d%d",&r1,&c1);
    printf("Enter the row and column of second matrix: ");
    scanf("%d%d",&r2,&c2);

    if(c1!=o){
         printf("Matrix mutiplication is not possible");
    }
  else{

      printf("Enter the First matrix: ");
      for(i=0;i<r1;i++)
      for(j=0;j<c1;j++)
           scanf("%d",&a[i][j]);

      printf("Enter the Second matrix: ");
      for(i=0;i<r2;i++)
      for(j=0;j<c2;j++)
           scanf("%d",&b[i][j]);

      printf("nThe First matrix is: n");
      for(i=0;i<r1;i++){
      printf("n");
      for(j=0;j<c1;j++){
           printf("%dt",a[i][j]);
      }
      }

      printf("nThe Second matrix is: n");
      for(i=0;i<r2;i++){
      printf("n");
      for(j=0;j<c2;j++){
           printf("%dt",b[i][j]);
      }
      }

      matMult(a,b);

  }

  printf("nThe multiplication of two matrixes is: n");
  for(i=0;i<r1;i++){
      printf("n");
      for(j=0;j<c2;j++){
           printf("%dt",c[i][j]);
      }
  }
  return 0;
}

void matMult(int a[MAX][MAX],int b[MAX][MAX]){

    static int sum,i=0,j=0,k=0;

    if(i<r1) //row of first matrix
    {
      if(j<c2) //column of second matrix
      {
         if(k<c1)
         {
             sum=sum+a[i][k]*b[k][j];
             k++;
             matMult(a,b);
         }
         c[i][j]=sum;
             sum=0;
             k=0;
             j++;
             matMult(a,b);
      }
        j=0;
        i++;
        matMult(a,b);
    }
}
output :

Enter the row and column of first matrix: 2 2
Enter the row and column of second matrix: 2 2
Enter the First matrix: 1 2 3 4
Enter the Second matrix: 1 2 1 2

The First matrix is:

1       2
3       4
The Second matrix is:

1       2
1       2
The multiplication of two matrixes is:

3       6
7       14

C Program to implement Binary Search Tree Traversal

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

typedef struct BST
{
    int data;
    struct BST *lchild,*rchild;
}node;

void insert(node *,node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *,int,node **);

void main()
{
 int choice;
 char ans='N';
 int key;
 node *new_node,*root,*tmp,*parent;
 node *get_node();
 root=NULL;
 clrscr();

 printf("nProgram For Binary Search Tree ");
 do
 {
   printf("n1.Create");
   printf("n2.Search");
   printf("n3.Recursive Traversals");
   printf("n4.Exit");
   printf("nEnter your choice :");
   scanf("%d",&choice);

   switch(choice)
   {
    case 1:
           do
             {
             new_node=get_node();

             printf("nEnter The Element ");
             scanf("%d",&new_node->data);

             if(root==NULL)   /* Tree is not Created */
                 root=new_node;
             else
                 insert(root,new_node);

             printf("nWant To enter More Elements?(y/n)");
             ans=getch();

             }while(ans=='y');

             break;

     case 2:
             printf("nEnter Element to be searched :");
             scanf("%d",&key);

             tmp = search(root,key,&parent);

             printf("nParent of node %d is %d",
                              tmp->data,parent->data);
             break;

    case 3:

            if(root==NULL)
                printf("Tree Is Not Created");
            else
               {
               printf("nThe Inorder display : ");
               inorder(root);
               printf("nThe Preorder display : ");
               preorder(root);
               printf("nThe Postorder display : ");
               postorder(root);
               }

            break;
    }
 }while(choice!=4);
}
/*
  Get new Node
*/
node *get_node()
 {
 node *temp;
 temp=(node *)malloc(sizeof(node));
 temp->lchild=NULL;
 temp->rchild=NULL;
 return temp;
 }
/*
  This function is for creating a binary search tree
*/
void insert(node *root,node *new_node)
{
  if(new_node->data < root->data)
     {
     if(root->lchild==NULL)
         root->lchild = new_node;
     else
         insert(root->lchild,new_node);
     }

  if(new_node->data > root->data)
     {
     if(root->rchild==NULL)
         root->rchild=new_node;
     else
         insert(root->rchild,new_node);
     }
}
/*
This function is for searching the node from
      binary Search Tree
*/
node *search(node *root,int key,node **parent)
{
 node *temp;
 temp=root;
    while(temp!=NULL)
    {
      if(temp->data==key)
         {
         printf("n The %d Element is Present",temp->data);
         return temp;
         }
      *parent=temp;

      if(temp->data>key)
         temp=temp->lchild;
      else
         temp=temp->rchild;
    }
 return NULL;
}
/*
This function displays the tree in inorder fashion
*/
void inorder(node *temp)
{
   if(temp!=NULL)
    {
    inorder(temp->lchild);
    printf("%d",temp->data);
    inorder(temp->rchild);
    }
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp)
{
 if(temp!=NULL)
    {
    printf("%d",temp->data);
    preorder(temp->lchild);
    preorder(temp->rchild);
    }
}

/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp)
{
 if(temp!=NULL)
    {
    postorder(temp->lchild);
    postorder(temp->rchild);
    printf("%d",temp->data);
    }
}
[468x15]
Output :
Program For Binary Search Tree
1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :1

Enter The Element 5

Do u Want To enter More Elements?(y/n)
Enter The Element 12

Do u Want To enter More Elements?(y/n)
Enter The Element 6

Do u Want To enter More Elements?(y/n)
Enter The Element 3

Do u Want To enter More Elements?(y/n)
1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :3

The Inorder   display  :   3  5  6  12
The Preorder  display  :   5  3  12  6
The Postorder display  :   3  6  12  5

1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :2

Enter Element to be searched :3

The 20 Element is Present
Parent of node 3 is 5

C program using pointers to read in an array of integers and print its elements in reverse order.

#include<stdio.h>
#include<conio.h>
#define MAX 30

void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();

ptr=&arr[0];

printf("Enter the size of array : ");
scanf("%d",&size);

printf("nEnter %d integers into array:n",size);
   for(i=0;i<size;i++)
   {
   scanf("%d",ptr);
   ptr++;
   }

ptr=&arr[size-1];

printf("nElements of array in reverse order are:n");

 for(i=size-1;i>=0;i--)
   {
   printf("nElement%d is %d :",i,*ptr);
   ptr--;
   }

getch();
}
Output :

Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11

C Program to Implement Stack Operations Using Array

#include<stdio.h>
#define M 50
struct state
{
    char name[50];
    long int population;
    float literacyRate;
    float income;
}st[M];     /* array of structure */
int main()
{
int i,n,ml,mi,maximumLiteracyRate, maximumIncome;
float rate;
ml = mi =-1;
maximumLiteracyRate = maximumIncome = 0;

printf("Enter how many states:");
scanf("%d",&n);
for(i=0;i<n;i++)
  {
    printf("\nEnter state %d details :",i);
    printf("\n\nEnter state name : ");
    scanf("%s",&st[i].name);
    printf("\nEnter total population : ");
    scanf("%ld",&st[i].population);
    printf("\nEnter total literary rate : ");
    scanf("%f",&rate);
    st[i].literacyRate = rate;
    printf("\nEnter total income : ");
    scanf("%f",&st[i].income);
  }
for(i=0;i<n;i++)
{
    if(st[i].literacyRate >= maximumLiteracyRate)
        {
        maximumLiteracyRate = st[i].literacyRate;
        ml++;
        }
    if(st[i].income > maximumIncome)
        {
        maximumIncome = st[i].income;
        mi++;
        }
}
printf("\nState with highest literary rate :%s",st[ml].name);
printf("\nState with highest income :%s",st[mi].name);
return(0);
}
Output :
Enter how many states:3

Enter state 0 details :

Enter state name : Maharashtra

Enter total population : 1000

Enter total literary rate : 90

Enter total income : 10000

Enter state 1 details :

Enter state name : Goa

Enter total population : 300

Enter total literary rate : 94

Enter total income : 5000

Enter state 2 details :

Enter state name : Gujrat

Enter total population : 900

Enter total literary rate : 78

Enter total income : 7000

The state whose literary rate is the highest : Goa
The state whose income is the highest : Maharashtra