I need to validate the numbers 1-16 within the given array. Any numbers that are duplicate or are outside of the range need to be outputted. While if all numbers 1-16 are inputted only once then the boolean value is true.
The numbers for the array theSquare are inputted previous to this function. So assume some generic array of numbers have been inputted.

NEED HELP QUICK:

bool validateRange(int theSquare [][4], const int numRows, const int magicvalue)
{ bool good = true;int i, j;
cout << "RANG: ";
for (i = 0; i<4; i++)
for (j=0;j<4;j++)
{
if (theSquare[j]<1 || theSquare[j] >16)
{good = false;
cout << theSquare[j] <1 || theSquare[j] > 16; }}

if (good == true)
{cout <<"VALID" << endl;
}
else
{
cout << endl;
}
return good;
}

Recommended Answers

All 13 Replies

So what is your problem?

problem is this code needs to output any duplicates or numbers that are out of the range after the abbreviation RANG:

Try to code this psuedocode and post your attempt. Use

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

I guess I don't understand exactly how to use your pseudocode, i understand C++ visual basic, but trying to translate that to C++ I don't understand.

the code I gave earlier works for one test set that I know has correct values, and it outputs correctly, however for the test set that has bad values. It just gives me gibberish since I don't know how exactly to test each individual element from the array theSquare versus every other element for duplicity and out of rangeness.

Ok so I got the validation of Range to work:

bool validateRange(int theSquare [][4], const int numRows, const int magicvalue)
{ bool good = true;int i, j;
    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]<< " "; }}//Validation of Range works



   if (good == true)
    {cout <<"VALID" << endl;
    }
    else
    {
     cout << endl;
    }
    return good;
}

However, now I need to figure out some way to check for duplicates.

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

There is a problem with the way it runs. What it does now it either says 16 rather than valid or gives me some sort of summation. Basically the way I want it to work is if there is repetition it needs to set the boolean value to false, or if there is out of range going on then it sets it to false. Or if both happen set to false.

Within set {{1,1,1,1}, {2,2,2,2}, {17,18,18, 5}, {-1, 6, 7, 8}}
the code you gave me outputs
{1,1,1,2,2,2, 18}
when it should output
{1,1,1,2,2,2,17,18,18,-1}

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++)
        {num[theSquare [i][j]]+=1;
    if (theSquare[i][j]<1 || theSquare[i][j] >16)
    {good = false;
        cout << theSquare[i][j];
       if(num[theSquare[i][j]] > 1)
        {cout <<num[theSquare[i][j]];
            good = false;}
    }
        }


    if (good == true)
    {cout <<"VALID" << endl;
    }
    else
    {
     cout << endl;
    }
    return good;
}

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.

Ok I've tried fixing what you have told me to and it still doesn't work. I've adjusted my braces to try and make it easier to read here is the code as is currently and with the following results.

Yes, I am a beginner. This is my first intro class to C++. And my teacher is less than willing to help me on this project.

bool validateRange(int theSquare [][4], const int numRows, const int magicvalue)
{
   bool good = true;int i, j, num[16] = {1};

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

         num[theSquare[i][j]] +=1;

        if (num[theSquare[i][j]] > 1)
        cout <<theSquare[i][j] << " ";



}
    if (good == true)
    {
        cout <<"VALID" << endl;
    }
    else
    {
     cout << endl;
    }
    return good;
}

Checking square for problems:
RANG: 1 1 1 2 2 2 17 18 18
DIAG: 0 1
ROWS: 0 1 2 3
COLS: 0 1 2 3
MS:NO

This one does not output -1 like it suppose to.

Checking square for problems:
RANG: 16 VALID
DIAG: VALID
ROWS: VALID
COLS: VALID
MS:YES

This outputs an extra "16" Before Valid.

RANG: 16 VALID
DIAG: VALID
ROWS: 0 1 2 3
COLS: 0 1 2 3
MS:NO

Again an extra 16

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

The output for the three matrices that I used are as follows using the code you just gave:

Checking square for problems:
RANG: 17 18 18 -1
DIAG: 0 1
ROWS: 0 1 2 3
COLS: 0 1 2 3
MS:NO

Checking square for problems:
RANG: 16
DIAG: VALID
ROWS: 0 1 2 3
COLS: 0 1 2 3
MS:NO


Checking square for problems:
RANG: 16
DIAG: VALID
ROWS: VALID
COLS: VALID
MS:NO

THIS PROJECT IS SOLVED thank you thank you thank you for the help. I appreciate it.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.