C:\Users\Jatinder\Desktop\ArrayProgmaWithExceptionHandleing.html
<!-- Exported by C-Free C/C++ IDE
#include<iostream.h>
class ArrayFullException : public std::exception
{
public:
ArrayFullException ()
{
}
virtual const char* what() const throw()
{
return "Array is Full";
}
};
class ArrayEmptyException : public std::exception
{
public:
ArrayEmptyException ()
{
}
virtual const char* what() const throw()
{
return "Array is Empty";
}
};
template <class TT>
void Swap(TT &ff, TT &ss)
{
TT temp;
temp=ff;
ff=ss;
ss=temp;
}
template <class T>
class Array
{
private:
T *a;
int n;
int size;
bool isFull();
bool isEmpty();
public:
Array(int );
void InsertAtFirst(T val);
void InsertAtLast(T val);
void InsertAtPos(int pos,T val);
T GetFromFirst();
T GetFromLast();
T GetFromPos(int);
int MinPos(int,int);
int MaxPos(int,int);
T MinVal(int,int);
T MaxVal(int,int);
void Reverse();
void sortbubble();
void sortselection();
void sortinsertion();
int Search(T snum);
void Update(int po,T num);
void DeleteFromFirst();
void DeleteFromLast();
friend ostream & operator<<(ostream &o,Array<T> &t)
{
for(int i=0;i<=t.n;i++)
o<<t.a[i]<<endl;
return o;
}
friend istream & operator>>(istream &is,Array<T> &t);
~Array();
};
template <class T>
Array<T>::Array(int siz=10)
{
a=new T[siz];
size=siz;
n=-1;
}
template <class T>
bool Array<T>::isFull()
{
return (n==size);
}
template <class T>
bool Array<T>::isEmpty()
{
return (n==-1);
}
template <class T>
void Array<T>::InsertAtFirst(T val)
{
InsertAtPos(0,val);
}
template <class T>
void Array<T>::InsertAtPos(int pos,T val)
{
for(int i=n;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
}
template <class T>
void Array<T>::InsertAtLast(T data)
{
if(isFull())
{
throw ArrayFullException ee();
}
else
{
n++;
a[n]=data;
}
}
template <class T>
T Array<T>::GetFromFirst()
{
return(a[0]);
}
template <class T>
T Array<T>::GetFromLast()
{
return(a[n]);
}
template <class T>
T Array<T>::GetFromPos(int pos)
{
return(a[pos]);
}
template <class T>
int Array<T>::MinPos(int k=0,int Endn=0)
{
T min=a[k];
int pos=0;
for(int i=k;i<=Endn;i++)
{
if(a[i]<=min)
{
min=a[i];
pos=i;
}
else
continue;
}
return pos;
}
template <class T>
int Array<T>::MaxPos(int k=0,int Endn=0)
{
T max=a[k];
int maxpos;
for(int i=k;i<=Endn;i++)
{
if(a[i]>max)
{
max=a[i];
maxpos=i;
}
else
continue;
}
return maxpos;
}
template <class T>
T Array<T>::MinVal(int k=0,int Endn=0)
{
return(a[MinPos(0,n)]);
}
template <class T>
T Array<T>::MaxVal(int k=0,int Endn=0)
{
return(a[MaxPos(0,n)]);
}
template <class T>
void Array<T>::Reverse()
{
T temp;
int i;
int head = 0;
int tail = n;
while (head < tail)
{
int temp = a[head];
a[head] = a[tail];
a[tail] = temp;
head++;
tail--;
}
}
template <class T>
void Array<T>::sortbubble()
{
T temp;
for(int i=0;i<=n;i++)
{
for(int j=0;j<i;j++)
{
if(a[i]<a[j])
Swap<T>(a[i],a[j]);
}
}
}
template <class T>
void Array<T>::sortselection()
{
T min;
int minpos;
for(int i=0;i<=n;i++)
{
int minpos=MinPos(i,n);
cout<<"---"<<minpos<<endl;
Swap<T>(a[i],a[minpos]);
}
}
template <class T>
void Array<T>::sortinsertion()
{
T temp;
int i,j;
for(i=1;i<=n;i++)
{
temp=a[i];
for(j=i-1;temp<=a[j];j--)
{
if(j==-1)
break;
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
template <class T>
int Array<T>::Search(T snum)
{
int result;
result=-1;
for(int i=0;i<n;i++)
{
if(a[i]==snum)
{
result=1;
break;
}
else
continue;
}
return result;
}
template <class T>
void Array<T>::Update(int po,T num)
{
a[po]=num;
}
template <class T>
void Array<T>::DeleteFromFirst()
{
if(isEmpty())
throw ArrayEmptyException();
else
{
for(int i=0;i<n;i++)
a[i]=a[i+1];
n--;
}
}
template <class T>
void Array<T>::DeleteFromLast()
{
if(isEmpty())
throw ArrayEmptyException();
else
{
n--;
}
}
template <class T>
Array<T>::~Array()
{
delete a;
}
0 comments: