Wednesday, October 12, 2016

Catch All Exceptions Using Single Catch Statement

Program to illustrate the catching of all exceptions using single catch statement:

#include<iostream>
using namespace std;
void test()
{
int x;
cout<<"Enter Any Positive Value Except 0\n";
cin>>x;
if(x<0)
{
throw (float)x;
}
else if(x==0)
{
throw x;
}
cout<<"\nEntered Value is "<<x;
}
main()
{
try
{
test();
}
catch(...)
{
cout<<"\nException Caught";}
cout<<"\nEnd";
}
Output:


Multiple Catch Statements (Solution To E Balagurusamy Problem 13.2)

In Multiple Catch Statements The Type of Argument is Matched with all catch statements and after a match the statement after the last catch block is executed.

Problem:
Write A Program that illustrates the application of multiple catch statements.

Solution:
#include<iostream>
using namespace std;
void test()
{
int x;
cout<<"Enter Any Positive Value Except 0\n";
cin>>x;
if(x<0)
{
throw (float)x;
}
else if(x==0)
{
throw x;
}
cout<<"\nEntered Value is "<<x;
}
main()
{
try
{
test();
}
catch(float)
{
cout<<"\nNegative Values Not Allowed";
}
catch(int)
{
cout<<"\n0 Not Allowed";
}
cout<<"\nEnd";
}
Output:


Exception Handling(Solution To E Balagurusamy Problem 13.1)

Problem:
Write a program containing a possible exception.Use a try block to throw it and a catch block to handle it properly.

Solution:

#include<iostream>
using namespace std;
main()
{
int i,fact=1;
cout<<"Enter A Positive No To Calculate Factorial\n";
cin>>i;
try
{
if(i<0)
{
throw i;
}
else
{
for(int j=1;j<=i;j++)
fact=fact*j;
cout<<"\nFactorial="<<fact;
}
}
catch(int)
{
cout<<"Number Not Valid";
}
cout<<"\nEnd";
}
Output:

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++]);
                                 }
                                 }
                                 }