Saturday, 20 May 2017

C++ program of queue

In this program we will take a look on working of queue by craeting, inserting, deleting and displaying an queue.
program:

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

using namespace std;

class queue{
private:
int front,counter,maxsize,rear,q[5];
public:
queue();
void create();
void enqueue(int d);
void dequeue();
void show();
};

queue::queue()
{
front=-1;
rear=-1;
maxsize=5;
counter=0;
}
/*
void queue::create()
{
for(int i=0;i<5;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>q[i];
}
front=0;
rear=5;
}
*/
void queue::enqueue(int d)
{
if(rear==4)
{
cout<<"\n\nQueue is full\n";
}
else
{
cout<<"Enter data : ";
cin>>d;

rear++;
q[rear]=d;
if(front==-1)
{
front=rear=0;
}
}
}

void queue::dequeue()
{
if(front==rear||front==-1)
{
cout<<"\n\nQueue is empty\n";
}
else
{
front++;
cout<<"\n\nData is deleted\n";
if(front==4&&rear==4)
{
front=rear=-1;
}
}
}

void queue::show()
{
if(rear==-1&&front==-1)
{
cout<<"\nQueue is empty\n";
}
else
{

for(int i=front;i<=rear;i++)
{
cout<<"Data at ["<<i<<"] == "<<q[i]<<endl;
}

}
}

int main()
{
queue q;
int c,d;

// q.create();

do{
cout<<"\nPress 1 for enqueue\nPress 2 for dequeue\nPress 3 for show\nPress 4 for exit\nValue == ";
cin>>c;

switch(c)
{
case 1:
q.enqueue(d);
break;
case 2:
q.dequeue();
break;
case 3:
q.show();
break;
case 4:
return 1;
}
}
while(1);
}
//Code Ends

Screenshot:


Share:

C++ program of creation, insertion and deletion in array

In this program we will create, print, delete and insert data in array using  methods of data structures.

Program:


#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

Screenshot:
Share:

Recursive function of exponential

In this program we will see how a function calls itself inside its body of fuction.

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

using namespace std;

int rf(int n,int b)
{
          if(b==1)
          {
                    return n;
                    }
          else 
          {
                    return n*rf(n,b-1);
          }
                   
}

int main()
{
          int m,o;
         
          cout<<"Enter base : ";
          cin>>m;
          cout<<"Enter power: ";
          cin>>o;
         
          cout<<rf(m,o);
          getch();
}


//Code Ends

Screenshot:

Share: