In: Computer Science
C++
Write a program with the following elements:
in main()
-opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt)
-calls a global function to determine how many lines are in each file
-creates 2 arrays of the proper size
-calls a global function to read the file and populate the array (call this function twice, once for each file/array)
-calls a global 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'
program should check to see if a name is already present in one file or another. If the name is present in both files display the name once in the merged file and add the numbers.
Example:
1st File:
Carlos 7
Tina 3
2nd File:
Lena 2
Carlos 3
Merged File:
Carlos 10
Tina 3
Lena 2
1.Code
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#include<string>
#include <algorithm>
using namespace std;
int main()
{
fstream file;
string filearr1,line,arr[100][2],arr1[100][2];
int j,row1,row2;
filearr1 = "Lab_HW9_2Merge1.txt";
file.open(filearr1.c_str());
j=0;
while( getline(file, line))
{
int i = 0;
stringstream ssin(line);
while (ssin.good() && i < 2)
{
ssin >> arr[j][i];
++i;
}
j++;
}
row1=j;
file.close();
filearr1 = "Lab_HW9_2Merge2.txt";
file.open(filearr1.c_str());
j=0;
while( getline(file, line))
{
int i = 0;
stringstream ssin(line);
while (ssin.good() && i < 2)
{
ssin >> arr1[j][i];
++i;
}
j++;
}
row2=j;
file.close();
string temp,t;
//sort first array
for(int i = 0; i < row1; ++i)
for( int j = i+1; j < row1; ++j)
{
if(arr[i][0] > arr[j][0])
{
temp = arr[i][0];
t=arr[i][1];
arr[i][0] = arr[j][0];
arr[i][1] = arr[j][1];
arr[j][0] = temp;
arr[j][1] = t;
}
}
//sort 2n array
temp="";
t="";
for(int i = 0; i < row2; ++i)
for( int j = i+1; j < row2; ++j)
{
if(arr1[i][0] > arr1[j][0])
{
temp = arr1[i][0];
t=arr1[i][1];
arr1[i][0] = arr1[j][0];
arr1[i][1] = arr1[j][1];
arr1[j][0] = temp;
arr1[j][1] = t;
}
}
cout<<endl;
string warr[row1+row2][2];
int a=0;;
int sum;
int i = 0;
j = 0; int k = 0;
while (i < row1 && j < row2) {
// If not common, print smaller
if (arr[i][0] < arr1[j][0]) {
warr[a][0]=arr[i][0];
warr[a][1]=arr[i][1];
a++;
i++;
k++;
}
else if (arr1[j][0] < arr[i][0]) {
warr[a][0]=arr1[j][0];
warr[a][1]=arr1[j][1];
a++;
k++;
j++;
}
// Skip common element
else {
warr[a][0]=arr1[j][0];
sum=stoi(arr1[j][1])+stoi(arr[i][1]);
warr[a][1]=to_string(sum);
a++;
i++;
j++;
}
}
// printing remaining elements
while (i < row1) {
warr[a][0]=arr[i][0];
warr[a][1]=arr[i][1];
a++;
i++;
}
while (j < row2) {
warr[a][0]=arr1[j][0];
warr[a][1]=arr1[j][1];
a++;
j++;
}
cout<<endl;
for(int i=0;i<row1+row2;i++)
cout<<warr[i][0]<<" "<<warr[i][1]<<"
";
cout<<"\n";
file.open ("merged_output.txt", ios::out | ios::in );
for(int y=0;y<row1+row2;y++)
{
file << warr[y][0]<<" "<<warr[y][1] <<
endl;}
file.close();
return 0;
}
2. input files
a.Lab_HW9_2Merge1.txt
carlos 7
tina 3
b.Lab_HW9_2Merge2.txt
lena 2
carlos 3
screenshot of input file 1 and file 2
3. screenshot of Output (create merged_output.txt before running the code)