Thursday, 21 August 2014

Type Casting In Class Using C++ Programing


#include
using namespace std;

class CDummy
{
    float i,j;
};

class CAddition
{
int x,y;
  public:
CAddition (int a, int b)
{ x=a; y=b; }
int result()
{ return x+y;}
};

int main ()
{
  CDummy d;
  CAddition * padd;
  padd = (CAddition*) &d;
  cout << padd->result();
  return 0;
}

C++ Example Pointer Program


#include 
#include 
void main()
{
int i=10;
int *Ptr;
clrscr();
Ptr=&i;
cout<<"\nValue Of i :"<cout<<"\nAddress Of i :"<cout<<"\nValue Of Ptr :"<cout<<"\nAddress Of Ptr :"<<&Ptr;
cout<<"\nPtr's Pointer Value:"<<*Ptr;
cout<<"\nPtr Equal to &i :"<<*(&i));

Pointer To Functions In C++


#include
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <  return 0;
}

Example Of Pointer In C++

#include
using namespace std;
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}

Factorial off a number using "do while" loop

#include
#include
void main(){int n,f=1;clrscr();printf("enter any number=");scanf("%d",&n);do{f=f*n;n--;}while(n>0);printf("factorial number is=%d",f);getch();}

Example Of XOR In C


#include
#include

struct xnode
{
int data;
unsigned long direction;
};
struct xnode *add_data(int data, struct xnode* list);
void walk_list(struct xnode *list);
int main(void)
{
struct xnode *l2 = add_data(2, NULL);
struct xnode *l1 = add_data(1, l2);
struct xnode *l3 = add_data(3, l2);
struct xnode *l4 = add_data(4, l3);
printf("front -> back....\n");
walk_list(l1);
printf("back -> front....\n");
walk_list(l4);
return 0;
}
struct xnode *add_data(int data, struct xnode *list)
{
struct xnode *newxnode = malloc(sizeof(struct xnode));
assert(newxnode);
newxnode->direction = (unsigned long)list;
newxnode->data = data;
if(list != NULL)
list->direction ^= (unsigned long)newxnode;
return newxnode;
}
void walk_list(struct xnode *list)
{
unsigned long prev = 0;
while(list != NULL) {
unsigned long next = prev ^ list->direction;
printf("%d ", list->data);
prev = (unsigned long)list;
list = (struct xnode *)next;
}
printf("\n");
}

Wednesday, 20 August 2014

Using C Program Print Fibonacci sequence 1 1 2 3 5 8 13 21 34 55 89 ...

1 1 2 3 5 8 13 21 34 55 89 ...
#include
void main()
{

static int prev_number=0, number=1; // static: so value is not lost

int fibonacci (int prev_number, int number);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);

}


fibonacci (int prev_number, int number)

{
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;

if (i==25)
{
printf ("\ndone"); //stop after 25 numbers

}

else
{
fibo=prev_number+number;
prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);
i++; // increment counter

fibonacci (prev_number,number); //recursion

}

}