20130413

How to use new to allocate memory for 3d array in CPP C++


/*How to use new to allocate memory for 3d array. How to use malloc for 3 dimensional array. CPP program for dynamically allocating memory for 3d array. How to write three dimensional array with malloc with new */

#include<iostream>
using namespace std;
int main()
{
// 3 Dimensions Array
// two 3x4 array
int x = 2, y = 3, z = 4;

int i, j, k;
// memory allocation for 3D

int ***array3D = new int**[x];
for(i = 0; i < x; i++)
{
array3D[i] = new int*[y];
for(j = 0; j < y; j++)
{
array3D[i][j] = new int[z];
}
}

20130411

How to learn stack and flow of function call and control in cpp

/*How to learn stack. Stack is memory in which Last in first out is followed.
When function call is encountered the address of the next statement is stored or pushed in the stack. When the control returns back from the definition to calling function the address is poped out which is nothing but the statement next to the function call and executed.*/
#include<stdio.h>
#include<iostream>
using namespace std;
void function1();
void function2();

int main()
{
printf("1\n");
printf("2\n");
function1();
printf("3\n");
printf("4\n");

return 0;
}
void function1()
{
printf("1.1\n");
printf("1.2\n");
function2();
printf("1.3\n");
printf("1.4\n");
}
void function2()
{
printf("2.1\n");
printf("2.2\n");
}

How to learn recursive function call in cpp/C++/C

/*How to learn recursive function call in cpp/C++/C
 Cpp Program to learn recursive function */
#include<iostream>
using namespace std;
int sum(int m, int k);
int main()
{
int i,m,k;
m=3;
k=2;
i=sum(m,k);
cout<<"sum="<<i<<endl;
//return 0;
}
int sum(int rm, int rk)
{
cout<<rm<<" "<<rk<<endl;
if(rm==0) return(rk);//termination condition
else return(1+sum(rk, rm-1));//recursive condition//if not +1 then sum=0
}

How to distinguish between call by value, call by reference, call by address

/*How to distinguish between call by value, call by reference, call by address.
Program to distinguish between pass by value,pass by reference,pass by address. Call by reference and call by address affects the calling value while call by value doe not. Most important difference is that in call by value new object is created which is some times such as when size of the variable is too large is not desirable so call by reference is preferred over call by value*/

#include<stdio.h>
#include<iostream>
using namespace std;
void callbyreference(int * x);//pass by address
int main()
{
int x;
x=10;
printf("Before x=%d",x);//10
callbyvalue(&x);
printf("\nAfter x=%d",x);//15
printf("\n");
return 0;
}

void callbyreference(int * x)
{
*x=*x+5;//mind the*
}


//CALL BY REFERNCE, PASS BY REFERNCE
/*

20130330

How to write cpp program for Circular Queue using Array


How to write C++ program for Circular Queue using Array
C++ program to implement Circular Queue using Array
Circular Queue implementation using Array
CPP program for implementing Circular Queue using Array


#include<iostream>
using namespace std;
#define MAXSIZE 4

class queue
{
private:
int cq[MAXSIZE];
int front, rear;
public:
queue()
{
int cq[MAXSIZE]={0};//error: array must be initialized with a brace-enclosed initializer
front= -1;
rear = -1;
}


void insert(int item)
{
cout<<"\n in insert.\t";
if( ( (front==0)&&(rear==MAXSIZE-1)%MAXSIZE ) || front==(rear+1)%MAXSIZE )
{
cout<<"Q is full.\n";
}
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAXSIZE;

cq[rear]=item;

cout<<"\n front = "<<front<<" rear = "<<rear<<endl;

}

}


void display()
{
int i;
for(i=0;i<MAXSIZE;i++)
{
cout<<cq[i]<<" ";
}
cout<<endl;
}


void deletee()
{
if(front==-1)
{
cout<<"Q is empty.\n";
}
else
{
int temp;
temp=cq[front];
cq[front]=0;
if(front==rear)//only 1 elt was there
front=rear=-1;//delete the single so q is empty
else
front=(front+1)%MAXSIZE;//if more than 1 elt then delete frm front

cout<<"\n Deleted elt is:"<<temp<<endl;
cout<<"\n front = "<<front<<" rear = "<<rear<<endl;
}
}

};
int main()
{
queue q;
q.insert(10);
q.insert(20);
q.insert(30);
q.insert(40);
q.display();

q.insert(111);//Q is full so make same place by deleting fronts
q.display();

q.deletee();
q.display();
q.insert(100);
q.display();



return 0;
}
/*
in insert.
front = 0 rear = 0

in insert.
front = 0 rear = 1

in insert.
front = 0 rear = 2

in insert.
front = 0 rear = 3
10 20 30 40

in insert. Q is full.
10 20 30 40

Deleted elt is 10.

front = 1 rear = 3
0 20 30 40

in insert.
front = 1 rear = 0
100 20 30 40
*/
Simples Way to learn Data Structure in C++ CPP or C