944,110 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2781
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 7th, 2006
0

Need help with Array Validation

Expand Post »
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[i][j]<1 || theSquare[i][j] >16)
{good = false;
cout << theSquare[i][j] <1 || theSquare[i][j] > 16; }}

if (good == true)
{cout <<"VALID" << endl;
}
else
{
cout << endl;
}
return good;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanario is offline Offline
10 posts
since Mar 2006
Apr 7th, 2006
0

Re: Need help with Array Validation

So what is your problem?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 7th, 2006
0

Re: Need help with Array Validation

problem is this code needs to output any duplicates or numbers that are out of the range after the abbreviation RANG:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanario is offline Offline
10 posts
since Mar 2006
Apr 7th, 2006
0

Re: Need help with Array Validation

Try to code this psuedocode and post your attempt. Use [code][/code] tags the next time.
C++ Syntax (Toggle Plain Text)
  1. For element = begin to end - 1 in array
  2. // Range Check
  3. if element is out of range
  4. output element
  5. continue next iteration
  6. end if
  7.  
  8. // Duplicity Check
  9. for temp = element + 1 to end in array
  10. if temp == element // Check for the first match
  11. output element // Output the match
  12. return from loop // Return from duplicity check
  13. end if
  14. end for
  15.  
  16. end for
  17.  
  18. element = final element of array
  19. if element is out of range
  20. output element
  21. end if
  22. // No need to check for duplicities as this is the final element
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 7th, 2006
0

Re: Need help with Array Validation

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanario is offline Offline
10 posts
since Mar 2006
Apr 7th, 2006
0

Re: Need help with Array Validation

Ok so I got the validation of Range to work:

C++ Syntax (Toggle Plain Text)
  1. bool validateRange(int theSquare [][4], const int numRows, const int magicvalue)
  2. { bool good = true;int i, j;
  3. cout << "RANG: ";
  4. for (i = 0; i<4; i++)
  5. for (j=0;j<4;j++)
  6. {
  7. if (theSquare[i][j]<1 || theSquare[i][j] >16)
  8. {good = false;
  9. cout << theSquare[i][j]<< " "; }}//Validation of Range works
  10.  
  11.  
  12.  
  13. if (good == true)
  14. {cout <<"VALID" << endl;
  15. }
  16. else
  17. {
  18. cout << endl;
  19. }
  20. return good;
  21. }



However, now I need to figure out some way to check for duplicates.
Last edited by lanario; Apr 7th, 2006 at 3:43 am. Reason: code/uncode
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanario is offline Offline
10 posts
since Mar 2006
Apr 7th, 2006
0

Re: Need help with Array Validation

Okay, no need to go through my rigmarole.
C++ Syntax (Toggle Plain Text)
  1. int occurences[ 16 ] = { 0 } ;
  2. for ( int i = 0 ; i < 4 ; i++ )
  3. {
  4. for ( int j = 0 ; j < 4 ; j++ )
  5. {
  6. // Range Check here
  7.  
  8. if RangeOK
  9. {
  10. occurences[ array[ i ][ j ] ] += 1;
  11. if ( occurences [ array[ i ][ j ] ] > 1 )
  12. cout << "Duplicate Found\n";
  13. }
  14. }
  15. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 7th, 2006
0

Re: Need help with Array Validation

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}


C++ Syntax (Toggle Plain Text)
  1. bool validateRange(int theSquare [][4], const int numRows, const int magicvalue)
  2. { bool good = true;int i, j, num[16]={0};
  3. cout << "RANG: ";
  4. for (i = 0; i<4; i++)
  5. for (j=0;j<4;j++)
  6. {num[theSquare [i][j]]+=1;
  7. if (theSquare[i][j]<1 || theSquare[i][j] >16)
  8. {good = false;
  9. cout << theSquare[i][j];
  10. if(num[theSquare[i][j]] > 1)
  11. {cout <<num[theSquare[i][j]];
  12. good = false;}
  13. }
  14. }
  15.  
  16.  
  17. if (good == true)
  18. {cout <<"VALID" << endl;
  19. }
  20. else
  21. {
  22. cout << endl;
  23. }
  24. return good;
  25. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanario is offline Offline
10 posts
since Mar 2006
Apr 7th, 2006
0

Re: Need help with Array Validation

Quote ...
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.
Quote ...
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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 7th, 2006
0

Re: Need help with Array Validation

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.

C++ Syntax (Toggle Plain Text)
  1. bool validateRange(int theSquare [][4], const int numRows, const int magicvalue)
  2. {
  3. bool good = true;int i, j, num[16] = {1};
  4.  
  5. cout << "RANG: ";
  6.  
  7. for (i = 0; i<4; i++)
  8.  
  9. for (j=0;j<4;j++)
  10. {
  11. if (theSquare[i][j]<1 || theSquare[i][j] >16)
  12. {
  13. good = false;
  14. cout <<theSquare[i][j] << " ";
  15. }
  16.  
  17. num[theSquare[i][j]] +=1;
  18.  
  19. if (num[theSquare[i][j]] > 1)
  20. cout <<theSquare[i][j] << " ";
  21.  
  22.  
  23.  
  24. }
  25. if (good == true)
  26. {
  27. cout <<"VALID" << endl;
  28. }
  29. else
  30. {
  31. cout << endl;
  32. }
  33. return good;
  34. }


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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanario is offline Offline
10 posts
since Mar 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Loops
Next Thread in C++ Forum Timeline: help with parrallel arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC