Question

In: Computer Science

C++ Write a program with the following elements: in main() -opens the 2 files provided for...

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

Solutions

Expert Solution

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)


Related Solutions

C++ Write a program with the following elements: in main() -opens the 2 files provided for...
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...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
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...
Write a program in C++ using the following 2 Files: Book.h and Source.cpp 1.)   In Book.h,...
Write a program in C++ using the following 2 Files: Book.h and Source.cpp 1.)   In Book.h, declare a struct Book with the following variables: - isbn:   string - title:    string - price: float 2.)   In main (Source.cpp), declare a Book object named: book - Use an initialization list to initialize the object with these values:           isbn à 13-12345-01           title à   “Great Gatsby”           price à 14.50 3.)   Pass the Book object to a function named: showBook - The...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
Question 1: Write a program in C++ using multi filing which means 3 files ( main...
Question 1: Write a program in C++ using multi filing which means 3 files ( main file, header file, and functions file) and attached screenshots as well. Attempt the Question completely which contain a and b parts a) Write a program that takes a number from the user and checks whether the number entered validates the given format: “0322-5441576”, xxxx-xxxxxxx where “x” implies the digits only and first two digits are 0 and 3 respectively. The program also identifies the...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT