how to call a function in an array



parent1[1, 3, 4, 5, 6]
parent2[1, 3, 3, 7, 6]
int child[5];

as you see both parent have the same 0, 1, 4 while 2 and 3 are differ. So what i want is the child array will have
child [ 1, 3, function(x), function(y), 6] while x and y will call a function that will complete the array.
1
2
3
4
5
6
7
8
9
10
11
12
double creating(???, ???)//what am i suppose to put here? parent1[]. parent2[]? because from what i learn you are suppose to create a dummy array, how to do that?
{
	int index;
	for(int i = 0;i<5;i++)
{
	if(parent1[i] == parent2[i])
		child[i] = parent1[i];
	else 
	{
		 child[i] = function(x) ;
	}
}


as you see child[i] = function (x); is not valid, that is becuase x is not declared, well i can simply declare x, but what if i have more than 1 value from both parents that are differ? do i need to declare another 5 separate variable for this code?

and let's say function x return to the previous value x 2, and if the previous value is not present it will return to 1.
Last edited on
Your function should be void /*I don't see what you could be returning*/ creating(int child[], int parent1[], int parent2[]){//...
As for your other problem, I have no clue what x is supposed to be.
oh, void function(x) yes of course, x is a temporary variable(or that's what i think). so what you suggest then?

to put it simple i want a code that compare two array of the same size, then copy all the same numbers in the same locations in an new array, and assign a new number for numbers that are differ in this case it will be hte previous number times 2.
so
parent1[1, 3, 4, 5, 6]
parent2[1, 3, 3, 7, 6]
since both parents have 1 3 6 in [0] [1] [4]
so the child will have
child[1,3,x,y,6]
x will be the previous number which is 3 times 2 which is 6
y will be the previous number which is x which is 6 times 2 = 12 so the child will have

child[1,3,6,12,6]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 // ...
if (parent1[0] != parent2[0])
	child[0] = 1;
	// You don't have a previous element for the first one.
	// Just decided on a value for child[0] where the parents' first element isn't the same.
else
	child[0] = parent1[0];

for (int i = 1; i < N; i++) { // N is the size of the arrays.
	if (parent1[i] == parent2[i])
		child[i] = parent1[i]; // You started well.
	else
		child[i] = child[i-1] * 2;
}

Is that what you mean?
Last edited on
What will you do if the first element doesn't match?
exactly, i would like to call a function in an array, the problem i gave you is just a prototype, i am actually trying to solve TSP, but anyways, but for now ignore the tsp. just answer the following

parent1[1, 3, 4, 5, 6]
parent2[1, 3, 3, 7, 6]
since both parents have 1 3 6 in [0] [1] [4]
so the child will have
child[1,3,function(x),funcion(y),6]
Last edited on
How does Zeillinger's post not answer that?
Would it be more clear for you if you replaced line 13 with
1
2
3
4
{
   int x = child[i-1];
   child[i] = function(x);
}

?
Topic archived. No new replies allowed.