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

Create a C++ program that: - in main - opens the 2 files provided for input - ca

ID: 3809331 • Letter: C

Question

Create a C++ program that:          

- in main

- opens the 2 files provided for input                   

- calls a function to determine how many lines are in each file                   

- creates 2 arrays of the proper size                   

- calls a function to read the file and populate the array (call this function twice, once for each file/array)                   

- calls a function to write out the 'merged' results of the 2 arrays                             

- if there are multiple entries for a person, these should be merged and should appear as a single entry in the resulting file                             

- resulting file should be named 'merged_output.txt'

I have done all the steps described above; however, I can't merge the arrays and remove duplicates found in arrays. Each file contains strings followed by Integers i.e."David 10". Here is my code so far:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

//Function Declarations
int numLines(ifstream& i);
int* readData(ifstream& fIn, int arr[], int count);
void writeMerge(ofstream& fIn, int arr[], int count);

int main()
{
//Opens the first file needed for input
ifstream fileIn("file1.txt"); //'file1.txt' is the first file on BB
if (!fileIn)
{
  cerr << "Unable to open file!" << endl;
  exit(1);
}
fileIn.close();

//Opens the second file needed for input
ifstream fileIn2("file2.txt"); //'file2.txt' is the second file on BB
if (!fileIn2)
{
  cerr << "Unable to open file!" << endl;
  exit(1);
}
fileIn2.close();

//Function call to determine how many lines are in file1
int count = 0;
count = numLines(fileIn);

//Function call to determine how many lines are in file2
int count1 = 0;
count1 = numLines(fileIn2);

//Create an array of proper size
int* arr = new int[count];
int* ar2 = new int[count1];

//Read function
readData(fileIn, arr, count);
readData(fileIn, ar2, count);

//Merge arrays
/*
int finalCount = 0;
finalCount += count;
finalCount += count1;
int *arf = new int[finalCount];
*/

cout << "merged data is: " << endl;
ofstream fileOut;
writeMerge(fileOut, arf, count); //Reversed Write Function
cout << endl << "Data Written to 'merged_output.txt' file" << endl;

return 0;
}


// Function declaration which will
// count the number of lines in the file
int numLines(ifstream& i)
{
// Dclaring variables
int count = 0, num;

// Opening input file
i.open("textFile.txt");
while (i >> num)
  count++;

// Closing input file
i.close();

return count;
}


//Function responsible to read file
//Responsible to populate array
int* readData(ifstream& fIn, int arr[], int count)
{
int num;
// Opening the input file
fIn.open("textFile.txt");

//This for loop will populate the contents
//of an array into an array
for (int i = 0; i < count; i++)
{
  fIn >> num;
  arr[i] = num;
}

// closing the input file
fIn.close();

return arr;
}

void writeMerge(ofstream& fIn, int arr[], int count)
{
// Opening the output file
fIn.open("merged_output.txt");

// Writing the data of an array in the reverse order
for (int i = count; i =< 0; i++)
{
  fIn << arr[i] << endl;
  cout << arr[i] << endl;
}

// closing the output file
fIn.close();
}

Explanation / Answer

HI, Please find my implementation.

I have foxed all issue ans also implemented merge function.

Please let me know in case of any issue.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Function Declarations
int numLines(ifstream& i);
void readData(ifstream& fIn, int arr[], int count);
void writeMerge(ofstream& fIn, int ar1[], int ar2[], int count1, int count2);
int main()
{
//Opens the first file needed for input
ifstream fileIn("file1.txt"); //'file1.txt' is the first file on BB
if (!fileIn)
{
cerr << "Unable to open file!" << endl;
exit(1);
}
//Function call to determine how many lines are in file1
int count = 0;
count = numLines(fileIn);
fileIn.close();
//Opens the second file needed for input
ifstream fileIn2("file2.txt"); //'file2.txt' is the second file on BB
if (!fileIn2)
{
cerr << "Unable to open file!" << endl;
exit(1);
}
//Function call to determine how many lines are in file2
int count1 = 0;
count1 = numLines(fileIn2);
fileIn2.close();


//Create an array of proper size
int* arr = new int[count];
int* ar2 = new int[count1];
//Read function
fileIn.open("file1.txt");
fileIn2.open("file2.txt");
readData(fileIn, arr, count);
readData(fileIn2, ar2, count1);
fileIn.close();
fileIn2.close();

//Merge arrays
cout << "merged data is: " << endl;
ofstream fileOut("merged_output.txt");
writeMerge(fileOut, ar1, ar2,count, count1); //Reversed Write Function
fileOut.close();
cout << endl << "Data Written to 'merged_output.txt' file" << endl;
return 0;
}

// Function declaration which will
// count the number of lines in the file
int numLines(ifstream& i)
{
// Dclaring variables
int count = 0, num;
while (i >> num)
count++;
return count;
}

//Function responsible to read file
//Responsible to populate array
void readData(ifstream& fIn, int arr[], int count)
{
int num;
//This for loop will populate the contents
//of an array into an array
for (int i = 0; i < count; i++)
{
fIn >> num;
arr[i] = num;
}
}
void writeMerge(ofstream& fIn, int ar1[], int ar2[], int count1, int count2)
{
// writing data from first array in reverse order
for (int i = count1-1; i >= 0; i--)
{
fIn << ar1[i] << endl;
cout << ar1[i] << endl;
}
// writing data from second array in reverse order, if not available in first array
for (int i = count2-1; i >= 0; i--)
{
    bool found = false;

    // checking if ar2[i] is present in ar1 or not
   for(int j=0; j<count1; j++){
        if(ar1[j] == ar2[i]){
            found = true;
            break;
        }
    }

    if(!found){
        fIn << ar2[i] << endl;
       cout << ar2[i] << endl;
    }
  
}
}