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;
}