int scrambled( int a[], int b[], int len )
{
int check[len];
for ( int i=0; i<len; i ++)
{
check[i] = 0;
}
for ( int i=0; i<len; i++ )
{
int flag = 0;
for ( int j=0; j<len; j++)
{
if( b[j] == a[i] && check[j] != 1 && flag == 0)
{
check[j] = 1;
flag = 1;
}
}
if (flag = 0)
{
return 0;
}
else
{
flag = 0;
}
}
return 1;
}
int main( void )
{
int a[3] = {10, 15, 20};
int b[3] = {10, 20, 15};
if( scrambled( a, b, 3 ) == 1 )
{
printf( "b is a scrambled version of a\n" );
} else {
printf( "b has different contents to a\n" );
}
return 0;
}
This is my program to check any two arrays and see if those two arrays are scrambled versions of each other or if they contain different contents.
I'm not allowed to manipulate the main
function, so I'm only allowed to tweak with the scrambled
function at the top.
But for some reason, my function keeps saying scrambled for some rare occasions and I don't know how I can fix that?