Click here to Skip to main content
15,849,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 5700 rows and 3 columns of 2d array.
have to eliminate the repeated row from that array and print only one row out of multiple repeated rows.
count=5700.
for example,
24.742235	0.005334	0.052465	
27.345229	-0.222687	0.266042	
27.345229	-0.222687	0.266042	
25.528389	-0.063540	0.117000	
24.742235	0.005334	0.052465	
25.528389	-0.063540	0.117000	
24.742235	0.005334	0.052465	
25.528389	-0.063540	0.117000	
24.742235	0.005334	0.052465	
25.528389	-0.063540	0.117000	
27.345229	-0.222687	0.266042	
24.742235	0.005334	0.052465	
27.345229	-0.222687	0.266042	
25.528389	-0.063540	0.117000	
25.528389	-0.063540	0.117000	
27.345229	-0.222687	0.266042	
27.345229	-0.222687	0.266042	
24.742235	0.005334	0.052465	
24.742235	0.005334	0.052465	
27.345229	-0.222687	0.266042	


What I have tried:

for(p=0;p<count;p++)
{
for(ppp=p+1;ppp<count;ppp++)
{
if(plane[ppp][1]!=plane[p][1])
{
printf("%lf\t%lf\t%lf\n",plane[p][0],plane[p][1],plane[p][2]);
printf("%lf\t%lf\t%lf\n",plane[ppp][0],plane[ppp][1],plane[ppp][2]);
}
}
}
Posted
Updated 27-Feb-17 23:24pm

If you want to remove all duplicates, so you are left with the distinct rows only, then the simplest way if to sort the input, so that duplicates are next to each other.
that way, it's trivial to remove duplicated rows.

C and C++ both include library routines to sort an array.
 
Share this answer
 
Comments
R!sh! 28-Feb-17 5:51am    
can you give the logic for sorting that type of array?
OriginalGriff 28-Feb-17 6:06am    
Look at the "standard" C library qsort - you call it with a pointer to your array of data, the number of elements in that array, the size of each element and a comparison function. You write the function, which is given two pointers to your data elements to compare.
R!sh! 1-Mar-17 23:40pm    
okk thank you
Taking advantage of the C++ library, you may use the std::set class. For instance:

C++
#include <iostream>
#include <set>
using namespace std;


struct DataInfo
{
  double d[3];

  bool operator()( const DataInfo & dia, const DataInfo & dib) const
  {
    for (size_t n=0; n<3; ++n)
    {
      if ( dia.d[n] < dib.d[n] ) return true;
      if ( dia.d[n] > dib.d[n] ) return false;
    }
    return false;
  }
};

int main()
{
  set <DataInfo, DataInfo> sdi;

  for (;;)
  {
    DataInfo di;
    cin >> di.d[0] >> di.d[1] >> di.d[2];
    if ( cin.eof()) break;
    sdi.insert(di);
  }


  for (const auto & di : sdi)
    cout << di.d[0] << " " << di.d[1] << di.d[2] << endl;
}
 
Share this answer
 
v2
A solution that does not change the order (untested):
/* Print first item */
printf("%lf\t%lf\t%lf\n",plane[0][0],plane[0][1],plane[0][2]);
for(p=1;p<count;p++)
{
    /* Check if a duplicate (already printed) */
    for(ppp=0;ppp<p;ppp++)
    {
        if(plane[p][1]==plane[ppp][1])
            break;
    }
    /* Not a duplicate if above loop not terminated */
    if (p >= ppp)
        printf("%lf\t%lf\t%lf\n",plane[p][0],plane[p][1],plane[p][2]);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900