Monday, October 31, 2016

Merging Elements of Two Sorted Arrays Into One Sorted Array

Tags
Merging is done by comparing the first elements of sorted arrays and the inserting the smallest in the resultant array.
Thus after all elements of one array are inserted then all elements of other array are inserted directly and we get a sorted array.
Following Program illustrates the above procedure:

#include<iostream>
using namespace std;
main()
{
int a[100],b[100],c[100],n,m,k;
cout<<"Enter The no of Elements Of 1st Array ";
cin>>n;
cout<<"Enter The no of Elements Of 2nd Array ";
cin>>m;
cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Enter The Elements\n";
for(int j=0;j<m;j++)
cin>>b[j];
int i=0,j=0;
k=0;
while(i<=n&&j<=m)     //comparing and inserting first elements
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;         //updating no of elements in resultant array
i++;
}
else if(b[j]<a[i])
{
c[k]=b[j];
j++;
k++;
}
}
k--;
if(i>n)
{for(int f=0;f<=m-j;f++)
{
c[k+f]=b[j+f];}      //inserting elements of second array
}
else if(j>m)
{
for(int f=0;f<=n-i;f++)
     {
c[k+f]=a[i+f];}     //inserting elements of first array
 }
cout<<"Sorted list is ";
for(int i=0;i<n+m;i++)
{
cout<<c[i]<<"\t";
}
}

Output:

Radix Sort :) :)

Tags
Radix Sort is the method to sort array elements according to the Numbers at unit place,tens place and then we get the sorted list.

Following Program Illustrates Radix Sort:

#include<iostream>
using namespace std;
main()
{   int b[10][10],a[100],n,num,pass,g=1;

cout<<"Enter The No Of Elements ";
cin>>n;

cout<<"Enter The Maximum No Of Digits ";
cin>>pass;

cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
cin>>a[i];

  for(int h=0;h<pass;h++)    //for number of passes
{for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
b[i][j]=-99;               //intializing all elements to  default value

int j=0,k=0;

for(int i=0;i<n;i++)
{
num=((a[i]/g)%10);    //finding remainder
b[j++][num]=a[i];     //inserting elements at proper position
   }
    g=g*10;                      //updating divisor  
   
   for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
   {
if(b[j][i]!=-99)
       a[k++]=b[j][i];     // updating array    
       }  
cout<<"Pass "<<h+1<<": ";
for(int i=0;i<n;i++)
{  
cout<<a[i]<<"\t";       //printing the results of particular pass
}
cout<<"\n";
}
cout<<"Soterd Array\n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";     //printing the result
}
}
Output:


Sunday, October 30, 2016

C++ Diwali Special :):)

Tags
Its Time to Celebrate Diwali in C++ Style. Following C++ Program Display Outputs according to the System(PC) Dates.

Run this Program on Oct 30, 31 and on Nov 1 and check the Outputs.
You can add more Festivals by changing Date and Festival Name.
Happy DIWALI!!


#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
void show(string festival);

int main()
{
string s;
system("COLOR 8B"); //Function to Change Text Color
    time_t currentTime;
    struct tm *localTime;

    time( &currentTime );                   // Get the current time
    localTime = localtime( &currentTime );  // Convert the current time to the local time

    int Day    = localTime->tm_mday; //Display Current Date
    int Month  = localTime->tm_mon + 1; //Months are counted from 0
    int Year   = localTime->tm_year + 1900; //Years are counted from 1900


if(Day==30&&Month==10&&Year==2016)
s="DIWALI";

else if(Day==31&&Month==10&&Year==2016)
s="VISHWAKARMA DAY";

else if(Day==1&&Month==11&&Year==2016)
s="BHAI DOOJ";
else
return 0;
cout<<"\n\n\n\n\n\tToday is "<<Day<<"/"<<Month<<"/"<<Year;

show(s);
    return 0;
}
void show(string festival)
{
cout<<"\n\tWish you a Very Happy "<<festival<<"!!\n";
cout<<"\tMay God bring you Luck and Help u Fulfill all your Dearest Dreams\n";

cout<<"\tHappy Coding!!\n\n";
cout<<"\tRegards,\n";

cout<<"\tDishant Mahajan\n\tNitesh Lekhi\n\tNaina Thaman\n\tHargun Kaur Gill\n";
cout<<"\t{gndecprogramming Bloggers}\n";
}





I got these Outputs by changing my PC's date.






Saturday, October 29, 2016

Infix To Prefix Conversion Using Stack :)

Tags
Following Code Converts  An Infix Expression To Prefix Expression :

#include<stdio.h>
#include<ctype.h>    //for isalnum function
#include<string.h>
char stack[50];
int top=-1;
void push(char x)
{
if(top==49)
{printf("Overflow");
}
else{

stack[++top]=x;
}
}
char pop()
{ if(top==-1)
{
return -1;
}
else{

return stack[top--];
}
}
int priority(char x)
{
if(x==')')
{ return 0;
}
else if(x=='+'||x=='-')
 return 1;
 else if(x=='*'||x=='/')
 return 2;
}
main()
{
char infix[50],pre[50];
char x,*e;
int i=0;
printf("Enter The Expression  ");
scanf("%s",infix);
strrev(infix);    //reversing the infix expression
e=infix;
while(*e!='\0')
{
if(*e==')')
{
push(*e);
}
else if(isalnum(*e))
{
pre[i++]=*e;
}
else if(*e=='(')
{
while((x=pop())!=')')
pre[i++]=x;
}
else
{
while(priority(stack[top])>=priority(*e))
pre[i++]=pop();
push(*e);
}
e++;
    }   while(top!=-1)
        pre[i++]=pop();
strrev(pre);      //reversing the result from procedure
                printf("Prefix Expression ");
printf("%s",pre);
}

Any Problem Please Contact Us mr.dishantmahajan@gmail.com

Output:

Overloading Pointer To Member -> Operator :)

Tags
Program To Overload -> Pointer to Member Operator :

#include<iostream>
using namespace std;
class sample
{
public:
int n;
sample()
{
cout<<"Enter Any Value ";
cin>>n;
}
sample* operator ->()
{
return this;
}
};
main()
{
    sample a;
    sample* p=&a;
    cout<<"Accessing Using Pointer";
    cout<<"\nValue is "<<p->n;
    cout<<"\nAccessing Using Object";
    cout<<"\nValue is "<<a->n;
}
Output:

Overloading Subscript [ ] Operator :)

Tags
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

Tags
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 :)

Tags
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 :)

Tags
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:)

Tags
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

Tags
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:)

Tags
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:

Swap two Numbers Of Anonymous Datatypes using Function Template:)

Tags
Following Program Swap Two Numbers Using Function Template:

#include<iostream>
using namespace std;
template<class T>
void swapp(T &x,T &y)
{
T a;
a=x;
x=y;
y=a;
}
main()
{
int a,b;
cout<<"Enter A ";
cin>>a;
cout<<"Enter B ";
cin>>b;
swapp(a,b);
cout<<"\nAfter Swapping";
cout<<"\nA="<<a;
cout<<"\nB="<<b;
}
Output:

Overloading Function Template :)

Tags
When we overload a function template by defining function with same signature but with any particular type then if called function is an exact match of the normal function then it is called.
else the template function is called.
Following Program Illustrates The Concept:

#include<iostream>
using namespace std;
template<class T>
void display(T x)
{
cout<<"\nTemplate Function\n";
cout<<"Value Passed: "<<x;
}
void display(int x)
{
cout<<"\nNormal Function\n";
cout<<"Value Passed: "<<x;
}
main()
{
display(5);
display('N');
}

Output:


Template Class With Multiple Parameters and Default Data Types :)

Tags
Following Program Illustrate The Use Of Template Class With Multiple Parameters:

#include<iostream>
using namespace std;
template<class T1=int,class T2=int>
class sample
{
T1 a;
T2 b;
public:
sample(T1 x,T2 y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nA="<<a<<"\nB="<<b;
cout<<"\n********************";
}
};
main()
{
sample<int,float>a(12,76.23);//passing int and float
    a.display();
sample<float,int>b(56.7,76);//passing float and int
b.display();
sample<float>c(15.7,12);//passing one type and other is default
c.display();
sample<>d('a',13);//default types used
d.display();
}

Output:


Insertion Sort

Tags
Following Program Sort The Array With Insertion Sort

Insertion Sort: In this the elements are inserted in same array by comparing it with previously inserted elements.

#include<iostream>
using namespace std;
main()
{
int a[100],n,i;
cout<<"Enter The No Of Elements in Array ";
cin>>n;
cout<<"Enter The Elements\n";
for(i=0;i<n;i++)
cin>>a[i];
int k;
for(k=1;k<n;k++)
{ int j=k-1;    //for comparing all elements with present element
int temp=a[k];
while(j>0 && temp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j]=temp;
}
cout<<"Sorted List Is ";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
Special Thank To Ms.Naina For This Program

Output:


Tuesday, October 25, 2016

Dot Product Of Vectors With Template Class

Tags
By using Template Class We Can do Generic Programming that is we can avoid the writing of same code for different datatypes.
Following Program Illustrate This Concept:
#include<iostream>
using namespace std;
template<class T>
class vector
{ T v[3];
public:
vector()
{ cout<<"Enter The Components of vector\n";
for(int i=0;i<3;i++)
cin>>v[i];
}
T operator*(vector &b)
{ T sum=0;
for(int i=0;i<3;i++)
sum+=v[i]*b.v[i];
return sum;
}
void display()
{ cout<<"Components of Vector Are ";
for(int i=0;i<3;i++)
cout<<v[i]<<"\t";
cout<<"\n";
}
};
main()
{ vector<typename>v1,v2;   //type name any type for which we want to do product
v1.display();
v2.display();
cout<<"V1*V2="<<v1*v2;
}

Output:


Saturday, October 22, 2016

Overloading Of Input/Output Operators of C++ :)

Tags
We know that in overloading we make the operator work on user defined datatype(Objects) same as on Built in Datatypes
So the Input Operator ">>"is overloaded for working on Objects and Output Operator "<<" also overloaded.

Following Program Illustrates the Concept:

#include<iostream>
using namespace std;
class time
{   int hours;
    int minutes;
    public:
    friend istream& operator>>(istream &,time &);  //for input in objects
    friend ostream& operator<<(ostream &,time &);  // for output from objects
};
istream& operator >>(istream &c,time &b)    //istream& for use in nested form like cin>>x>>y;
{   cout<<"\nEnter Time In Hours And Minutes\n";
c>>b.hours>>b.minutes;
return c;
}
ostream& operator <<(ostream &d,time &b)
{   cout<<"\nEntered Time";
d<<"\nHours: "<<b.hours<<" Minutes: "<<b.minutes;
return d;
}
main()
{    time t,n;
cin>>t>>n;
cout<<t<<n;

}

Output:





Special Thanks To Ms.Naina For This Program

Thursday, October 13, 2016

Arithmetic Operations Using Multiple Catch Statements:)

Tags
Program To Display Arithmetic Operations On Two Numbers Using Multiple Catch Statements:
#include<iostream>
using namespace std;
void operations()
{   int a,b;
    char c;
cout<<"Enter Two Values\n";
cin>>a>>b;
cout<<"Enter Any Arithmentic Operator ";
cin>>c;
try
{
         if(c!='+'&&c!='*'&&c!='/'&&c!='-')
{
throw a;
}
else
{
switch(c)
{
case '+': cout<<"Sum is "<<a+b; break;
case '-': cout<<"Difference is "<<a-b; break;
case '*': cout<<"Product is "<<a*b; break;
case '/':

if(b==0)
{
throw float(a);
}
else
{
cout<<"Quotient is "<<a/b; break;
}
             
}
}}
catch(float)
               {
                cout<<"Divide By Zero";
  }
catch(int)
{
cout<<"Invalid Operator";
}
}
main()
{
operations();
}
Output:

Arithmetic Operations Using Try Catch Statement :)

Tags
Program To Display Arithmetic Operations On Two Numbers Using Try Catch :

#include<iostream>
using namespace std;
void operations()
{   int a,b;
    char c;
cout<<"Enter Two Values\n";
cin>>a>>b;
cout<<"Enter Any Arithmentic Operator";
cin>>c;
try
{
         if(c!='+'&&c!='*'&&c!='/'&&c!='-')
{
throw a;
}
else
{
switch(c)
{
case '+': cout<<"Sum is "<<a+b; break;
case '-': cout<<"Difference is "<<a-b; break;
case '*': cout<<"Product is "<<a*b; break;
case '/': cout<<"Quotient is "<<a/b; break;
}
}
}
catch(int)
{
cout<<"Invalid Operator";
}
}
main()
{
operations();
}

Output:


Wednesday, October 12, 2016

Exception handling (Solution To E Balagurusamy Problem 13.5)

Tags
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: