Wednesday, October 12, 2016

Exception handling (Solution To E Balagurusamy Problem 13.5)

Solution:
#include<iostream>
using namespace std;
double x,y;
void read()
{
cout<<"\nEnter Two Double Type Values Except 0\n";
cin>>x>>y;
try
{
if(x==0||y==0)
{
throw 'x';
}
} catch(char)
{
cout<<"\n0 or Character Values Not Allowed";
}
}
void divide()
{
try
{
if(y==0)
{
throw 0;
}
else
{
cout<<"\nDivision Result is "<<x/y;
}
}
catch(int)
{
cout<<"\nDivide By Zero Exception Caught";
}

}
main()
{
read();
divide();

}
Output:


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;
}