Instructions
Objective
Write a C++ assignment program to implement inner join.
Requirements and Specifications
Source Code
#include
#include
#include
#include
#include
using namespace std;
vector> read_csv(string file_name)
{
/*
This function will read the content of file_name into a vector of vectors
Each sub vector represents a row of data.
For each file, the first line is always skipped because it is assumed that it contains the header
*/
vector> data;
vector row;
string line;
ifstream myFile(file_name);
if(!myFile.is_open()) {
cout << "Could not open file " << file_name << endl;
exit(1);
}
if(myFile.good())
{
// read first line which is the header
getline(myFile, line);
string val;
while(getline(myFile, line)) {
stringstream ss(line);
row.clear();
while(getline(ss, val, ',')) {
row.push_back(val);
}
data.push_back(row);
}
myFile.close();
}
return data;
}
vector> inner_join(vector> table1, vector> table2) {
/*
Given two vectors of vector, this function will apply an inner-join to the two
datasets, based on the id of the customer. Then, this function will return a vector of vector containing that
data in the format of ID,NAME,AGE,MAKE,MODEL,YEAR
*/
// Create the vector to store output
vector> output;
// Now, for each user in table1, check if there is one car in table2
int id1, id2, age, year;
string name, make, model;
vector row;
for(int i =0; i < table1.size(); i++) {
id1 = stoi(table1[i][0]);
name = table1[i][1];
age = stoi(table1[i][2]);
// Loop through elements from table 2
for(int j = 0; j < table2.size(); j++)
{
id2 = stoi(table2[j][0]);
make = table2[j][1];
model = table2[j][2];
year = stoi(table2[j][3]);
if(id1 == id2)
{
// Append to output
row.clear();
row.push_back(to_string(id1));
row.push_back(name);
row.push_back(to_string(age));
row.push_back(make);
row.push_back(model);
row.push_back(to_string(year));
output.push_back(row);
}
}
}
return output;
}
void write_csv(string file_name, vector> data)
{
/*
This function receives a vector of vector and appends each row of data
into a csv file
*/
ofstream myFile(file_name);
myFile << "ID,NAME,AGE,MAKE,MODEL,YEAR" << endl;
for(int i = 0; i < data.size(); i++)
{
myFile << data[i][0] << "," << data[i][1] << "," << data[i][2] << "," << data[i][3] << "," << data[i][4] << "," << data[i][5] << endl;
}
myFile.close();
}
int main() {
// Read Table1.csv
vector> table1 = read_csv("Table1.csv");
// Read Table2.csv
vector> table2 = read_csv("Table2.csv");
// Apply Inner-Join
vector> result = inner_join(table1, table2);
// Now, write output
write_csv("Output.csv", result);
}
Similar Samples
Explore our extensive selection of programming assignment samples at ProgrammingHomeworkHelp.com. Whether you're working with Java, Python, C++, or tackling topics like algorithms and web development, our samples offer insightful solutions and clear explanations. Each example is crafted to aid comprehension and improve coding skills. Dive into our samples to gain practical insights and excel in your programming projects
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++