Tuesday, October 11, 2016

Addition Of Polynomials Using Array :)


#include<iostream>
using namespace std;
main()
{
int a[100],b[100],c[100],n,m,e;
cout<<"Enter the Max Power of Polynomial ";
cin>>n;
n++;
cout<<"Enter The Max power Of 2nd Polynomial ";
cin>>m;
m++;
cout<<"Enter the Coefficents and Constants\n";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Enter The Coefficents and Constants\n";
for(int i=0;i<m;i++)
cin>>b[i];
if(m==n)
{
e=n;
for(int i=0;i<n;i++)
c[i]=a[i]+b[i];}
else if(m<n)
{ e=n;

for(int i=0;i<m;i++)
c[i]=a[i]+b[i];
for(int i=n-1;i>=m;i--)
{
c[i]=a[i];
}
}
else
{e=m;

for(int i=0;i<n;i++)
c[i]=a[i]+b[i];
for(int i=m-1;i>=n;i--)
c[i]=b[i];
}
cout<<"Coefficents and Constants of Result Polynomial  ";
for(int i=0;i<e;i++)
cout<<c[i]<<"x^"<<i<<"+";
}

Output:


Monday, October 10, 2016

Quick Sort Using Stack :)

Quick Sort is an Sorting process which helps in sorting a list of elements.

Following Program Illustrates The Concept :

#include<iostream>
using namespace std;
int lower[10],upper[10],top=-1,loc;   //stacks for keeping records of sublists
void sort(int a[],int,int);    //function for performing  quick sort operation
main()
{  int n,beg,end,a[20];
cout<<"Enter The No of Elements ";  //taking the no of elements
cin>>n;
cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
{
cin>>a[i];         //storing the elements in array
}
beg=0;      //first element
end=n-1;   //last element
top++;
lower[top]=0;   //pushing in stack
upper[top]=end;
while(top!=-1)
{
beg=lower[top];  //popping from stack
end=upper[top];
top--;
sort(a,beg,end);   //calling quick sort
if(beg<loc-1)       //if it is a left sublist 
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1<end)   //if it is a right sublist
{
top++;
lower[top]=loc+1;
upper[top]=end;
}
}
cout<<"Sorted List Is ";
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";   //printing of sorted list
}
}
void sort(int a[],int beg,int end)  //function definition
{
int left,right;
left=beg;
right=end;
loc=beg;
step:
while(a[loc]<=a[right]&&loc!=right)   //scanning from right to left
{
right--;
}
if(loc==right)
{
return;
}
if(a[loc]>a[right])
{
int temp;
temp=a[loc];        //swapping values
a[loc]=a[right];
a[right]=temp;
loc=right;
}
while(a[left]<=a[loc]&&loc!=left)   //scanning from left to right
{
left++;
}
if(loc==left)
{
return;
}
if(a[loc]<a[left])
{
int temp;         //swapping values
temp=a[loc];
a[loc]=a[left];
a[left]=temp;
loc=left;
}
goto step;           //for next search
}

Also See Program of Operations On Circular Queue Click Here
Output:


new and delete Operators :)

C++ Program to show use of new and delete Operators.

#include <iostream>
using namespace std;
int main()
{
    int i,*p; //pointer variable
    p=&i; //pointer now points to i
    p=new int [3]; //new operator
    for(i=0; i<3; i++)
    {
        cout<<"Enter Value "<<(i+1)<<": ";
        cin>>(*(p+i)); //incrementing pointer address
    }

    for(i=0; i<3; i++)
    {
        cout<<"\nValue: "<<(*(p+i));
        cout<<"\nAddress: "<<(unsigned)p+i;//address of value
    }

    cout<<"\n\tdelete operator:";
    delete []p; //delete operator
    cout<<"\nMemory Released";
    return 0;
}



Saturday, October 8, 2016

Operations on CIRCULAR QUEUE!! :)

/* Before understanding the concept of circular queue,you must visit my previous
program on "OPERATIONS ON QUEUE",which discusses how operations are carried out on linear queue.
This program shows how even after the queue is FULL, insertions can be made after deletion.
This is the advantage of circular queue over linear queue!! */

#include<stdio.h>
#include<conio.h>
#define MAX 2 //defining size of queue
void insert(int);
int del();
void display();
int queue[MAX];
int front=-1;
int rear=-1;
main()
{
      int choice,num;
      while(1) //keep continuing the program until user wants to exit
      {
                    printf("Enter your choice \n");
                    printf("1.INSERTION \n");
                    printf("2.DELETION \n");
                    printf("3.DISPLAY \n");
                    printf("4. EXIT \n");
                    scanf("%d",&choice);
         switch(choice)
                    {
                    case 1: //executes insertion
                    insert(num);
                    break;
                    case 2: //executes deletion
                    num=del();
                    break;
                    case 3: //executes display
                    display();
                    break;
                    case 4: //stop further processing
                    exit(1);
                    break;
                    default:
                    printf("Invalid choice");
                    }
                    }
                    }
                 
                    // Function definition //
                 
                    void insert(int x)
                    {
if((front==0 && rear==MAX-1) || (front==rear+1))/* if either no deletion has occured and we have
reached the end of the queue  OR  after all deletions,we have inserted new elements that have reached
the queue's capacity */
            {
            printf("\n Queue is FULL \n ");
            return;
            }
            else
            {
                 printf("Enter number to be inserted: ");
                    scanf("%d",&x);
                    }

            if (front==-1) //for inserting 1st element we set front=0
            {
                front=0;
                }
                if(rear==MAX-1) //if we have reached the end of the queue
                {
                            rear=0; //we reset rear=0,as it is a circular queue
                            }
                            else
                {
                        rear=rear+1;
                                }
                         queue[rear]=x; //insertion at end of queue
                                }
                                int del()
                                {
                                     int x;

                 if(front==-1)
                 {
                       printf("\n Queue is EMPTY \n ");
                 }
            x=queue[front]; /* since queue is  FIFO and deletion occurs from front end,so we set
                      element at front of the queue as a value to the variable x */
                      if(front==rear) //if there is only 1 element in the queue
            {
                front=-1;//deleting that element
                rear=-1;
                                     }
                      else if(front==MAX-1) /* one last element at the "end of the queue" is left
                      which is soon going to be deleted by making front +1,we further reset the value
                      of front=0 after the last deletion as it is a circular queue and now front is
                      once again ready to delete newly entered elements in the queue */
                      {
                          front=0;
                          }
                          else
                          {
                              front=front+1;
                              }
                              printf("%d has been deleted \n",x);
                              return x;
                              }
                 void display()
                 {
                      if(front==-1)
                      {
                                   printf("Queue is Empty");
                                   return;
                                   }
                     int i;
                     i=front;
                     if(front<=rear)
                     {
                                    while(i<=rear) //setting a condition for printing entire queue
                                    {
                           printf("%d\n",queue[i++]);
                           }
                           }
                      else //touches the concept of circular queue
                      {
                          while(i<=MAX-1) //if we are somewhere in between the queue
                          {
                               printf("%d\n",queue[i++]);
                               } //this will go on till last element
                               i=0; //we reset front =0
                      while(i<=rear)
                               {
                                 printf("%d\n",queue[i++]);
                                 }
                                 }
                                 }                                                
                                                                                                       
                                                         
                       
                 
                       

Friday, October 7, 2016

Display Area(Solution To E Balagurusamy Problem 9.1)

Solution:

#include<iostream>
using namespace std;
class shape
{
protected:
double a,b;
public:
void getdata()
{
cout<<"\nEnter The Parameters \n";
cin>>a>>b;
}
virtual void display()
{

}
};
class triangle:public shape
{
public:
void display()
{
double area;
area=0.5*a*b;
cout<<"\nArea of Triangle is "<<area;
}
};
class rectangle:public shape
{
public:
void display()
{
double area;
area=a*b;
cout<<"\nArea of Rectangle is "<<area;
}
};
main()
{  int choice;
shape *p;
cout<<"\nPress 1 For Triangle\nPress 2 For Rectangle\n ";
cin>>choice;
if(choice==1)
{ triangle t;
p=&t;
}
else if(choice==2)
{
rectangle r;
p=&r;
}
p->getdata();
p->display();
}


Output:


Object Slicing :)

Object Slicing:

When a derived class object is assigned to base class then the base class contents in derived object  are copied to base class leaving behind the derived class specific contents.

Following Program Illustrates This Concept:
#include<iostream>
using namespace std;
class base
{
public:
int i=10,j=70;
void show()
{   cout<<"\nIn Base Class";
cout<<"\nI ="<<i;
cout<<"\nJ ="<<j;
}
};
class derived:public base
{
public:
int k=150;
void show()
{  i=50;
  j=75;
cout<<"\nIn Derived Class";
cout<<"\nI ="<<i;
cout<<"\nJ ="<<j;
cout<<"\nK ="<<k;
}
};
main()
{
base b;
derived d;
b.show();
d.show();
b=d;
b.show();
d.show();
}

Output:


Thursday, October 6, 2016

Virtual Destructor :)

When We use Pointers In Inheritance then the destructor of derived class is not called.
To follow the normal rule we can use Virtual Destructor

Following is a simple program to Illustrate the Above Concept

#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"Base Constructor Called\n";
}
virtual ~A()
{
cout<<"Base Destructor Called\n";
}
};
class B:public A
{
public:
B()
{
cout<<"Derived Constructor Called\n";
}
virtual~B()
{
cout<<"Derived Destructor Called\n";
}
};
main()
{   A *p=new B();
delete(p);
}

Output:













And if we donot use virtual destructor then the above program gives the following output