Thursday, November 3, 2016

FILE HANDLING :)

C++ Program to Illustrate the Concept of File Handling.

#include <iostream>
#include <fstream> //Header file for File Operations
using namespace std;
int main()
{
    int sal,age;
    char name[20];
    cout<<"Enter Name: ";
    cin>>name;
    cout<<"Enter Age: ";
    cin>>age;
    cout<<"Enter Salary: ";
    cin>>sal;
    cout<<"\n\nWriting to File..\n";

    ofstream fout;
    fout.open("employee.dat"); //Opening file "employee.dat"  for writing using open() Function
    fout<<name<<"\n"; //Writing Data to File
    fout<<age<<"\n";
    fout<<sal<<"\n\n";
    cout<<"\nClosing File..\n\n";
    fout.close(); //Closing File

    cout<<"Reading from File..\n\n";
    ifstream fin; //Opening file for Reading using open() Function
    fin>>name>>age>>sal; //Reading Data from File
    cout<<"Name: "<<name;
    cout<<"\nAge: "<<age;
    cout<<"\nSalary: "<<sal;
    cout<<"\n\nClosing File..\n";
    fin.close();
    return 0;
}



employee.dat File



Tuesday, November 1, 2016

Post Overloading Increment Operator

As we all know in operator overloading we overload operators so that they work on objects in same way as they work on built in datatypes.
Now We can overload some operators as Pre or Post
Pre Increment Click Here

In Post Increment we have to supply an integer type argument in operator definition so that compiler can distinguish between Post And Pre.
Following Program Illustrates The Above Concept:
#include<iostream>
using namespace std;
class sample
{
int a;
public:
sample()
{
cout<<"Enter The Number ";
cin>>a;
}
void operator ++(int)
{
a++;
}
void display()
{
cout<<a;
}
};
main()
{
sample n;
n++;
cout<<"After Incrementing  ";
n.display();
}
Output:


Monday, October 31, 2016

Merging Elements of Two Sorted Arrays Into One Sorted Array

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

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

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

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

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: