October 19th, 2013, 12:46 PM

Urgent help needed!
hey guys
I'm busy with a c programming project with a couple of functions and that involved.
the issue is one of the functions which was working before is now crashing.
this function is proven and it does work well this is what it looks like
Code:
double Determinant(double **a,int n)
{
printf("\n\n%d\n", n);
printMatrix(a,&n);
int i,j,j1,j2;
double det = 0;
double **m = NULL;
if (n < 1) { // Error
} else if (n == 1) { // Shouldn't get used
det = a[0][0];
} else if (n == 2) {
det = a[0][0] * a[1][1]  a[1][0] * a[0][1];
} else {
det = 0;
for (j1=0;j1<n;j1++) {
m = malloc((n1)*sizeof(double *));
for (i=0;i<n1;i++)
m[i] = malloc((n1)*sizeof(double));
for (i=1;i<n;i++) {
j2 = 0;
for (j=0;j<n;j++) {
if (j != j1)
{
m[i1][j2] = a[i][j];
j2++;
}
}
}
det += pow(1.0,j1+2.0) * a[0][j1] * Determinant(m,n1);
for (i=0;i<n1;i++)
free(m[i]);
free(m);
}
}
return(det);
im calling the function like this
det = Determinant( G, *NodesPtr );
G is a dynamically created matrix which has had values given to its elements from a .dat file but this is all good as a printed the matrix to check that it held the correct values and it does
so can anyone tell me why this is crashing???
October 20th, 2013, 01:59 AM

Where is your stack trace showing that this is indeed the function where it is crashing?
Where is your minimal test case (and data set) which allows us to investigate the issue? You can't just pick a random 'I think this function crashes' function out of your large program and expect us to magically figure it out.
What you should do is now create a small main along these lines.
Code:
int main ( ) {
int n = 10;
int **m;
m = malloc((n1)*sizeof(double *));
for (i=0;i<n1;i++)
m[i] = malloc((n1)*sizeof(double));
// now fill in m[r][c] with valid data
// this could be simple, like m[r][c] = r + c;
// whatever, we don't care so long as it's valid data and
// causes a crash on your machine
det = Determinant(m,n); // This crashes
}
Now, if you can't make this crash in isolation, then the problem isn't with this function at all, but in some other function. Any function (cause) which makes a mess of say memory allocation can result in any random later function (effect) to crash in mysterious ways, even when that said later function has been 'proven' correct. You need to find the root cause of the problem, and that can be quite tricky to do (it's impossible for us, if you don't post a complete program which crashes).
> the issue is one of the functions which was working before is now crashing.
> this function is proven and it does work well this is what it looks like
One of these statements is false. Most likely, your idea of "proven" is flawed, since reality always wins (it crashes).
October 20th, 2013, 05:58 AM

I managed to sort the issue now im having another issue of course
hi
Ok I see now.
Im now having a problem where when I use the mydata3.dat file everything is correctly placed in the matrices and the intermediate steps in the voltage calculations are right but the final step of multiplying the vector by the matrix is gives an incorrect voltage vector answer but Im positive the MVmultiply function is correctly structured and i cannot find the problem.
i get this
I =
 0.000 
 0.000 
 0.000 
 5.000 
 0.000 
 10.000 
 0.000 
G =
 0.317 0.200 0.000 0.067 0.000 0.000 0.000 
 0.200 0.450 0.050 0.000 0.200 0.000 0.000 
 0.000 0.050 0.350 0.000 0.000 0.000 0.200 
 0.067 0.000 0.000 0.167 0.100 0.000 0.000 
 0.000 0.200 0.000 0.100 0.600 0.200 0.000 
 0.000 0.000 0.000 0.000 0.200 0.300 0.100 
 0.000 0.000 0.200 0.000 0.000 0.100 0.500 
the determinant value : 0.000113
 7.159 4.742 1.243 4.783 3.198 2.462 0.990 
 4.742 6.139 1.559 4.105 3.681 2.852 1.194 
 1.243 1.559 4.210 1.225 1.213 1.468 1.978 
 4.783 4.105 1.225 10.402 4.148 3.138 1.118 
 3.198 3.681 1.213 4.148 4.781 3.589 1.203 
 2.462 2.852 1.468 3.138 3.589 6.344 1.856 
 0.990 1.194 1.978 1.118 1.203 1.856 3.162 
but then my V is this
 0.000 
 0.000 
 0.000 
 144.596 
 0.000 
 217.096 
 0.000 
my MVmultiply looks like this :
Code:
void MVMultiply( double *Vector, double **Matrix, int *numRows, double *Answer )
{
printMatrix(Matrix,numRows);
printVector(Vector,numRows);
printf("%d",*numRows);
int i,j; //counters
for( i=0; i<(*numRows); i++ )
{
Answer[i] = 0;
}
for( i=0; i<(*numRows); i++ )
{
for( j=0; j<(*numRows); j++ )
{
Answer[i] += Matrix[i][j]*Vector[i] ; //calculates each block in Answer
}
}
printVector(Answer,numRows);
return;
}
i used print statements to see if what i sent it was right and it is right the only thing that is wrong is the answer V.
why???
help is greatly appreciated
October 20th, 2013, 08:33 AM
