Simple C++ Code clearly illustrating flow of control when Object is passed to a function and constructors and distructors are used …

//Tested in GCC and Code BLocks COmpilers
#include <iostream>
using namespace std;
 class r
{
    int i;
    public :
     r(int n);
     ~r();
     void seti(int n)
     {
         i=n;
     }
     int geti()
     {
         return i;
     }

};
r::r(int n)
{
    i=n;
    cout <<"\nconstructor in action : "<<i <<"\n";
}
r::~r()
{
    cout << " \ndestructor in action: " << i << " \n ";
}
int pass(r s );//This is the function to which object of a function can be passed as parameter
void another();
void first();
void second();
int main()
{
    cout <<"\nStarting point main";
    r k(3);
    cout<<"\n"<<pass(k)<<"\t";
    another();
    cout <<"\n end of  main : " <<k.geti() << "\n " ;
    return 0;
}
int pass(r s )//This is the function to which object of a function can be passed as parameter

{
    cout <<"\nStarting point of pass function ";
    s.seti(222);
    cout << " \n this end of pass function i: "<<s.geti();
    return s.geti();

}
void another ()
{
    cout <<"\nConstructor call from another";
    r ls(77);
    cout <<"\nCalling pass from another";
    pass(ls);
    void second();
    cout<<"\nBack in another ";
}
void second()
{
    cout <<"\nInside second";
    void first();
    cout <<"back in second ";
}
void first()
{
    cout <<"\nConstructor call from first";
    r fir(30);
    cout<<"\nPass call from first";
    pass(fir);
    cout<<"\nBack in first";
}

Generic Code to move a given set of consecutive members of your choice,in an array, to the end of the array

#include<stdio.h>
#include<stdlib.h>
void rotate(void *front,void *middle,void *back)
{
    int frontsize=(char *)middle-(char *)front;
    int backsize=(char *)back-(char *)middle;
    char buffer[frontsize];//This declaration works fine in Code blocks compiler or GCC but ANSI doesnot support such declaration
    memcpy(buffer,front,frontsize);
    memmove(front,middle,backsize);//built in function to be noted down
    memcpy((char *)back-frontsize,buffer,frontsize);

}

int main()
{
    int n,begin,end,i;
    printf("\nEnter the size of the array: ");
    scanf("%d",&n);
    int array[n];
    for(i=0;i<n;i++)
    {
        array[i]=i+1;
    }
    printf("\nEner the positions of elements starting from - to -  you would wish to move it to end of the array: ");
    scanf("%d %d",&begin,&end);
    if(begin<0||end>n)
     {
         printf("\nInvalid entry");
         exit(0);
     }

  rotate(&array[begin-1],&array[end],&array[n]);
  printf("\nThe shifted array is: ");
  for(i=0;i<n;i++)
   printf("\t%d",array[i]);
  return 0;
}

Code which gives the Truth table for a given Boolean expression for all possible combination of inputs(A-B+AB- is read as A bar B + A Bbar)

//the code is to be run in a compiler called code blocks http://www.codeblocks.org/downloads/26
#include <iostream>
#include <stdio.h>
void sort(char *in , int n);
void swap(char *in,int i,int minpos);
int binsearch(char* in,int n,char temp);

using namespace std;
int main()
{
        int i,n,k,max,j=0,bit,l=0,ret,q,z;
        char temp;
        cout << "Enter the no.of.number of inputs : ";
        cin >> n;
        char in[n];
        cout <<"\nEnter the names of Input Variables: ";
        for (i=0; i<n; ++i)
        {
                cin >> in[i];
        }

        cout << "\nInput : ";
        for (i=0; i<n; ++i)
        {
            cout << in[i] << "  ";
        }
        sort(in,n);//The order of arguments <array(in), n>
        cout <<"\nThe sorted inputs are: ";
        for(i=0;i<n;i++)
         cout<<"\t"<<in[i];
        k=(1<<n);
        int out[k];
        max=2*k*n;
        char equation[max];
        fflush(stdin);
        cout<<"\nEnter the Boolean Expression: ";
         fgets(equation,max,stdin);
        for(i=0;i<k;i++)
        {
            j=0;
            while(equation[j]&&out[i]!=1)
            {

              temp=equation[j];

              ret=binsearch(in,n,temp);

              if(ret==-1)
               {
                   cout<<"\n\t\t\t\tiNVALID INPUT, RUN THE PROGRAM AGAIN";
                   exit(EXIT_FAILURE);
               }
              bit=i&(1<<ret);

              if(equation[j+1]=='-')
               {
                   ++j;
                   bit=!bit;
               }
             if(bit==0)
             {
              while((equation[j]!='+') && (equation[j]!=NULL))
              {
                    j++;
              }
              if(equation[j]=='+')
                  ++j;

             }

             if(bit)
             {
                 ++j;

                 if(equation[j]=='+'||equation[j+1]==NULL)
                  {

                  out[i]=1;

                  }
             }
           }
            if(bit==0)
             {
                 out[i]=0;

             }
        }

  printf("\n");
cout<<"\nThe equation is : "<<equation;
cout<<"\n";
for(i=n-1;i>=0;i--)
 cout<<in[i]<<"\t";
cout<<"OUTPUT\t";
for(i=0;i<k;i++)
{
    cout<<"\n";
    for(q=n-1;q>=0;q--)
    {
        z=i&(1<<q);
        if(z==0)
         cout<<"0"<<"\t";
        if(z)
         cout<<"1"<<"\t";

    }
 cout<<out[i];
}

}
void sort(char *in , int n)
{
    int i,j,min,minpos;
    for(i=0;i<n-1;i++)
    {
       min=in[i];
       minpos=i;
       for(j=i+1;j<n;j++)
        {
            if(min<in[j])
            {
                minpos=j;
                min=in[j];
            }
            swap(in,i,minpos);
        }
    }

}

void swap(char *in,int i,int minpos)
{
 char temp;
 temp=in[i];
 in[i]=in[minpos];
 in[minpos]=temp;
}

int binsearch(char *in,int n,char key)
{
    int i;

     for(i=0;i<n;i++)
     {
         if(in[i]==key)
          return i;
     }
     return -1;

}

A generic code for preventing Overflow in Stacks when dynamic arrays used .(Just the functions are provided for now m will publish a sample soon)

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include<assert.h>
typedef struct
{
 void *elems;
 int elementsize;
 int loglength;
 int alloclength;
}stack;

void stacknew(stack *s,int elemsize);
void stackdispose(stack *s);
void push(stack *s,void *elemaddr);
void pop(stack *s,void *elemaddr);
static void stackgrow(stack *s);
void stacknew(stack *s,int elemsize)
{
 s->elementsize=elemsize;
 s->loglength=0;
 s->alloclength=4;
 s->elems=malloc(4*sizeof(elemsize));
 assert(s->elems!=NULL);
}
void stackdispose(stack *s)
{
 free(s->elems);

}
void push(stack *s,void *elemaddr)
{
     if(s->loglength==s->alloclength)
      stackgrow(s);
    void *target=(char *)s->elems+(s->loglength*s->elementsize);
    memcpy(target,elemaddr,s->elementsize);
    s->loglength++;
}
static void stackgrow(stack *s)
{
    s->alloclength*=2;
    s->elems= realloc(s->elems,s->elementsize * s->alloclength);
    assert(s->elems!=NULL);
}

void pop(stack *s,void *elemaddr)
{   int temp=s->loglength-1;
    assert(s->loglength>0);
    void *source=(char *)s->elems+temp*s->elementsize;
    memcpy(elemaddr,source,s->elementsize);
    s->loglength--;
}

int main()
{
 char *names []={"HI","BOB","BYE"};
 int i;

 stack stringstack;
 stacknew(&stringstack,sizeof(char *));
 for( i=0;i<3;i++)
 {
  char *cpy=strdup(names[i]);
 push(&stringstack,&cpy);
 }
 char *name;
 for( i=0;i<3;i++)
 {
  pop(&stringstack,&name);
  printf("\n%s",name);
  free(name);
 }
 stackdispose(&stringstack);
 return 0;
 }

Functions to avoid heap overflow while using Dynamically Allocated Arrays like int *v,char **h,char t[].(this code is type specific for ints)

typedef struct
{
 int *elems;
 int loglength;
 int alloclength;
}stack;

void stacknew(stack *s);
void stackdispose(stack *s);
void push(stack *s,int value);
void pop(stack *s);

void stacknew(stack *s)
{
 s->loglength=0;
 s->alloclength=4;
 s->elems=malloc(4*sizeof(int));
 assert(s->elems!=NULL);
}

void stackdispose(stack *s)
{
 free(s->elems);
}

void stackpush(stack *s,int value)
{
 if(s->loglength==s->alloclength)
 {
  s->elems=realloc(s->elems,4*sizeof(alloclength));
  assert(s->elems!=NULL);
 }
 s->elems[loglength]=value;
 s->loglength++;
}

void pop(stack *s)
{
    assert(loglength>0);
    loglength--;
    return s->elems[loglength];//Identify the error
}



BoolGorithm – 1 (hey guys i have started a project on Solving a boolean equation provided the minterms by the user to get a simplified equation using C , flow in and contribute to the code )

/*Project BoolGOrithm, this part of the code tells u the no.of.ones in the supplied minterms */

/*21/9/2011*/

//FRom 22-00//

#include<stdio.h>

#include<stdlib.h>

int main()

{

int n,size,i,j,k,f,num,arrsize;

printf(“\nEnter the no.of.Input variables: “);

scanf(“%d”,&n);

printf(“\nEnter no.of.Minterms Available: “);

scanf(“%d”,&size);

arrsize=(1<<n)-1;

if(size>arrsize)

{

printf(“\nInvalid Input”);

exit(EXIT_FAILURE);

}

int minterms[arrsize];

printf(“\nEnter the Min terms: “);

for(i=0;i<size;i++)

{

scanf(“%d”,&minterms[i]);

if(minterms[i]>arrsize)

{

printf(“\nInvalid Input”);

exit(EXIT_FAILURE);

}

}

for(j=0;j<size;j++)

{

num=0;

for(i=n-1;i>=0;i–)

{

k=(minterms[j]& (1<<i));

if(k)

num++;

}

printf(“\n\nThe minterm is %d, no.of.ones is %d “,minterms[j],num);

}

return 0;

}

//23-50 110 Minutes of coding

BoolGorithm – 1 (hey guys i have started a project on Solving a boolean equation provided the minterms by the user to get a simplified equation using C , flow in and contribute to the code )

/*Project BoolGOrithm, this part of the code tells u the no.of.ones in the supplied minterms */
/*21/9/2011*/
//FRom 22-00//
#include<stdio.h>
#include<stdlib.h>
int main()
{
 int n,size,i,j,k,f,num,arrsize;

 printf("\nEnter the no.of.Input variables: ");
 scanf("%d",&n);
 printf("\nEnter no.of.Minterms Available: ");
 scanf("%d",&size);
 arrsize=(1<<n)-1;
  if(size>arrsize)
   {
    printf("\nInvalid Input");
    exit(EXIT_FAILURE);
   }
 int minterms[arrsize];
 printf("\nEnter the Min terms: ");
 for(i=0;i<size;i++)
 {
  scanf("%d",&minterms[i]);
  if(minterms[i]>arrsize)
   {
    printf("\nInvalid Input");
    exit(EXIT_FAILURE);
   }
 }
for(j=0;j<size;j++)
{
 num=0;
 for(i=n-1;i>=0;i--)
 {
  k=(minterms[j]& (1<<i));
  if(k)
   num++;


 }
  printf("\n\nThe minterm is %d,    no.of.ones is %d ",minterms[j],num);
}
return 0;
}
//23-50 110 Minutes of coding 


Relation of A connection with PAcket switched Networks .

There is a General Assumption that Connection-oriented Protocols are always associated with Circuit Switched Networks , as All Circuit switched networks Have a Predefined Path created for the communication , in other words a connection .But even though the Packet switched Networks Doesnt have Inherit quality of connection as in case of a circuit Switched networks , These connection oriented protocols are very important to them because they enable the implementation of applications that require connections over Packet switched networks .Popular Protocols of TCP family (TCP is a connection-oriented family of protocols) such as FTP and TELNET needs a strong connection in-spite of using the PAcket -switching technology .In this way connection-oriented protocols cannot be assumed to be always associated with Circuit Switched Networks .