Need help with Array Validation

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2006
Posts: 10
Reputation: lanario is an unknown quantity at this point 
Solved Threads: 0
lanario lanario is offline Offline
Newbie Poster

Need help with Array Validation

 
0
  #1
Apr 7th, 2006
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need help with Array Validation

 
0
  #2
Apr 7th, 2006
So what is your problem?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 10
Reputation: lanario is an unknown quantity at this point 
Solved Threads: 0
lanario lanario is offline Offline
Newbie Poster

Re: Need help with Array Validation

 
0
  #3
Apr 7th, 2006
problem is this code needs to output any duplicates or numbers that are out of the range after the abbreviation RANG:
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need help with Array Validation

 
0
  #4
Apr 7th, 2006
Try to code this psuedocode and post your attempt. Use [code][/code] tags the next time.
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 10
Reputation: lanario is an unknown quantity at this point 
Solved Threads: 0
lanario lanario is offline Offline
Newbie Poster

Re: Need help with Array Validation

 
0
  #5
Apr 7th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 10
Reputation: lanario is an unknown quantity at this point 
Solved Threads: 0
lanario lanario is offline Offline
Newbie Poster

Re: Need help with Array Validation

 
0
  #6
Apr 7th, 2006
Ok so I got the validation of Range to work:

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need help with Array Validation

 
0
  #7
Apr 7th, 2006
Okay, no need to go through my rigmarole.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 10
Reputation: lanario is an unknown quantity at this point 
Solved Threads: 0
lanario lanario is offline Offline
Newbie Poster

Re: Need help with Array Validation

 
0
  #8
Apr 7th, 2006
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}


  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need help with Array Validation

 
0
  #9
Apr 7th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 10
Reputation: lanario is an unknown quantity at this point 
Solved Threads: 0
lanario lanario is offline Offline
Newbie Poster

Re: Need help with Array Validation

 
0
  #10
Apr 7th, 2006
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.

  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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 2489 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC