ALGORITHM OPERATIONS

Question – WAP TO USE SORT,COPY,MERGE, FIND ALGORITHM.

Answer-

#include <iostream.h>    
#include <algorithm>    
#include <vector.h> 
#include <conio.h>      
using namespace std;
int main () 
{
  int arr[] = { 200, 100, 300 ,400, 50, 150, 246 };
  int prr[] = { 11, 22, 33, 44, 55, 66, 77 };
  int * p;
  
  //find algorithm
  cout<<"\n\t****FIND*****"<<endl<<endl;
  p = find (arr,arr+7,300);
  ++p;
  cout << "The element after 300 is " << *p <<endl;
  
  //copy algorithm
  cout<<"\n\t**** COPY *****"<<endl<<endl;
  vector <int> v(7);
  copy(arr,arr+7,v.begin());
  vector <int>::iterator it;
  for(it=v.begin(); it!=v.end(); ++it)
      cout << "The element is " << *it <<endl;
      
  //sorting algorithm
  cout<<"\n\t**** SORT *****"<<endl<<endl;
  sort(v.begin(),v.end());
  for(it=v.begin(); it!=v.end(); ++it)
      cout << "The element is " << *it <<endl;
     
  //merging algorithm
  cout<<"\n\t**** MERGING arr & 3 element of prr *****"<<endl<<endl;
  vector<int> v1(10);
  merge(arr,arr+7,prr,prr+3,v1.begin());
  vector<int>::iterator itt;
  for(itt=v1.begin(); itt!=v1.end(); ++itt)
      cout << "The element is " << *itt <<endl;
  cout<<"\nPROGRAMMING @ C#ODE STUDIO";
  getch();
  return 0;
}

algo_fin

THE STANDARD TEMPLATE LIBRARY

The Standard Function Library: This library consists of general-purpose,stand-alone functions that are not part of any class. The function library is inherited from C, it is a set of C++ template classes to provides general-purpose empathized classes and functions that implement commonly used algorithms and data structures like vectors, lists, and stacks.

Vectors are array which can be change in size; compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Containers : Containers are used to manage collections of objects of a certain kind. We Use <vector> in our syllabus.

Algorithms : Algorithms act on containers. They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of containers.

Iterators : Iterators are used to step through or move pointer in a container.

  • The push_back( ) member function inserts value at the end of the vector, expanding its size as needed.
  • The size( ) function displays the size of the vector.
  • The function begin( ) returns an iterator to the start of the vector.
  • The function end( ) returns an iterator to the end of the vector.
  • The function empty() returns whether the vector is empty (i.e. whether its size is 0).

A NORMAL PROGRAM IS HERE TO GET INPUT IN A VECTOR, DISPLAY THAT VECTOR, INPUT VALUE AT PARTICULAR POSITION;

#include"conio.h"
#include"iostream"
#include"vector" //HEADER FILE FOR VECTOR
using namespace std;
int main()
{
    vector <int> v;  //DECLARING VECTOR
    int i,n,l,num;
    cout<<"\nENTER TOTAL NUMBER OF ELEMENT : ";
    cin>>n;
    for(i=0;i<n;i++)
    {
        cout<<"\nEnter Value : ";
        cin>>num;
        v.push_back(num); //USING push_back FUNCTION TO PUT NO. IN VECTOR
    }
    cout<<"\n_______VALUES ARE________"<<endl;
    for(i=0;i<n;i++)
    {
        cout<<"\nValue at "<<i+1<<" is "<<v[i]<<endl; 
    }
    cout<<"\nEnter Value To Insert At Last Position : ";
    cin>>num;
    v.push_back(num);
    for(i=0;i<v.size();i++)
    {
        cout<<"\nValue at "<<i+1<<" is "<<v[i]<<endl;
    }
    cout<<endl;
    vector <int> :: iterator ie=v.begin(); //DEFINING iterator TO STARTING OF VECTOR USING <vector>.begin() function
    cout<<"\nEnter Steps You Want To Move Down : ";
    cin>>l;
    ie=ie+l; //SETTING VALUE TO STEP THROUGH
    cout<<"\nEnter Value to Insert at "<<l<<" Steps Down : ";
    cin>>num;
    v.insert(ie,num); INSERTING VALUE AT DEFINED POSITION USING <vector>.insert();
    cout<<endl;
    for(i=0;i<v.size();i++)
    {
        cout<<"\nValue at "<<i+1<<" is "<<v[i]<<endl;
    }
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO ";
    getch();
}

vector

COPY A FILE INTO ANOTHER ;

