WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Try to code this psuedocode and post your attempt. Use [code][/code] tags the next time.
For element = begin to end - 1 in array
// Range Check
if element is out of range
output element
continue next iteration
end if
// Duplicity Check
for temp = element + 1 to end in array
if temp == element // Check for the first match
output element // Output the match
return from loop // Return from duplicity check
end if
end for
end for
element = final element of array
if element is out of range
output element
end if
// No need to check for duplicities as this is the final element
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Okay, no need to go through my rigmarole.
int occurences[ 16 ] = { 0 } ;
for ( int i = 0 ; i < 4 ; i++ )
{
for ( int j = 0 ; j < 4 ; j++ )
{
// Range Check here
if RangeOK
{
occurences[ array[ i ][ j ] ] += 1;
if ( occurences [ array[ i ][ j ] ] > 1 )
cout << "Duplicate Found\n";
}
}
}
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
the code you gave me outputs
{1,1,1,2,2,2, 18}
I dont think so. It should output {1,1,1,2,2,2}. My code does not output 18. That is your part.when it should output
{1,1,1,2,2,2,17,18,18,-1}
As I said before the out of range checking is your part. By making a small change in the above code you should be able to make it work the way you want.
Another thing, when you post code, format it properly. The braces should be vertically placed and no code other than comments should be in the lines with braces. It is difficult to read the code in the way you write them.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
OKay. Tell me whether the output of this function is okay. If not tell me why.
bool validateRange(int theSquare [][4], const int numRows, const int magicvalue)
{
bool good = true;
int i, j, num[16] = {0};
cout << "RANG: ";
for (i = 0; i<4 ; i++)
{
for (j=0;j<4;j++)
{
if (theSquare[i][j]<1 || theSquare[i][j] >16)
{
good = false;
cout <<theSquare[i][j] << " ";
}
else
{
num[theSquare[i][j] - 1 ] +=1; // The elements at num begins at 0; so we have to use the - 1
if (num[theSquare[i][j] - 1 ] > 1)
{
cout <<theSquare[i][j] << " ";
good = false;
}
}
}
}
if (good == true)
{
cout <<"VALID" << endl;
}
else
{
cout << endl;
}
return good;
}
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Please post your solution here. Maybe someone else will find it usefull in the future.
Edit:
Better also if you gave the test arrays also.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115