-2

I have two data files, in which there are some data points with same values, I need to produce and output in which I'll only have the common data points.

Here's my code:

#include<iostream>
#include<cmath>
#include<fstream>

using namespace std;

int main()

{
  long double p_1,pd_1, age, mag, p_2, pd_2, dm_2, tsc_2, s_2, d_2, lum_2;
   double data1[1659];
double data2[1688];

std::ifstream fin ("sort.txt",std::ifstream::in);
std::ifstream gin ("sort1.txt", std::ifstream::in);

for(int i=0; i<1659; i++)
{
    fin>> p_1 >> pd_1 >> age >> mag;
    data1[i]= p_1;
}
for(int i=0; i<1688; i++)
{
    gin>> p_2 >> pd_2 >> dm_2 >> tsc_2 >> s_2 >> d_2 >> lum_2;
    data2[i]= p_2;
}

for(int i=0; i<1659; i++)
{
    if(data1[i]==data2[i])
        cout<<p_2<<"\t"<<pd_2<<"\t"<<dm_2<<"\t"<<tsc_2<<"\t"<<s_2<<"\t"<<d_2<<"\t"<<lum_2<<endl;
}

return(0);

}

I did not produce and output file as i wanted to see what my output looks like. Please help me out here.

1 Answers1

0

If your data is ordered, this should work.


This code is very poor, use it as a draft. (magic numbers, etc.)

#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

struct rec1 {
    long double p_1, pd_1, age, mag;
};

struct rec2 {
    long double p_2, pd_2, dm_2, tsc_2, s_2, d_2, lum_2;
};

int main()
{
    rec1 data1[1659];
    rec2 data2[1688];

    std::ifstream fin("sort.txt", std::ifstream::in);
    std::ifstream gin("sort1.txt", std::ifstream::in);

    for (int i = 0; i<1659; i++)
    {
        fin >> data1[i].p_1 >> data1[i].pd_1 >> data1[i].age >> data1[i].mag;
    }

    for (int j = 0; j<1688; j++)
    {
        gin >> data2[j].p_2 >> data2[j].pd_2 >> data2[j].dm_2 >> data2[j].tsc_2 >> data2[j].s_2 >> data2[j].d_2 >> data2[j].lum_2;
    }

    int i = 0, j = 0;
    while (i < 1659 && j < 1688)
    {
        if (data1[i].p_1 < data2[j].p_2) i++;
        else if (data1[i].p_1 > data2[j].p_2) j++;
        else {
            cout << data2[j].p_2 << "\t" << data2[j].pd_2 << "\t" << data2[j].dm_2 << "\t" << data2[j].tsc_2 << "\t" << data2[j].s_2 << "\t" << data2[j].d_2 << "\t" << data2[j].lum_2 << endl;
            i++; j++;
        }
    }

    fin.close();
    gin.close();

    return 0;
}

Note that it is not necessary to store the whole dataset in memory. You can do the intersection "on the fly."

MrAnno
  • 754
  • 5
  • 17