/*We can read a file and can put same data into another file
but keep the statement in mind that “A file can’t be deleted by a normal program. We can only change its name
that it looks like file has been deleted” so the program follows by : */

Ques – MAKE A CLASS STUDENT WITH DATA MEMBER NAME, ROLL, REG AND CGPA. TAKE INPUT FROM USER. WRITE DATA INTO A FILE AND COPY THE SAME FILE INTO ANOTHER FILE. CHECK IF DESTINATION FILE DOESN’T EXISTS, SUGGEST USER TO CREATE ONE.

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class student
{
      char Name[20];
      int Regno;
      int Rollno;
      float CGP;
      public:
             void enter()
             {
                  cout<<"\n____Enter the details as follow:-____"<<endl;
                  cout<<"Name of student:";
                  cin>>Name;
                  cout<<"Enter the Reg. No.:";
                  cin>>Regno;
                  cout<<"Enter the Roll no:";
                  cin>>Rollno;
                  cout<<"Enter the CGP:";
                  cin>>CGP;
                  }
                  void display()
                  {
                       cout<<"NAME: "<<Name<<endl;
                       cout<<"REGISTRATION NUMBER: "<<Regno<<endl;
                       cout<<"ROLL NUMBER: "<<Rollno<<endl;
                       cout<<"CGP: "<<CGP<<endl;
                  }
};
void write()
{
          cout<<"\n\n______ UPDATE DATA ______\n\n";
          char fname[10];
          cout<<"\nENTER FILE NAME WITH EXTENSION : ";
          cin>>fname;
          student obj;
          ofstream fp2(fname,ios::app);
          //fp2.open(fname,ios::app);
          if(fp2==NULL)
          {
              cout<<"\nFILE DOESN'T EXISTS : NEW FILE CREATED "<<endl;
          }
          obj.enter();
          fp2.write((char*)&obj,sizeof(obj));
          fp2.close();
}
void out()
{
            cout<<"\n\n______ READING DATA ______\n\n";
            char fname[10];
          cout<<"\nENTER FILE NAME WITH EXTENSION : ";
          cin>>fname;
          student obj;
          ifstream fp1;
          fp1.open(fname,ios::out);
          if(fp1==NULL)
          {
              cout<<"\nFILE DOESN'T EXISTS :"<<endl;
          }
          else
          {
              while(fp1.read((char*)&obj,sizeof(obj)))
              {
                  cout<<endl;
                obj.display();
              }
          }
          fp1.close();
}
void copy()
{
          student obj;
          char fname[10],tname[10];
          ifstream fp3;
          ofstream fp4;
          cout<<"\n\n______ COPYING FILE ______\n\n";
          cout<<"\nENTER DESTINATION FILE WITH EXTENSION : ";
          cin>>fname;
          fp3.open(fname,ios::out);
          cout<<"\nENTER TARGET FILE WITH EXTENSION : ";
          cin>>tname;
          fp4.open(tname,ios::app);
          if(fp3==NULL)
          {
              cout<<"\nDESTINATION FILE DOESN'T EXISTS :"<<endl;
          }
          else
          {
              while(fp3.read((char*)&obj,sizeof(obj)))
              {
                  cout<<endl;
                  cout<<"\n______ WRITING DATA _______"<<endl;
                obj.display();
                fp4.write((char*)&obj,sizeof(obj));
              }
          }
          fp3.close();
          fp4.close();
          cout<<"\n\n______ FILE COPIED______\n\n";
}
int main()
{
    int a;
    line:
    cout<<"\n1:UPDATING DATA, 2:READ DATA, 3:COPY FILE 4:EXIT PROGRAM : ";
    cin>>a;
    switch(a)
    {
        case 1: write();
                goto line;
        case 2: out();
                goto line;
          case 3: copy();
                goto line;
          case 4: cout<<"\n\nEXITING PROGRAM .......";
                  break;
          default:
                  cout<<"\nWRONG CHOICE... MAKE CHOICE AGAIN \n";
                  goto line;
    }
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO ";
    getch();
    return 0;
}

TARGET FILE-

file_copy2

 

DESTINATION FILE-

file_copy

OUTPUT –file_copy1

DELETE FILE;

/* We can ask user which file he/she want to open. the program defines it below */
Que -Make a class student with data member name,reg,roll and cgpa and with member function enter_data and display;
get input from user as many time he wants and write data into file in one function, also read data file in another function. Make a function in which you can delete particular file:

