Wednesday, November 9, 2016

Pointer Arithmetic

Pointer Arithmetic involves the following operations
1.Increment / Decrement pointer
2.Addition  / Subtraction of integer
3.Subtraction of Two Pointers

Following Program Illustrates the Concept:

#include<iostream>
using namespace std;
main()
{
int a[3]={1,2,3};
int *p,*q;
p=&a[0];
q=&a[2];
//incrementing decrementing
cout<<"Value"<<"\t"<<"Address"<<"\n";
cout<<*p<<"\t"<<p<<"\n";
p++;
cout<<*p<<"\t"<<p<<"\n";
p--;
cout<<*p<<"\t"<<p<<"\n";
// addition and subtraction of integer
p+=2;
cout<<*p<<"\t"<<p<<"\n";
p-=2;
cout<<*p<<"\t"<<p<<"\n";
//subtaction of two pointers
cout<<"q-p= ";
cout<<q-p;
}

Output:




File Opening Modes App(append) vs Ate(at the end)

Both Ate and App makes the file pointer points to the end of file,but there is a difference in these two.
That during App mode we cannot change the position of pointer using seekg i.e "No Modify"
While in Ate mode we can change the position of pointer using seekg i.e "Modify Allowed".

Following Program Illustrates the concept:

#include<iostream>
#include<fstream>
using namespace std;
main()
{ int a;
fstream file("file.txt",ios::in|ios::out|ios::app);
    fstream file2("file2.txt",ios::in|ios::out|ios::ate);
file<<1507;
file2<<1507;
file.seekg(0,ios::beg);
file2.seekg(0,ios::beg);
file<<623;
file2<<623;
file.close();
file2.close();
}

Special Thanks To Miss Naina Thaman For This Program.

File Output:
file.txt:                                                                                file2.txt:


File Copying :)

Program To Copy The Contents of One File To Another ,so that other file is Identical Except Every Consecutive blank Space is replaced by Single Space.

#include<iostream>
#include<fstream>
using namespace std;
main()
{
ifstream fin("text.txt");
ofstream fout("text2.txt");
char c=fin.get(),d;
while(fin)
{
fout.put(c);
d=fin.get();
if(c==' '&&d==' ')
{
d=fin.get();
}
c=d;
}
}

File  Output:
Text.txt


Text2.txt

Sunday, November 6, 2016

File Pointers :)

Program To Illustrate the Concept of File Pointers :

#include<iostream>
#include<fstream>
using namespace std;
main()
{
char data[50];
cout<<"Enter Text ";
cin.getline(data,50);
ofstream fout("file1.txt",ios::out);
fout<<data;
fout.close();
cout<<"\nOpening The File\n";
ifstream fin("file1.txt",ios::in);
    fin.seekg(0,ios::end);
    int x=fin.tellg();
    cout<<"Total size "<<x<<" Bytes\n";
}

Output :



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: