Saturday, 4 March 2017

C++ program of template

In this program we will clear the concept of template in c++

//Code starts 

#include<iostream>
#include<conio.h>

using namespace std;

template <class T>

void abc(T* array,int size)
{
for(int i=0;i<size;i++)
{
cout<<array[i]<<",";
}
}

int main()
{
float ar[5]= {0.3,2.5,6.3,5.9,6.6};

int arr[5];


for(int i=0;i<5;i++)
{
cout<<"Enter integer : ";
cin>>arr[i];
}

abc(ar,5);
cout<<endl;
abc(arr,5);

getch();

}
//Code ends

OUTPUT:


Share:

C++ program of data insertion at end in array

In this program we will learn how to enter data on the last index of array by assigning new memory location.


//Code starts 
#include<iostream>
#include<conio.h>
using namespace std;

class array{
private:
int a[10],size,data,index;
public:
array(); //constructor
void create();
void print();
void insert();
};

array::array()
{
size=5;
index=size; 
}

void array::create()
{
    for(int i=0;i<size;i++)
    {
    cout<<"Enter data at index ["<<i<<"] : ";
    cin>>a[i];
}
}

void array::print()
{
for(int i=0;i<size;i++)
{
cout<<"Data at index ["<<i<<"] = "<<a[i]<<endl;
}
}

void array::insert()
{
cout<<"Enter data to be inserted : ";
cin>>data;


for(int i=size-1;i=0;i++)
{
a[i+1]=a[i];
}
size++;                     // assigning new memory
a[index]=data;         //copying new data on last index

cout<<"Data after insertion of "<<data<<" on index["<<index<<"]"<<endl;
}

int main()   //mainfunction
{
array a;          //creating object of class

a.create();              //calling create function of class
cout<<"-------------------------------------"<<endl;

a.print();                // calling print function of class
cout<<"-------------------------------------"<<endl;

a.insert();             //calling insert function of class
cout<<"-------------------------------------"<<endl;

a.print();                   // calling print function of class

getch();
}
//Code ends

OUTPUT:

Share:

C++ program of data insertion and deletion in stack

In this program we will learn how to insert and delete data in a stack.
Stack works on the principal of LIFO last in first out so new data can only be inserted and delete from the last index.
e.g if we take the array of 5 elements, 0,1,2,3,4. so the data existing on the last index (4) an only be inserted or deleted.


The code is given below.

//Code starts 

#include<iostream>
#include<conio.h>

using namespace std;

class stack{

private:
int a[10],top;

public:
void create();   //Function of inserting data
void print();     //Function of printing data
void push(int data);   //Function of inserion
void pop();    //Function of deletion
stack();     //Constructor
};

stack::stack()
{
top=-1;
}

void stack::create()
{
for(int i=0;i<5;i++)
{
cout<<"Enter data in index ["<<i<<"] : ";
cin>>a[i];
}
top=4;

}


void stack::print()
{
cout<<"Your entered data "<<endl;
for(int i=0;i<=top;i++)
{
cout<<"Data at index ["<<i<<"] : "<<a[i]<<endl;
}
}

void stack::push(int data)
{
cout<<"Insert data in stack : ";
cin>>data;
if(top==5)
{
cout<<"Stack is full";

}
else{
top++;

a[top]=data;
}
cout<<"Stack after insertion of "<<data<<" at index 5"<<endl;

}


void stack::pop()   
{
if(top==-1)
{
cout<<"Stack is full";
}
else{
cout<<"Data after insertion of index 5"<<endl;

top--;
}

}

int main()   //Main function starts
{
stack s; //Creating object of class

int x,d;  //Declaring two variable

do{

cout<<"Enter 1 to enter data : "<<endl;
cout<<"Enter 2 to show entered data : "<<endl;
cout<<"Enter 3 to insert new data in stack : "<<endl;
cout<<"Enter 4 to delete data from stack : "<<endl;
cout<<"Enter 5 to exit from program : "<<endl;
cout<<"      Value =     ";

cin>>x;
cout<<"---------------------------------------------"<<endl;

switch (x) //Starting switch function to switch the given value from user


 {

 case 1:
 
  s.create();   //Calling function of create from class
  cout<<"---------------------------------------------"<<endl;

 break;
 
 case 2:
 
   s.print();      //Calling function of print from class
   cout<<"---------------------------------------------"<<endl;

 break;
 
 case 3:
 
  s.push(d);    //Calling function of push from class
  cout<<"---------------------------------------------"<<endl;

  s.print();    //Calling function of print from class
        cout<<"---------------------------------------------"<<endl;


 break;
 
 case 4:
 
  s.pop();    //Calling function of pop from class
      cout<<"---------------------------------------------"<<endl;

  s.print();   //Calling function of print from class
  cout<<"---------------------------------------------"<<endl;

break;

case 5:

cout<<"Program terminated "<<endl;
return 1;  //Returning value 1 to terminate program

break;

}
}
while(1);  //Program will be ended when it returns value 1

getch();
}
//Code ends

OUTPUT:




Share:

Friday, 3 March 2017

C++ program of deletion and insertion in array

In this program we will learn how to insert data in an array, how to show that data, how to allocate memory and insert new data in an existed data, and how to delete a specific index.
The code is given below:

//Code starts 

#include<iostream>
#include<conio.h>
using namespace std;

class array{
private:
int a[10],size;

public:

array();

void create();
void print();
void insertion(int data,int index);
void deletion(int index);

};

array::array()
{
size=5;
}

void array::create()
{
for(int i=0;i<size;i++)
{
cout<<"Enter data in index ["<<i<<"] : ";
cin>>a[i];
}
}

void array::print()
{
cout<<"Your entered data "<<endl;
for(int i=0;i<size;i++)
{
cout<<"Data at index ["<<i<<"] : "<<a[i]<<endl;
}
}

void array::insertion(int data,int index)
{
     
for(int i=size-1;i>=index;i--)
{
a[i+1]=a[i];
}
size++;

a[index]=data;

cout<<"New data after insertion at index["<<index<<"]"<<endl;
for(int i=0;i<size;i++)
{
cout<<"Index ["<<i<<"] = "<<a[i]<<endl;
}

}

void array::deletion(int index )
{
for(int i=index+1;i<=size-1;i++)
{
a[i-1]=a[i];
}

size--;

cout<<"After deleting index["<<index<<"]"<<endl;

for(int i=0;i<=size-1;i++)
{
cout<<"Index ["<<i<<"] = "<<a[i]<<endl;
}

}


int main()
{
array a;
int x;
do{

cout<<"Enter 1 to enter data : "<<endl;
cout<<"Enter 2 to show entered data : "<<endl;
cout<<"Enter 3 to insert data in array : "<<endl;
cout<<"Enter 4 to delete data in array : "<<endl;
cout<<"Enter 5 to exit from program : "<<endl;
cout<<"      Value =     ";

cin>>x;
switch (x)
 {
 case 1:
 
 
  a.create();
 break;
 
 case 2:
 
    a.print();
 

 break;
 case 3:
  int n,b;
 
  cout<<"Enter data : ";
  cin>>n;
 
  cout<<"Enter index : ";
  cin>>b;
      a.insertion(n, b);
 
 break;
 case 4:
  int v;
 
  cout<<"Enter index to be deleted : ";
  cin>>v;
  a.deletion(v);
 
break;

case 5:

                  cout<<"Program terminated "<<endl;
return 1;

break;

}
}
while(1);

getch();

}
//Code Ends

OUTPUT:


Share:

Wednesday, 1 March 2017

C++ program of multiplication of array

In this program we will learn how to enter data in array and multiply the index of array.
The code is given below:

//Code starts

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
int a;
int array[a];
int multi;

multi=1;
cout<<"enter size of array"<<endl;
cin>>a;

for(int i=1;i<=a;i++)
{
cout<<"enter number for multi"<<endl;
cin>>array[i];
multi=multi*array[i];
    }
cout<<"multi is ="<<multi<<endl;
   
 getch();

}
//Code ends

OUTPUT:





Share:

Sunday, 26 February 2017

C++ Program of Binary Search in Array

In this program we will learn how to find the data from array inserted in an array 

//Code Start
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[100],n,i,beg,end,mid,item;
cout<<"\n------------ BINARY SEARCH ------------ \n\n";
cout<<"Enter No. of Elements= ";
cin>>n;
cout<<"\nEnter Elements in Sorted Order=\n";
for(i=0;i<n;i++)
{
cout<<"Enter number at index ["<<i<<"] : ";
cin>>a[i];
}
cout<<"\nEnter Item you want to Search= ";
cin>>item;
beg=1;
end=n;
mid=(beg+end)/2; // Find Mid Location of Array
while(beg<=end && a[mid]!=item) // Compare Item and Value of Mid
{
if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
mid=(beg+end)/2;
}
if(a[mid]==item)
{
cout<<"\nData is Found at index ["<<mid<<"]";
}
else
{
cout<<"Data is Not Found";
}
getch();
}
//CODE Ends

OUTPUT: 
Share: