'split an array into 2 equivalent arrays in C++
I have an array a[10][50] and I created an array b[10][35] that stores the elements of a but I don't know how to create the array c[10][15] where the elements of array c are the elements left over from "a", can you help me?
int main(){
//Variables iniciales
float min = -32.768; //limite inferior
float max = 32.768; //limite superior
const int dim = 10; //dimension
const int N = 50; //poblacion inicial
//inicializar A
float a[dim][N];
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < N; j++)
{
float aleatorio;
aleatorio = (float)rand() / (float)RAND_MAX;
a[i][j] = min + abs(max - min) * aleatorio;
cout << i<<" - " << j << ":" << a[i][j] << endl;
}
}
//Dividir A en presas y depredadores
float preys_rate[] = { 0.7,0.9 };
float aleatorio = (float)0 + rand() % 1 / (float)RAND_MAX;
float rate = preys_rate[0] + (preys_rate[1] - preys_rate[0]) * aleatorio;
float N_h = round(N * rate); //Numero de presas
float N_p = N - N_h; //Numero de depredadores
//Asignar los miembros de A a H
float H[dim][35] ;
float fitness_value_h[35];
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < N_h; j++)
{
H[i][j] = a[i][j];
}
}
//Evaluar la funcion en el elemento N_h
for (int i = 0; i < N_h; i++)
{
fitness_value_h[i] = funcion_obj(H,35, 10 ,dim);
}
//Asignar los miembros restantes de A a P
float P[dim][15];
//float fitness_value_p[15];
//Si pertenecen a H[J], se ignoran
for (int i = 0; i < dim; i++)
{
for (int j = 35; j< N; j--)
{
if (H[i][j]=a[i][j])
{
}else{
P[i][j] = a[i][j];
}
}
}
}
As you can see, array a[10][50] has some random data, and array H[10][35] has the values of "a" and I need a third array (in this example, P) P[10][15] that has the other "a" elements
Solution 1:[1]
Why not create the new array and simply add the remaining elements at the end of whatever loop you're using? If you know exactly how long the arrays are supposed to be, you can do this very easily.
It would be much easier if you included code in your questions so that we could see where the issue is.
EDIT: You are doing the loop in a more complicated way than necessary. Never set the initial in in a for loop to be the number of repetitions and subtracting.
for (int j = 35; j < N; j--) //BAD
for (int j = 0; j <= 35; j++) //GOOD
To fix your issue, simply add a condition to your loop to do something past the 35th repitition.
//Asignar los miembros de A a H
float H[dim][35] ;
float fitness_value_h[35];
float K[dim][15] ;
float fitness_value_k[15];
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < N_h; j++)
{
if (j < 35) {
H[i][j] = a[i][j];
}
else {
K[i][j] = a[i][j];
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
