| | |
Need help with Array Validation
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2006
Posts: 10
Reputation:
Solved Threads: 0
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;
}
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;
}
Try to code this psuedocode and post your attempt. Use [code][/code] tags the next time.
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Mar 2006
Posts: 10
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Mar 2006
Posts: 10
Reputation:
Solved Threads: 0
Ok so I got the validation of Range to work:
However, now I need to figure out some way to check for duplicates.
C++ Syntax (Toggle Plain Text)
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.
Last edited by lanario; Apr 7th, 2006 at 3:43 am. Reason: code/uncode
Okay, no need to go through my rigmarole.
C++ Syntax (Toggle Plain Text)
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"; } } }
•
•
Join Date: Mar 2006
Posts: 10
Reputation:
Solved Threads: 0
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}
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)
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}
•
•
•
•
when it should output
{1,1,1,2,2,2,17,18,18,-1}
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.
•
•
Join Date: Mar 2006
Posts: 10
Reputation:
Solved Threads: 0
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.
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
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)
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: Loops
- Next Thread: help with parrallel arrays
Views: 2489 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






