How to join two Arrangements? C ++

2

I am new to this, I am trying to unite the two arrangements to one, but it does not work out.

#include <iostream>

using namespace std;

int main(int argc, char** argv) {

     int cantidad1, cantidad2;
     int valor1, valor2;

     int arreglo1[cantidad1];
     int arreglo2[cantidad2];

     cout<<"Cuantos elementos tiene el primer arreglo? ";
     cin>>cantidad1;

     cout<<"Teclea los valores del primer arreglo: "<<endl;

     for(int i = 0; i< cantidad1; i++){

                cout<<(i+1)<<": ";
                cin>>valor1;
                arreglo1 [i]=valor1;
     }

    cout<<"Cuantos elementos tiene el segundo arreglo? ";
    cin>>cantidad2;

    cout<<"Teclea los valores del segundo arreglo: "<<endl;

          for(int j = 0; j<cantidad2; j++){

                cout<<(j+1)<<": ";
                cin>>valor2;
                arreglo2 [j]=valor2;
    }

    cout<<"A interseccion B: ";
    for (int i = 0; i<cantidad1; i++){
        for ( int j = 0; j<cantidad2; j++){
            if (arreglo1 [i]== arreglo2 [j]){

                cout<<arreglo1 [i]<<endl;               
                }
            }
        }       


        int arreglo3[10000];

        cout<<"A Union B: ";
        for (int i = 0; i<cantidad1; i++){
            arreglo3[i] = arreglo1 [i];
        for ( int j = 0; j<cantidad2; j++){
                arreglo3[i] = arreglo2 [j];

            for (int i = 0; i <cantidad1 + cantidad2; i++){
                cout<<arreglo3[i]<<endl;
            }
        }
    }

    return 0;            
}
    
asked by Melissa Lopez 16.04.2018 в 04:37
source

2 answers

2

Do not use VLA

Variable-length arrays or VLA (Variable Length Array) are not a feature supported by the standard ... that the VLA compile depends on the compiler you use and of the extensions you have installed ... there are better solutions.

What is a VLA ? This:

 int cantidad1, cantidad2;

 int arreglo1[cantidad1]; // <<--- VLA
 int arreglo2[cantidad2]; // <<--- VLA

They are VLA because their size is given by a variable instead of a constant.

Well, apart from the use of VLA (which may or may not compile), here we have a more serious error and that cantidad1 and cantidad2 are not initialized ... and given that these variables are used to give a size to arreglo1 and arreglo2 ... what size will these arrays have? 0? A negative number? 17 million elments each? You do not know.

Contrary to what you might think at first, this will not work:

int numElementos;
int miArray[numElementos];

// Quiero almacenar 10 elementos
numElementos=10;
for( int i=0; i<10; i++ )
  miArray[i] = /* ... */;

// Ahora quiero almacenar 20
numElementos=20;
for( int i=0; i<20; i++ )
  miArray[i] = /* ... */;

If the number of elements is given by the user you can use or dynamic memory:

int cantidad1;

std::cin >> cantidad1;
int* arreglo1 = new int[cantidad1];

// ...

delete[] arreglo1;
  

I'm trying to unite the two arrangements to one, but it does not work out.

One mistake you have is that the second loop also includes the impression of the result ... then it will come out more often than desired:

for ( int j = 0; j<cantidad2; j++){ // <<--- llave de apertura
        arreglo3[i] = arreglo2 [j];

    for (int i = 0; i <cantidad1 + cantidad2; i++){
        cout<<arreglo3[i]<<endl;
    }
} // <<--- llave de cierre

You have to correctly limit the scope of each loop:

for ( int j = 0; j<cantidad2; j++){
    arreglo3[i] = arreglo2 [j];
}

for (int i = 0; i <cantidad1 + cantidad2; i++){
    cout<<arreglo3[i]<<endl;
}

On the other hand, to join the two arrangements you need three indexes ... one for each arrangement.

Copying the first arrangement is trivial and you've already done it:

for (int i = 0; i<cantidad1; i++){
  arreglo3[i] = arreglo1 [i];

But for the second you need two indexes and you need to update both:

 for ( int i=cantidad1, j = 0; j<cantidad2; i++, j++)
   arreglo3[i] = arreglo2 [j];
    
answered by 16.04.2018 / 07:50
source
0

The problem is in the way you are nesting the for because in the second you are writing the data within the same index, I do not know if what you want to do is show the sum or if you wanted to show all the data of the two arrangements in a single arrangement

    for (int i = 0; i<cantidad1; i++){
        arreglo3[i] = arreglo1 [i];
        for ( int j = 0; j<cantidad2; j++){
            arreglo3[i] = arreglo2 [j];  // Aqui es donde sobre escribes
             for (int i = 0; i <cantidad1 + cantidad2; i++){ // se va a repetir la impresión porque esta dentro de tu ciclo principal y va a generar muchas impresiones basura, para imprimir cuando ya estén unidos tus arreglos saca este for  
            cout<<arreglo3[i]<<endl;
             } //For de impresión 
         } //Segundo For
     } //For principal  

Those would be the problems that I think your code causes you

If you wanted to join the data of one arrangement and another you could use something like this:

    int cantidadTotal = cantidad1 + cantidad2;
    int contador = 0, contador2 = 0;
    while (cantidadTotal > contador) {
        if (contador>cantidad1-1) {
            arreglo3[contador] = arreglo2[contador2];
            contador2++;
        } else {
            arreglo3[contador] = arreglo1[contador];
        }
        contador++;
    }
    
answered by 16.04.2018 в 05:24