As we know a file cant be deleted by a delete function it can be renamed to another file  ;

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class student
{
      char Name[20];
      int Regno;
      int Rollno;
      float CGP;
      public:
             void enter()
             {
                  cout<<"\n____Enter the details as follow:-____"<<endl;
                  cout<<"Name of student:";
                  cin>>Name;
                  cout<<"Enter the Reg. No.:";
                  cin>>Regno;
                  cout<<"Enter the Roll no:";
                  cin>>Rollno;
                  cout<<"Enter the CGP:";
                  cin>>CGP;
                  }
                  void display()
                  {
                       cout<<"NAME: "<<Name<<endl;
                       cout<<"REGISTRATION NUMBER: "<<Regno<<endl;
                       cout<<"ROLL NUMBER: "<<Rollno<<endl;
                       cout<<"CGP: "<<CGP<<endl;
                  }
};
void write()
{
          cout<<"\n\n______ UPDATE DATA ______\n\n";
          char fname[10];
          cout<<"\nENTER FILE NAME WITH EXTENSION : ";
          cin>>fname; //take file name from user
          student obj;
          ofstream fp2(fname,ios::app); //put string as file name;
          if(fp2==NULL)
          {
              cout<<"\nFILE DOESN'T EXISTS : NEW FILE CREATED "<<endl;
          }
          obj.enter();
          fp2.write((char*)&obj,sizeof(obj));
          fp2.close();
}
void out()
{
            cout<<"\n\n______ READING DATA ______\n\n";
            char fname[10];
          cout<<"\nENTER FILE NAME WITH EXTENSION : ";
          cin>>fname;
          student obj;
          ifstream fp1(fname,ios::out);
          //fp1.open(fname,ios::out);
          if(fp1==NULL)
          {
              cout<<"\nFILE DOESN'T EXISTS :"<<endl;
          }
          else
          {
              while(fp1.read((char*)&obj,sizeof(obj)))
              {
                  cout<<endl;
                obj.display();
              }
          }
          fp1.close();
}
void del()
{
          cout<<"\n\n______ DELETING FILE ______\n\n";
          char fname[10];
          cout<<"\nENTER FILE NAME WITH EXTENSION : ";
          cin>>fname;
          rename(fname,"temp.txt");
          cout<<"\n\n______ FILE DELETED______\n\n";
}
int main()
{
    int a;
    line:
    cout<<"\n1:UPDATING DATA, 2:READ DATA, 3:DELETE FILE 4:EXIT PROGRAM : "<<endl;
    cin>>a;
    switch(a)
    {
        case 1: write();
                goto line;
        case 2: out();
                goto line;
          case 3: del();
                goto line;
          case 4: cout<<"\n\nEXITING PROGRAM .......";
                  break;
          default:
                  cout<<"\nWRONG CHOICE... MAKE CHOICE AGAIN \n";
                  goto line;
    }
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO ";
    getch();
    return 0;
}

file_user1

file_user2

file_user

FILE HANDELING ( IN AND OUT)

 syntax to read file : fp1.read((char*)&object_to_class,sizeof(object_to_class))
syntax to write file : fp1.write((char*)&object_to_class,sizeof(object_to_class)) 

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class student
{
      char Name[20];
      int Regno;
      int Rollno;
      float CGP;
      public:
             void enter()
             {
                  cout<<"\n____Enter the details as follow:-____"<<endl;
                  cout<<"Name of student:";
                  cin>>Name;
                  cout<<"Enter the Reg. No.:";
                  cin>>Regno;
                  cout<<"Enter the Roll no:";
                  cin>>Rollno;
                  cout<<"Enter the CGP:";
                  cin>>CGP;
                  }
                  void display()
                  {
                       cout<<"NAME: "<<Name<<endl;
                       cout<<"REGISTRATION NUMBER: "<<Regno<<endl;
                       cout<<"ROLL NUMBER: "<<Rollno<<endl;
                       cout<<"CGP: "<<CGP<<endl;
                       }
                  };
void write()
{
          cout<<"\n\n______ UPDATE DATA ______\n\n";
          student obj;
          ofstream fp2;
          fp2.open("student.txt",ios::app);
          obj.enter();
          fp2.write((char*)&obj,sizeof(obj));
          fp2.close();
}
void out()
{
            cout<<"\n\n______ READING DATA ______\n\n";
          student obj;
          ifstream fp1;
          fp1.open("student.txt",ios::out);
          while(fp1.read((char*)&obj,sizeof(obj)))
          {
                     obj.display();
          }
          fp1.close();
}
int main()
{
    int a;
    line:
    cout<<"\n1:UPDATING DATA, 2:READ DATA, 3:EXITING PROGRAM:";
    cin>>a;
    switch(a)
    {
        case 1: write();
                goto line;
        case 2: out();
                goto line;
          case 3: cout<<"\n\nEXITING PROGRAM .......";
                  break;
          default:
                  cout<<"\nWRONG CHOICE... MAKE CHOICE AGAIN \n";
                  goto line;
    }
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO ";
    getch();
    return 0;
}

 

