Mar 19, 2011 at 1:19am UTC
hey i am new at learning c++.
i want to create a programm that will calculate the determinant of an dyn. array if its dimension is 2*2
the array will be allocated by this:
[...]
float **matrix = new float*[firstdim];
[...]
for(int i = 0; i < a; i++){
for(int j = 0; j < a; j++){
cout << " Insert Element [" << i+1 << "][" << j+1 <<"] ";
cin >> matrix[i][j];
}
}
my calculating function will be:
float det2(float (&a)[2][2]){
return ( (a[0][0] * a[1][1]) - ( a[1][0] * a[0][1] ) );
}
and when i try to pass this argument to the function:
if (a == 2){
cout << det2(matrix[2][2]);
};
the compiler says: invalid initialization of reference of type 'float(&)[2][2]'
from exression of type 'float'
i really don't get the right way to pass my matrix to the function.
so i need a little bit help.
thanks.
( sorry that my english is so poor ^^ ).
Mar 19, 2011 at 1:56am UTC
When you write matrix[2][2], you are telling the compiler to get the element at position 3x3 of the matrix (which I suppose does not exist, as your array is only 2x2). Try just passing matrix without the subscript part (the []).