josecarlosgutierrez2002@gmail.com

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.5 kB
1
Indexable
Never
#include <iostream>

using namespace std;

void cargarVec(int vector[], int n);
void eliminar_dup(int vector[], int vector_nodup[], int n, int &nodup);
void comunes(int vector1[], int vector2[], int vector3[], int n, int m, int &dimen);

int main()
{
    int vector1[50], vector2[50], vector3[50], vector_no_duplicado[50], m, n, nodup, dimen;

    cout << "Digite la cantidad del vector 1: ";
    cin >> n;
    cout << "Digite la cantidad del vector 2: ";
    cin >> m;

    cargarVec(vector1, n);
    cargarVec(vector2, m);

    for (int i = 0; i < n; i++)
    {
        cout << vector1[i] << " ";
    }

    cout << endl;

    for (int i = 0; i < m; i++)
    {
        cout << vector2[i] << " ";
    }
    cout << endl;

    comunes(vector1, vector2, vector3, n, m, dimen);
    eliminar_dup(vector3, vector_no_duplicado, dimen, nodup);

    for (int i = 0; i < dimen; i++)
    {
        cout << vector3[i] << " ";
    }
    cout << endl;

    cout << endl;
    for (int i = 0; i < nodup; i++)
    {
        cout << vector_no_duplicado[i] << " ";
    }

    return 0;
}

void cargarVec(int vector[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << "[" << i << "]: ";
        cin >> vector[i];
    }
    cout << endl;
}

void comunes(int vector1[], int vector2[], int vector3[], int n, int m, int &dimen)
{
    int aux = 0, cont = 0;

    for (int i = 0; i < n; i++)
    {
        cont = 0;
        for (int j = 0; j < m; j++)
        {
            if (vector1[i] == vector2[j])
            {
                cont++;
            }
        }

        if (cont != 0)
        {
            vector3[aux++] = vector1[i];
        }
    }

    for (int i = 0; i < m; i++)
    {
        cont = 0;
        for (int j = 0; j < n; j++)
        {
            if (vector2[i] == vector1[j])
            {
                cont++;
            }
        }

        if (cont != 0)
        {
            vector3[aux++] = vector2[i];
        }
    }

    dimen = aux;
}

void eliminar_dup(int vector[], int vector_nodup[], int n, int &nodup)
{
    int cont = 0, aux = 0, cont2 = 0;

    for (int i = 0; i < n; i++)
    {
        aux++;
        cont = 0;
        for (int j = 0; j < aux; j++)
        {
            if (vector[i] == vector[j])
            {
                cont++;
            }
        }

        if (cont == 1)
        {
            vector_nodup[cont2++] = vector[i];
        }
    }

    nodup = cont2;
}