file_in_out

 

FILE –file_in_out2

USE OF SEEKP AND TELLP

SEEKP – The seekp() function is used with output streams.
TELLP – The tellp() function is used with output streams, and returns the current “put” position of the pointer in the stream.

SYNTAX – seekp( pos_type position, ios::origin);
<filepointer>.tellp( );

#include<fstream>      // std::ofstream
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int main () 
{
    char ch[10];
    string chs;
    string c;
    int m;
    cout<<"_____OPENING FILE TO WRITE DATA_____"<<endl;
     ofstream ofs("seekp.txt"); //opening file via constructor method
     cout<<"\nINPUT TEXTLINE : ";
      getline(cin,chs);
      ofs<<chs;
    long pos = ofs.tellp(); //getting curr position of pointer
    cout<<"\n\nCurrent Position : "<<pos;
    cout<<"\nEnter Position from you want to rewrite : ";
    cin>>m;
      ofs.seekp (pos-m); //putting pointer some value back
      cout<<"\nINPUT WORD : ";
      cin>>ch;
      ofs<<ch;
    ofs.close();
    cout<<"\n\n_____OPENING FILE TO READ DATA_____\n\n"<<endl;
    ifstream ifs;
    ifs.open("seekp.txt");
    getline(ifs,c);
    cout<<c;
    ifs.close();
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO";
    getch();
    return 0;
}

 

file_seekp

file_seekp1

USE OF SEEKG AND TELLG

The function seekg() is used with input streams, and it repositions the “get” pointer for the current stream to offset bytes away from origin, or places the “get” pointer at position.

syntax : seekg( position , ios::origin );
ORIGIN : beg , cur , end
Position : will be no. where you want to move the pointer;

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
using namespace std;
int main()
{
                         //here is how to initialize file pointer
    ofstream f;         /*ofstream is a class, we are giving an object as 'f' to it .*/
    string ch;
    string c;
    cout<<"_____OPENING FILE TO WRITE DATA_____"<<endl;
    f.open("seek.txt");    /*calling function 'open()' of class 'ofstream' using object .*/
    cout<<"\nENTER TEXTLINE TO WRITE IN FILE : \n"<<endl;
    getline(cin,ch);
    f<<ch;
    f.close();
    ifstream ifs;             //'ifstream' is use for take input from file; 
    cout<<"\n\n_____OPENING FILE TO READ DATA_____\n\n"<<endl;
    ifs.open("seek.txt");
    int m,n;
    n=ifs.tellg();
    cout<<"\n\nCurrent Position is :"<<n;
    cout<<"\nEnter no.of data you want to skip : ";
    cin>>m;
    cout<<"\n_____DATA AFTER SKEEPING "<<m<<" BYTES_____\n\n"<<endl;
    ifs.seekg(m,ios::beg); //seeking file pointer 
    while(!ifs.eof())
    {
        getline(ifs,c);
        cout<<c;
    }                    //reading data till end of file "ifs.eof()"  
    ifs.close();
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO";
    getch();
}

file_seek

file_seek1

File Handling ( BASICS )

As you get the concept of file handling in previous post, let do some basics input/output into file….

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
using namespace std;
int main()
{
    //here is how to initialize file pointer
    ofstream f; /*ofstream is a class, we are giving an object as 'f' to it .*/
    string ch;
    string c;
    cout<<"_____OPENING FILE TO WRITE DATA_____"<<endl;
    f.open("new.txt"); /*calling function 'open()' of class 'ofstream' using object .*/
    cout<<"\nENTER TEXTLINE TO WRITE IN FILE : \n"<<endl;
    getline(cin,ch);
    f<<ch;
    f.close();
    ifstream ifs; //'ifstream' is use for take input from file; 
    cout<<"\n\n_____OPENING FILE TO READ DATA_____\n\n"<<endl;
    ifs.open("new.txt");
    while(!ifs.eof())
    {
        getline(ifs,c);
        cout<<c;
    } //reading data till end of file "ifs.eof()"
    ifs.close();
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO";
    getch();
}

file_normal1

file_normal