Finding common elements between arrays and storing them

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

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

#31 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 28 January 2013 - 10:27 PM

There is a pointer to the Form in the cpp file, which can be used.

  Form1->Memo2->Lines->Add("-Memo-");




Functions can also be added to the Form class and will work if called from other class members.
In the editor, or where the tab reads Unit1.cpp (or whatever name) right-click to display the context menu.
Click Open Source/Header File, this will display the header file Unit1.h containing the form class.
Right-click and click Open Source/Header File again, this will redisplay the cpp file.
Was This Post Helpful? 0
  • +
  • -

#32 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 29 January 2013 - 08:20 PM

This is my entire function, it finally compiles, but it displays 0 in the memo2, and it should display 172, since it'ts the common element

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+3);
	 std::vector<int>::iterator it2;

	 it2=std::set_intersection (commonax12, commonax12+3,arrayax3,arrayax3+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[]
			
		 }



	 return commonax123[*it];
	}


          int z;
	  z=intersections(arrayax1,arrayax2,arrayax3,commonax12,commonax123) ;
	  Memo2->Lines->Add(IntToStr(z));


Was This Post Helpful? 0
  • +
  • -

#33 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 29 January 2013 - 09:18 PM

You are saying that the arrays are of constant size, and for example arrayax1[3] is of size 3.

01	int intersections(int arrayax1[3], int arrayax2[3], int arrayax3[3],int commonax12[2], int commonax123[2])



The iterator here points to a value in the vector, which you want to assign to the array.

09        for (it=v.begin(); it!=v.end(); ++it)
10        {
11	 
12           commonax12[*it]; //The new intersection is being stored in commonax12[]
13	 
14        }



You could try this, to test the data you could print it.

09        for (it=v.begin(); it!=v.end(); ++it)
10        {
11	     // The new intersection is being stored in commonax12[]
12           commonax12[ it-v.begin() ] = *it; 
13	 
14        }




Or you could have another variable for the array index.

09        for (int i=0, it=v.begin(); it!=v.end(); ++i, ++it)




You don't need a second iterator you can still use the first.

19	     std::vector<int>::iterator it2;




What are you trying to return?

32	     return commonax123[*it];




Were you wanting to create a console program?
Was This Post Helpful? 1
  • +
  • -

#34 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 30 January 2013 - 11:28 AM

The arrays commonax12 and commonax123 are meant to be arrays that contain the intersections between arrayax 1 and 2 and arrayax 1,2 and 3. I'm trying to returno the array taht contains the intersection between arrayax1,2 and 3
Was This Post Helpful? 0
  • +
  • -

#35 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 30 January 2013 - 12:06 PM

You are returning an element of the array. Arrays cannot be returned simply this way.

return commonax123[*it];



It is probably best not to return anything this way at the moment.

int intersections(int arrayax1[3], int arrayax2[3], int arrayax3[3],int commonax12[2], int commonax123[2]) 



You pass commonax123 to the function so it should be updated by the function.
Was This Post Helpful? 0
  • +
  • -

#36 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: Finding common elements between arrays and storing them

Posted 31 January 2013 - 06:26 AM

With the number of messages in this thread, so I may have missed it, but something that I wanted to remind the OP since (s)he opted to use std::set_intersection(). The function assumes that the incoming data is sorted.
Was This Post Helpful? 1
  • +
  • -

#37 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 02 February 2013 - 11:51 AM

So, #define, the array commonax123 is NOT being updated by the function?? What is the problem with the function?? How can i store in commonax123 the common elements between arrayax 1,2,3??
Was This Post Helpful? 0
  • +
  • -

#38 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 02 February 2013 - 12:25 PM

The array should be updated but you cannot return the array in the way you were attempting.

This works, notice there is no need to return the array (basically we send the address of the array, so the array is updated directly).

void change(int c[], int size)
{
  for(int i=0; i<size; i++)
    c[i] = i*12;
}

void __fastcall TForm1::Button3Click(TObject *Sender)
{
  int common[10] = {0};

  change(common, 10);

  for(int i=0; i<10; i++)   {
    Memo1->Lines->Add(IntToStr(common[i]));
  }
}


Was This Post Helpful? 0
  • +
  • -

#39 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 03 February 2013 - 09:20 PM

Im sorry but i failed to understand what the input array of the function stands for. Thank you so much for your help
Was This Post Helpful? 0
  • +
  • -

#40 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: Finding common elements between arrays and storing them

Posted 03 February 2013 - 09:36 PM

I think that it had already been suggested earlier in this thread and you may have taken the advice, but it maybe time for a review of part II of JackOfAllTrades's tutorial about functions and arrays.
Was This Post Helpful? 0
  • +
  • -

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