0

Hey everyone I am relearning C++ by doing some hacker rank challenges and am getting a segment fault error. The program should take in the dimensions for the matrix and compute both diagonals, then add them together. I am pretty sure the error is in how the 2d array is passed to the computeMainDiagonal and computeSecondaryDiagonal functions. Thanks for the help !

int ComputeMatrixMainDiagonal(int matrixDimensions, int* matrix){
    int rowIndent = 0;
    int diagonalValue; 

    for(int i = 0;i < matrixDimensions;i++){
        diagonalValue =+ (&matrix)[i][rowIndent];
        rowIndent++;
    }
    return diagonalValue;
}

int ComputeMatrixSecondaryDiagonal(int matrixDimensions, int* matrix){
    int rowIndent = matrixDimensions;
    int diagonalValue;

    for(int i = matrixDimensions;i > 0;i--){
        diagonalValue =+ (&matrix)[i][rowIndent];
        rowIndent--;
    }
    return diagonalValue;
}


int main() {
    int matrixDimension;
    int differenceAcrossSumsOfDiagonal;
    int matrixMainDiagonal;
    int matrixSecondaryDiagonal;

    int * matrixPointer;

    cin >> matrixDimension; //get matrix dimensions
    int matrix[matrixDimension][matrixDimension]; //declare new matrix
    for(int index = 0; index < matrixDimension;index++ ){ //populate matrix
        for(int i = 0; i < matrixDimension;i++){
            cin >> matrix[index][i];
        }
    }
    matrixMainDiagonal = ComputeMatrixMainDiagonal(matrixDimension,&matrix[matrixDimension][matrixDimension]);      
    matrixSecondaryDiagonal = ComputeMatrixSecondaryDiagonal(matrixDimension,&matrix[matrixDimension][matrixDimension]);

    differenceAcrossSumsOfDiagonal = (matrixMainDiagonal + matrixSecondaryDiagonal);

    cout << differenceAcrossSumsOfDiagonal;

    return 0;
}
Doug Ray
  • 988
  • 2
  • 9
  • 29
  • 1
    The declaration " int matrix[matrixDimension][matrixDimension]" is not valid C++ (it's a C99 VLA). The indexing used is 1-based. C++ raw array indexing is 0-based. That said you should have debugged your code yourself. True,, some people can just look at code and spot what's wrong with it, but you'll never get anywhere if you offload all small problems to others. – Cheers and hth. - Alf Nov 18 '15 at 21:31
  • `&matrix[matrixDimension][matrixDimension]` delivers an address just behind the matrix. – Martin Zabel Nov 18 '15 at 21:36
  • @MartinZabel Is that's what's known as the red pill? – Jonathan Potter Nov 18 '15 at 21:37
  • Take a look [here](http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) to get idea how to pass the multidimensional array into the function – Elena Viter Nov 18 '15 at 21:41

1 Answers1

1

Your segmentation fault likely occurs because &matrix[matrixDimension][matrixDimension] does not mean what you think it means. Your question title suggests that you think this is a way to pass the array by value (though why you would want to do so escapes me), but pass-by-value vs. pass-by-reference is a matter of how the function is declared, not of how it is called.

The expression &matrix[matrixDimension][matrixDimension] would be the address of the matrixDimensionth element of the matrixDimensionth row of the matrix. This is outside the bounds of the matrix, as the maximum index for an array is one less than the array dimension. Even if you wrote &matrix[matrixDimension - 1][matrixDimension - 1], however, it would not be what you want. You want the address of the first element of the array, which is &matrix[0][0] or simply matrix, though these are inequivalent on account of having different type (corresponding to different senses of what the elements of matrix are).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thanks for the answer I think I see what you are saying and what I am doing wrong. I am trying to pass the array to the function ComputeMatrixMainDiagonal() so that it can compute the first main diagonal. Can you elaborate on "though why you would want to do so escapes me". If i needed to manipulate the array I would need to pass it by reference but since I am just getting the values out of the array to be computed pass-by-value should be fine right? Also how could this be changed to be a pass-by-reference.Thanks ! – Doug Ray Nov 18 '15 at 21:50
  • Passing an *array* by value implies passing all its elements, which in fact you cannot do at all. You can, however, pass a pointer (by value), which is fine. – John Bollinger Nov 18 '15 at 22:07
  • @DougRay, note, too, that it is perversely appropriate that you have tagged the question with both [c] and [c++]. This is almost always the wrong thing to do, but your code uses features of each language that are not present in the other. – John Bollinger Nov 18 '15 at 22:11
  • Thanks @JohnBollinger, so I am calling my function like this matrixMainDiagonal = ComputeMatrixMainDiagonal(matrixDimension,&matrix[0][0]); Then receiving it in the function like this int ComputeMatrixMainDiagonal(int matrixDimensions, int* matrix). Which to me means pass the address of the 2d array and the function then should have a pointer to that array with the field matrix. Which then could be accessed via diagonalValue =+ matrix[i][rowIndent];. Is that logic correct ? – Doug Ray Nov 18 '15 at 22:34
  • The argument passing you describe sounds fine, but you cannot use a double subscript to dereference an `int *`. The element you can access in `main()` as `matrix[row][col]` would need to be accessed in the function as `matrix[row * matrixDimensions + col]`. – John Bollinger Nov 18 '15 at 22:43
  • Sorry for all the sub questions I am now getting my matrix values but I am noticing something strange for my for loop i get the correct values for diagonalValue but when I return it only returns the last value. This is the line I am using in my for loop diagonalValue =+ matrix[i * matrixDimensions + rowIndent]; but when I use cout << diagonalValue to see the value it returns 11 5 -12 -12 when it should be 11+5-12=4. Thanks for all the help and sticking with the question :) !!! – Doug Ray Nov 19 '15 at 00:01
  • Never mind simple mistake i flipped the += sign to a =+ sign thanks for everything ! :) – Doug Ray Nov 19 '15 at 01:18