Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ emp_data.txt(copy) 101 41 8.11 Y 49 666 1 66.66 Y 66 722 32 7.22 N 40 9876 2

ID: 3772237 • Letter: C

Question

C++

emp_data.txt(copy)

101 41 8.11 Y 49 666 1 66.66 Y 66 722 32 7.22 N 40 9876 23 9.99 N 39

binary_search.txt(copy)

//Implementation of Binary Search iteratively
int binSearch(int nums[ ], int size, int key)
{
int L = 0,
R = size -1,
M;
while( L<= R )
{
M = (L+R)/2;
if ( key == nums[M] )
return M; // return the middle index
else if (key < num[M] )
R = M-1; //reset the rightmost index
else
L = M+1; //reset the leftmost index
}
return -1; // while condition is false
}
//TO include in rom main()
result = binSearch(nums,size,key); // making function call
if ( result == -1 )
cout<<"Not found";
else cout<<"key found at location............"<<endl;

Explanation / Answer

The below is the program for writing the employee salary details to a file:

#include <fstream.h>

class employees

{

  protected:

    int empno;

    char name[10];

    char dept[5];

    char desig[5];

    double basic;

    double deds;

  public:

    void getdata(void)

    {

      coul<<endl<<"enter empno";cin>>empno;"

      cout<<endl<<"enter empname";cin>>name;

      cout<<endl<<"enter department ";

      cin>>dept; cout<<endl<<"enter designation ";

      cin>>desig; cout<<endl<<"enter basic pay ";cin>>basic:

      cout<<endl<<"enter deds ";cin>>deds;

    }

  void main(void){

  employees emp;

  emp.getdata( );

  ofstream outfile("f3.fil");

  outfile. write((char * )&emp,sizeof(emp));

}

The below is the program for reading object from file:

//program for reading data files

#include <istream.h>

class employees

{

  protected:

    int empno;

    char name[I0];

    char dept[5];

    char desig[5];

    double basic;

    double deds;

public:

  void showdata(void)

  {

    cout<<endl<<"employeenumber: "<<empno;

    cout<<endl<<"empname "<<name;

    cout<<endl<<"department "<<dept;

    cout<<endl<<"designation "<<desig;

    cout<<endl<<"basic pay "<<basic:

    cout<<endl<<"deds "<<deds;

  }

  void main(void){

    employees empl;

    ifstream infile("f3.fil");

    infile.read((char*)&empl, sizeof(empl));

    empl.showdata( );

  }

}

It may be noticed that both read( )and write( functions have similar argument. we must specify the address in which the disc input will be placed. We also use size of to indicate the number of bytes to be read.

The sample output looks as shown below:

1

2

3

4

5

6

employeenumber; 123

empname venkatesa

department elec

designation prog

basic pay 567.89

deds 45.76

//program for reading data files

#include <istream.h>

class employees

{

  protected:

    int empno;

    char name[I0];

    char dept[5];

    char desig[5];

    double basic;

    double deds;

public:

  void showdata(void)

  {

    cout<<endl<<"employeenumber: "<<empno;

    cout<<endl<<"empname "<<name;

    cout<<endl<<"department "<<dept;

    cout<<endl<<"designation "<<desig;

    cout<<endl<<"basic pay "<<basic:

    cout<<endl<<"deds "<<deds;

  }

  void main(void){

    employees empl;

    ifstream infile("f3.fil");

    infile.read((char*)&empl, sizeof(empl));

    empl.showdata( );

  }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote