Compare two Arrays

0

The program consists in comparing two arrays to see if it has at least one element repeated. The problem is in the function mismosElementos , specifically in:

estaElemento(a[i],b,usados))

The error resides in a[i] .

  

Error: invalid conversion to 'int' to 'const int'

If I remove the [i] the program works, but I would only compare the first value of the array a

#include<iostream>
using namespace std;

const int MAX=3;
typedef int tArrayEnt[MAX];
typedef bool tArrayBool[MAX];
void leerArray(tArrayEnt a, tArrayEnt b);
void inicializarBool(tArrayBool usados);
bool mismosElementos(const tArrayEnt a,const tArrayEnt b,tArrayBool usados);
bool estaElemento(const tArrayEnt a,const tArrayEnt b, tArrayBool usados);


int main()
{
    tArrayEnt a,b;
    tArrayBool usados;
    leerArray(a,b);
    inicializarBool(usados);
    if(mismosElementos(a,b,usados))
    {
        cout << "Elementos repetidos";
    }
    else
    {
        cout << "Elementos no repetidos";
    }
    return 0;
}

void leerArray(tArrayEnt a,tArrayEnt b)
{
    cout << "Introduce los valores del primer array:";
    for(int i=0; i<MAX; i++)
    {
       cin >> a[i];
    }

    cout << "Introduce los valores del segundo array:";
    for(int i=0; i<MAX; i++)
    {
       cin >> b[i];
    }
}

void inicializarBool(tArrayBool usados)
{
    for(int i=0; i<MAX; i++)
    {
        usados[i]=false;
    }
}

bool mismosElementos(const tArrayEnt a,const tArrayEnt b,tArrayBool usados)
{
    int i=0;
    while(i<MAX && estaElemento(a[i],b,usados))
    {
        i++;
    }
    return i==MAX;
}

bool estaElemento(const tArrayEnt a,const tArrayEnt b, tArrayBool usados)
{
    int i=0;
    bool encontrado;
    int elemento=a[i];
    while(i<MAX &&(elemento!=b[i] || usados[i]))
    {
        i++;
    }
    encontrado=i<MAX;
    if(encontrado)
    {
        usados[i]=true;
    }
    return encontrado;
}
    
asked by Vendetta 16.12.2017 в 13:37
source

1 answer

1

You're getting involved.

In estaElemento it seems that you want to search for an element of the array a in the array b . So:

  • Either you pass the array a and the index of the element to search , as a

  • or you pass directly the item to search , such as a[i] .

What you do is pass the array a but no index, so you invent one

int i = 0;
...
int elemento = a[i];

Solution:

Or you pass the element directly, changing the definition (and declaration) of estaElemento to

bool estaElemento(const int a, const tArrayEnt b, tArrayBool usados);

Either you pass the array and the index

bool estaElemento(const tArrayEnt a, const int indiceElemento, const tArrayEnt b, tArrayBool usados
    
answered by 16.12.2017 / 14:37
source