//how do i fix this? im gettint these errors. bool sameSet(const int *aBeg, cons
ID: 3601554 • Letter: #
Question
//how do i fix this? im gettint these errors.
bool sameSet(const int *aBeg, const int *aEnd,
const int *bBeg, const int *bEnd)
{
// Add your code yere
for (auto p = aBeg; p!= aEnd; p++)
{
bool a = false;
for (auto p2 = bBeg; p2 != bEnd; p2++)
{
if(*p2 == *p)
{
a = true;
break;
}
else
{
return false;
}
}
}
for (auto q = bBeg; q != bEnd; q++)
{
bool b = false;
for (auto q2 = aBeg; q2 != aEnd; q2++)
{
if(*q2 == *q)
{
b = true;
break;
}
}
}
return true;
}
Explanation / Answer
#include <stdio.h>
//bool sameSet(const int *aBeg, const int *aEnd, const int *arrayB, const int *bEnd);
//aSize is the size of arrayA
//bSize is the size of arrayB
bool sameSet(const int *arrayA,const int aSize, const int *arrayB, const int bSize);
int main(void) {
int a[] = {1,4,1};
int b[] = {11,11,7,9,16,4,1};
int aSize = sizeof(a)/sizeof(a[0]);
int bSize = sizeof(b)/sizeof(b[0]);
bool stat = sameSet(a,aSize,b,bSize);
printf("%d",stat);
return 0;
}
//Logic :
//We want first take Array A, and compare with Array B if its a subset of Array B. We store the result in Boolean aFinal as true or false.
//Then we take Array B, and compare with Array A if its a subset of Array A. We then again store the result in Boolean bFinal as true or false.
//We take the OR of aFinal and bFinal. Since Array A might be a subset of B or Array B might be a subset of B or both. And store the result in Boolean final, //and return final
bool sameSet(const int *arrayA, const int aSize,const int *arrayB, const int bSize){
// Add your code yere
auto p = arrayA;
bool final = false;
bool aFinal = true;
bool bFinal = true;
for (int i = 0; i < aSize; i++)
{
bool a = false;
auto p2 = arrayB;
for (int j = 0; j < bSize; j++)
{
if(*p2 == *p)
{
a = true;
break;
}
p2++;
}
if(a == false){
aFinal = false;
break;
}
p++;
}
auto q = arrayB;
for (int i = 0; i < bSize; i++)
{
bool b = false;
auto q2 = arrayA;
for (int j = 0; j < aSize; j++)
{
if(*q2 == *q)
{
b = true;
break;
}
q2++;
}
if(b == false){
bFinal = false;
break;
}
q++;
}
final = aFinal | bFinal;
return final;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.