Saturday, October 29, 2016

Overloading Subscript [ ] Operator :)

Program to Overload [ ] Subscript Operator:

#include<iostream>
using namespace std;
class sample
{
int a[10];    //private data
public: int n;
sample()
{
cout<<"Enter The No of Elements of Array  ";
cin>>n;
cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
cin>>a[i];
}
int operator [](int k)  // overloading Subscript operator
{
return(a[k]);
}
};
main()
{
sample a;
cout<<"Elements You Entered\n";
for(int i=0;i<a.n;i++)
cout<<a[i]<<"\n";       //fetching private data using object
}
Output:

Thursday, October 27, 2016

Matrix Operations Using Objects

Write a Class Template by the name matrix and perform the following operations:
1. Sum of 2 Matrices
2. Product of 2 Matrices
3. Sum of each row of matrix and display them
4. Sum of each column of matrix and display them


Solution:
#include<iostream>
using namespace std;
template<class T>
class matrix
{
T a[10][10];
public: int m,n;
matrix()
{
cout<<"Enter The Order of Matrix\n";
cin>>m>>n;
cout<<"Enter The Elements of Matrix\n";
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
}
matrix(int x,int y)
{  m=x;n=y;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
a[i][j]=0;
}
matrix operator +(matrix b)
{
matrix<T>c(b.m,b.n);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c.a[i][j]=a[i][j]+b.a[i][j];
return c;
}
matrix operator *(matrix b)
{
matrix<T>c(m,b.n);
if(n!=b.m)
{
cout<<"Multiplication Not Possible\n";
}
else
{for(int i=0;i<m;i++)
for(int j=0;j<b.n;j++)
{c.a[i][j]=0;
for(int k=0;k<n;k++)
c.a[i][j]+=a[i][k]*b.a[k][j];}

    return c;
}
}
void sumr()
{    T sum=0.0;
for(int i=0;i<m;i++)
{ sum=0.0;
for(int j=0;j<n;j++)
{
sum+=a[i][j];
}
cout<<"Sum of"<<i+1<<" row is "<<sum<<"\n";
}
}
void sumc()
{    T sum=0.0;
for(int i=0;i<n;i++)
{ sum=0.0;
for(int j=0;j<m;j++)
{
sum+=a[j][i];
}
cout<<"Sum of"<<i+1<<"column is "<<sum<<"\n";
}
}
void display()
{
cout<<"Elements Are\n";
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
{cout<<a[i][j]<<"\t";}
cout<<"\n";}
}

};
main()
{
matrix<int>a,b,c(a.m,b.n);
c=a+b;
cout<<"\nSum is\n";
c.display();
cout<<"\nProduct is\n";
c=a*b;
c.display();
cout<<"\nSum of Rows of A\n";
a.sumr();
cout<<"\nSum of Columns Of A\n";
a.sumc();

}

Matrix Operations using Class Template :)

Write a Class Template by the name matrix and perform the following operations:
1. Sum of 2 Matrices
2. Product of 2 Matrices
3. Sum of each row of matrix and display them
4. Sum of each column of matrix and display them


#include <iostream>
using namespace std;
template<class T> //Declaring Template class

class matrix
{
    T a[30][30],b[30][30];
    int i,j,m,n;
public:
    matrix() //Constructor to Input Matrix
    {
        cout<<"Enter No.of Rows and Columns of Matrices: ";
        cin>>m>>n;
        cout<<"Enter "<<(m*n)<<" Elements of Matrix 1: ";
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                cin>>a[i][j];
        }
        cout<<"Enter "<<(m*n)<<" Elements of Matrix 2: ";
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                cin>>b[i][j];
        }
    }

    void display()
    {
        cout<<"\nMatrix 1:\n";
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                cout<<a[i][j]<<"\t";
            cout<<"\n";
        }

        cout<<"\nMatrix 2:\n";
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                cout<<b[i][j]<<"\t";
            cout<<"\n";
        }
    }
    void sum() //Sum of Matrices
    {
        T c[30][30];
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                c[i][j]=a[i][j]+b[i][j];
        }
        cout<<"\n\nSum of Matrices is:\n";
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                cout<<c[i][j]<<"\t";
            cout<<"\n";
        }
    }
    void rowsum() //Display Sum of Rows
    {
        T sumr=0.0;
        cout<<"\n\nMatrix 1:";
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                sumr+=a[i][j];

            cout<<"\nSum of Row "<<(i+1)<<" is "<<sumr;
            sumr=0;
        }

        cout<<"\n\nMatrix 2:";
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                sumr+=b[i][j];

            cout<<"\nSum of Row "<<(i+1)<<" is "<<sumr;
            sumr=0;
        }
    }

    void colsum() //Display Sum of Columns
    {
        T sumc=0.0;
        cout<<"\n\nMatrix 1:";
        for(j=0; j<n; j++)
        {
            for(i=0; i<m; i++)
                sumc+=a[i][j];

            cout<<"\nSum of Column "<<(j+1)<<" is "<<sumc;
            sumc=0;
        }
        cout<<"\n\nMatrix 2:";
        for(j=0; j<n; j++)
        {
            for(i=0; i<m; i++)
                sumc+=b[i][j];

            cout<<"\nSum of Column "<<(j+1)<<" is "<<sumc;
            sumc=0;
        }
    }

    void product() //Function to find product
    {
        T prod[30][30]= {0.0}; //Matrix to store Product
        int p,q,k;
        cout<<"\n\nMatrix Product:-\n";
        cout<<"Enter Rows and Columns of Matrix 1: ";
        cin>>m>>n;
        cout<<"Enter "<<(m*n)<<" Elements of Matrix 1: ";
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
                cin>>a[i][j];
        }
        cout<<"Enter Rows and Columns of Matrix 2: ";
        cin>>p>>q;
        if(n!=p)
            cout<<"\nMatrices Cannot be Multiplied!!";
        else
        {
            cout<<"Enter "<<(p*q)<<" Elements of Matrix 2: ";
            for(i=0; i<p; i++)
            {
                for(j=0; j<q; j++)
                    cin>>b[i][j];
            }

            for(i=0; i<m; i++)
            {
                for(j=0; j<q; j++)
                {
                    prod[i][j]=0.0;
                    for(k=0; k<n; k++)
                        prod[i][j]+=a[i][k]*b[k][j];
                }
            }
            cout<<"\nProduct of Matrices is:\n";
            for(i=0; i<m; i++)
            {
                for(j=0; j<q; j++)
                    cout<<prod[i][j]<<"\t";
                cout<<"\n";
            }
        }
    }
};

int main()
{
    matrix<int> m; //Declaring Template
    m.display();
    m.sum();
    m.rowsum();
    m.colsum();
    m.product();
    return 0;
}






Shell Sort :)

Following Program Sort The Array Elements Using Shell Sort.


#include<iostream>
using namespace std;
main()
{
int a[100],d,n;
cout<<"Enter The No oF Elements ";
cin>>n;
cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
cin>>a[i];
for(d=n/2;d>=1;d--)
for(int i=0;i<n-d;i++)
if(a[i]>a[i+d])
{
int temp=a[i];
a[i]=a[i+d];
a[i+d]=temp;
}
cout<<"Sorted Array Is\n";
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}

Output:

Selection Sort:)

Following Program Sort The Array Elements Using Selection Sort :

#include<iostream>
using namespace std;
int a[100];
void min(int a[],int k,int n)
{
int j,i,min=a[k];
for(i=k+1;i<n;i++)
{
if(min>a[i])
{
min=a[i];
j=i;
a[j]=a[k];
          a[k]=min;
}
}

}
main()
{
int k,n;
cout<<"Enter The Number Of Elements ";
cin>>n;
cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
cin>>a[i];
for(k=0;k<n;k++)
{
min(a,k,n);
}
cout<<"Sorted Array is\n";
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}

Output:



Wednesday, October 26, 2016

Bubble Sort Using Function Template

Code:

#include<iostream>
using namespace std;
float a[100];
template<class T>
void sort(T a[],int n)
{
int c=n-1;
T k;
for(int j=0;j<=c;j++)
for(int i=0;i<n-j-1;i++)
{
if(a[i]>a[i+1])
{
k=a[i];
a[i]=a[i+1];
a[i+1]=k;
}
}
}
main()
{
int n,i;
cout<<"ENTER THE NO OF ELEMENTS";
cin>>n;
cout<<"ENTER THE ELEMENTS";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,n);
printf("AFTER SORTING\n");
for(i=0;i<n;i++)
{
cout<<a[i];
cout<<"\n";}
}
Output:

Minimum Element In An Array with Anonymous Datatype:)

Following Program Find Minimum Element Using Function Template:

#include<iostream>
using namespace std;
template<class T>
T min(T a[],int n)
{
T min=a[0];
for(int i=0;i<n;i++)
if(min>a[i])
{
min=a[i];
}
return min;
}
main()
{   int n,a[100];
cout<<"Enter The No of Elements\n";
    cin>>n;
    cout<<"Enter The Elements\n";
    for(int i=0;i<n;i++)
    cin>>a[i];
    cout<<"Minimum Value is: "<<min(a,n);
}
Output: