I have a 2D array which I'm trying to pass to another function but when I go to pass it, the function which I'm passing it to only gets one "row" of the array. Here's the simplified version of my program:
1 2 3 4 5 6 7 8 9 10 11
int main(){
int numberMatrix[3][3];
fillMatrix(); //this function fills my matrix
passMatrix(numberMatrix);
}
passMatrix(int numberMatrix[MATRIX_SIZE][MATRIX_SIZE]){
}
A Matrix is a pointer pointing to 1st position ( [0][0] ) if left without the [ ] ;) Actually even with the [] [] its a pointer always of course pointing to the ++ positions but its more complicated and it will difficult you so leave this thought for now...
Otherwise if you dont want to change the function declaration of matrix think you have to pass it as an antire matrix, i think like
Thanks for the replies! I still can't seem to get it to work though. I'm going to post my code a bit more detailed to see if someone can hopefully (please) help me out. Here's the important parts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
bool isMagic(constint matrixArray[3][3]);
int main(){
int numberMatrix[3][3];
fillMatrix(string numbers); // this method fills my matrix by converting a string into ints and
// placing the ints into a 2D array. The matrix is filled the way
// I want it to be at this point.
isMagic(numberMatrix); // When I try calling this function and passing the matrix, the matrix
// doesn't get sent in full, only it's first row.
}
bool isMagic(constint numberMatrix[3][3]){
// This is where I need to use the matrix, but for some reason, only the first "row" is passed
// into here
}
// matrixArray[][ MAX_ELEMENT ]
bool isMagic(constint matrixArray[][3]);
int main(){
int numberMatrix[3][3];
fillMatrix(string numbers); // this method fills my matrix by converting a string into ints and
// placing the ints into a 2D array. The matrix is filled the way
// I want it to be at this point.
isMagic(numberMatrix); // When I try calling this function and passing the matrix, the matrix
// doesn't get sent in full, only it's first row.
}
bool isMagic(constint numberMatrix[][3]){
// This is where I need to use the matrix, but for some reason, only the first "row" is passed
// into here
}
It doesn't really do anything yet....I cant do what I want to do with it because I can't get the array to get passed in correctly. The purpose of the function (once it works correctly) is to iterate through each element in the 2D array and ensure that each element has a unique value.
bool isMagic(constint matrixArray[][MATRIX_SIZE]);
int main(){
int numberMatrix[MATRIX_SIZE][MATRIX_SIZE];
bool isMagic = isMagic(numberMatrix);
}
bool isMagic(constint numberMatrix[][MATRIX_SIZE]){
bool foundDuplicateNumber = false;
for(int i = 1; i <= MATRIX_SIZE * MATRIX_SIZE; i++){
for(int j = 0; j < MATRIX_SIZE; j++){
for(int k = 0; k < MATRIX_SIZE; k++){
// when instance of number is found
if(numberMatrix[j][k] == i){
break;
}
}
}
// only one of each
returnfalse;
}
Figured it out....It was my compiler (Visual stuio 2010) that wasn't displaying my array correctly. Since passing an array only passes a reference the the first index, it was showing me only the first index's row. I manually typed in the location of the other rows in my watch window and i saw the values.