Finding common elements between arrays and storing them

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 3632 Views - Last Post: 03 February 2013 - 09:36 PM Rate Topic: -----

#16 pablomirandavaliente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 24 January 2013 - 03:11 PM

This is my entire code. When i compile now it gets stucked and i have to restart the program and nothing appears in the memo2

#include <vcl.h>
#pragma hdrstop
#include <math.h>

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	double large,width,height,f;
	   int arrayax1[3],arrayax2[3],arrayax3[3],data47[10];
	 large=StrToInt(Edit1->Text);
	 width=StrToInt(Edit2->Text);
	 height=StrToInt(Edit3->Text);


   // These stores the values of f in the respective array
   //When i write large=2 width=3 height=4, i get the values i already posted before
	   for (int l = 1; l <= 1; l++)
	   {
		 for(int i=1;i<=4;i++)
		  {
			f=172*sqrt(pow((i/large),2));
			arrayax1[l]=f;
			Memo1->Lines->Add(IntToStr(arrayax1[l]));
		  }
	   }

	   for (int l = 1; l <= 1; l++)
	   {
		 for(int i=1;i<=4;i++)
		  {
			f=172*sqrt(pow((i/width),2));
			arrayax2[l]=f;
			Memo1->Lines->Add(IntToStr(arrayax2[l]));
		  }
	   }

	   for (int l = 1; l <= 1; l++)
	   {
		 for(int i=1;i<=4;i++)
		  {
			f=172*sqrt(pow((i/height),2));
			arrayax3[l]=f;
			Memo1->Lines->Add(IntToStr(arrayax3[l]));
		  }
	   }


	int SIZE_1=sizeof(arrayax1)/sizeof(arrayax1[0]);
	int SIZE_2=sizeof(arrayax2)/sizeof(arrayax2[0]);
	int SIZE_3=sizeof(arrayax3)/sizeof(arrayax3[0]);

	 // When i want to compare the 3 arrays i use

	 int m=0;
	 for (int i=0; i < SIZE_1; i++)
	 {
	  for (int j = 0; j < SIZE_2; j++)
	  {
	   for (int k = 0; k < SIZE_3; j++)
	   {
		 if (arrayax1==arrayax2 && arrayax1==arrayax3)
		  {
		   data47[m]=arrayax1[i];
		   Memo2->Lines->Add(StrToInt(data47[m]));
		   m++;
		  }
	   }
	  }
	 }

}



Please help i donīt know why this doesn't work
Was This Post Helpful? 0
  • +
  • -

#17 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,559
  • Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 24 January 2013 - 08:57 PM

You don't compare the elements of the array here :

71        if (arrayax1==arrayax2 && arrayax1==arrayax3)




You create the arrays to hold three elements.

19       int arrayax1[3],arrayax2[3],arrayax3[3]




You can try this to have a look at what your for loops are doing.

  for (int l = 1; l <= 1; l++)
  {
    for(int i=1;i<=4;i++)
    {
      AnsiString str;
      f = 172 * sqrt( pow( (i/large), 2) );
       
      str =   "l=" + IntToStr(l)
          + ", i=" + IntToStr(i)
          + ", f=" + IntToStr((int) f);
      Memo1->Lines->Add(str);
    }
  }


Was This Post Helpful? 1
  • +
  • -

#18 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Finding common elements between arrays and storing them

Posted 24 January 2013 - 09:42 PM

Do you have to use 3 levels of nested loops? I would only do that if it is part of the assignment description. Otherwise, I would try something involving an std::map<int,size_t> occurrences.

You could update it like this:
For each list l:
    For each element in l
        occurrences[l]++

And then extract the common elements like this:

common: vector
For each key (element,count) in occurrences
    if( count == number of lists being intersected )
        add element to common

This would be significantly faster than scanning each array and comparing every three-tuple over (array1*array2*array3), and it has the nice property that it generalizes to any number of lists.

I realize that you probably haven't used the standard template library (e.g., vector and map), but if you were finding the intersection between three arbitrary arrays, you would almost certainly want to use an expandable data structure, as there would be no way of knowing before hand how much space you would need to allocate. The best you could do short of that would be to allocate the minimum of the sizes of the three arrays, because there is no way the intersection of three sets could be larger than this size of the smallest.

On second thought, if the STL were allowed in this assignment, you would probably just be using std::set_intersection from <algorithm> :)/>

This post has been edited by NathanMullenax: 24 January 2013 - 09:43 PM

Was This Post Helpful? 1
  • +
  • -

#19 pablomirandavaliente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 25 January 2013 - 09:47 AM

#define: What do you mean with

Quote

You don't compare the elements of the array here :

71        if (arrayax1==arrayax2 && arrayax1==arrayax3)





You create the arrays to hold three elements.


19       int arrayax1[3],arrayax2[3],arrayax3[3]


How is this wrong? Could you explain me?


I donīt have to use all these fors in my code, so i will search for other options as NathanMullenax suggested
Was This Post Helpful? 0
  • +
  • -

#20 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: Finding common elements between arrays and storing them

Posted 25 January 2013 - 01:24 PM

How is this wrong? Could you explain me? 


You need to be comparing the ith element of arrayx1 to the jth element of arrayx2 and the kth element of arrayx3. What happened to i, j, and k in "if (arrayax1==arrayax2 && arrayax1==arrayax3)"? You had them before.
Was This Post Helpful? 1
  • +
  • -

#21 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,559
  • Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 25 January 2013 - 02:36 PM

Have another look at how to use arrays. Arrays
Was This Post Helpful? 1
  • +
  • -

#22 pablomirandavaliente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 26 January 2013 - 08:41 PM

mojo666, do you mean this?


if( arrayax1[i] == arrayax2[j] && arrayax2[j] == arrayax3[k] )
	   { data47[m] = arrayax1[i];
		 Memo2->Lines->Add(IntToStr(data47[m]));
		 m++;
	   }



I'm currently searching for other ways to resolve this, like using set_intersection. Thank you all for your replies.
Was This Post Helpful? 0
  • +
  • -

#23 pablomirandavaliente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 12:55 PM

I've been trying to write a function that intersects array 1 and 2, and then the intersection of these two intersects array 3, in order to find the intersection between the 3 arrays. I'm failing to compile this, it appears taht i have sintaxis errors. Any ideas?? Shall i declare the function in another place?? I've never wrote a function before

#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include <algorithm>
#include <iostream>
#include <vector>

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int arrayax1[3],arrayax2[3],arrayax3[3],data47[10],m,commonax12[2],
	 commonax123[2];

double large,width,height,f;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
 int intersections(int arrayax1[3], int arrayax2[3], int arrayax3[3],int commonax12[2], int commonax123[2])
	{
	 //Intersection between arrayax1 and arrayax2
	 std::vector<int> v(8);
	 std::vector<int>::iterator it;

	 it=std::set_intersection (arrayax1, arrayax1+4, arrayax2, arrayax2+3, v.begin());
	 v.resize(it-v.begin());
		for (it=v.begin(); it!=v.end(); ++it)
		 {
			commonax12[it]; //The new intersection is being stored in commonax12[]

		 }

	 //Intersection between arrayax12  and arrayax3
	 int SIZE_4=sizeof(commonax12)/sizeof(commonax12[0]);
	 std::vector<int> v2(SIZE_4+4);
	 std::vector<int>::iterator it2;

	 it2=std::set_intersection (arrayax12, arrayax12+3,arrayax3,arrayax+4, v2.begin());
	 v2.resize(it2-v2.begin());
		for (it2=v2.begin(); it2!=v2.end(); ++it2)
		 {
			commonax123[it]; //The new intersection is being stored in commonax123[]
			Memo2->Lines->Add(IntToStr(commonax123[it]));
		 }



	 return 0;
	}


}

Was This Post Helpful? 0
  • +
  • -

#24 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3551
  • View blog
  • Posts: 10,997
  • Joined: 05-May 12

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 01:59 PM

It would help if you tell us what errors you are getting. Give us all the details including line numbers as this provides important information about the error.

This post has been edited by Skydiver: 28 January 2013 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#25 pablomirandavaliente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 06:29 PM

I get Declaration sintax error in line 24, it seems ok to me. I don't know why.
Was This Post Helpful? 0
  • +
  • -

#26 jimblumberg  Icon User is online

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,401
  • Joined: 25-December 09

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 06:46 PM

Please post the complete error message exactly as it appears in your development environment. These messages have important information embedded within them to aid in locating and fixing the error. If there is more than one error or warning post them all.

Jim

This post has been edited by jimblumberg: 28 January 2013 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

#27 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,559
  • Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 07:14 PM

You are trying to create a function inside another function that is incorrect - a syntax error.

Create the function outside of other functions, how about creating an intersect function, that takes two vectors in and outputs the intersect in another vector :

TForm1 *Form1;

void intersect(vector<int> &vin_a, vector<int> &vin_b, vector<int> &vout)
{
  int size_a =  vin_a.size();
  int size_b =  vin_b.size();

  // push_back into vout

}




Also you could create an initialse vector function :

void init_vector(vector<int> &vout, int size, double large)
{
  double f;
  // your function
}




Within a calling function you can then initialise your vector :

  vector<int> vec_a;
  init_vector(vec_a, 4, 2.0);




You can initialise a vector from an array :

  int array2[] = {57, 114, 172, 229, 86};
  vector<int> vec_b(array2, array2 + ( sizeof(array2) / sizeof(int) ));


Was This Post Helpful? 1
  • +
  • -

#28 pablomirandavaliente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 08:31 PM

In lines 31 and 44 i get "[BCC32 Error] Unit1.cpp(31): E2034 Cannot convert std::_Vector_iterator<int,std::allocator<int> >' to 'int'"
Was This Post Helpful? 0
  • +
  • -

#29 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,559
  • Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 08:42 PM

The iterator points to an element so needs dereferencing.

commonax12[*it];


Was This Post Helpful? 1
  • +
  • -

#30 pablomirandavaliente  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 08:57 PM

Now i'm having problems with the memo2

[BCC32 Error] Unit1.cpp(45): E2451 Undefined symbol 'Memo2'

I have the memo2 in the design

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3