determinant of matrix in c N*N dimension return error at //answer[r][s]=input[p][q];
-
float determinant(float **input,int order)
{int j,p,q,t,i;
float pr;
float d;
float *c;
//c=memory_alloc_2D1(row,column);
float **answer;
answer=memory_alloc_2D1(order,order);
float **b;
b=memory_alloc_2D1(order,order);for(j=1;j<=order;j++)
{
int r=1,s=1;
for(p=1;p<=order;p++)
{for(q=1;q<=order;q++) { if(p!=1&&q!=j) { answer\[r\]\[s\]=input\[p\]\[q\]; // error as segmentation fault s++; if(s>order-1) { r++; s=1; } } } } for(t=1,pr=1;t<=(1+j);t++) pr=(-1)\*pr; c\[j\]=pr\*determinant(b,order-1); } for(j=1,d=0.0;j<=order;j++) { d=d+(input\[1\]\[j\]\*c\[j\]); }
return(d);
}
main.c
float determinantmatrix;
determinantmatrix=determinant(covariance1,waveframesize);//covariance1 is the input matrix
-
float determinant(float **input,int order)
{int j,p,q,t,i;
float pr;
float d;
float *c;
//c=memory_alloc_2D1(row,column);
float **answer;
answer=memory_alloc_2D1(order,order);
float **b;
b=memory_alloc_2D1(order,order);for(j=1;j<=order;j++)
{
int r=1,s=1;
for(p=1;p<=order;p++)
{for(q=1;q<=order;q++) { if(p!=1&&q!=j) { answer\[r\]\[s\]=input\[p\]\[q\]; // error as segmentation fault s++; if(s>order-1) { r++; s=1; } } } } for(t=1,pr=1;t<=(1+j);t++) pr=(-1)\*pr; c\[j\]=pr\*determinant(b,order-1); } for(j=1,d=0.0;j<=order;j++) { d=d+(input\[1\]\[j\]\*c\[j\]); }
return(d);
}
main.c
float determinantmatrix;
determinantmatrix=determinant(covariance1,waveframesize);//covariance1 is the input matrix
I'll guess that you are attempting to access past the end of an array. code like this,
for(j=1;j<=order;j++)
would normally look like this.
for(j=0;j<order;j++)
as array access is zero based, from 0 to (size - 1)
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
-
I'll guess that you are attempting to access past the end of an array. code like this,
for(j=1;j<=order;j++)
would normally look like this.
for(j=0;j<order;j++)
as array access is zero based, from 0 to (size - 1)
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
-
That issue (indexing past the end of an array) isn't a syntactic issue and won't be flagged by the compiler, it is a logic issue and you will only see a problem at run time.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
-
That issue (indexing past the end of an array) isn't a syntactic issue and won't be flagged by the compiler, it is a logic issue and you will only see a problem at run time.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
-
Do you understand what I said about array access?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
-
float determinant(float **input,int order)
{int j,p,q,t,i;
float pr;
float d;
float *c;
//c=memory_alloc_2D1(row,column);
float **answer;
answer=memory_alloc_2D1(order,order);
float **b;
b=memory_alloc_2D1(order,order);for(j=1;j<=order;j++)
{
int r=1,s=1;
for(p=1;p<=order;p++)
{for(q=1;q<=order;q++) { if(p!=1&&q!=j) { answer\[r\]\[s\]=input\[p\]\[q\]; // error as segmentation fault s++; if(s>order-1) { r++; s=1; } } } } for(t=1,pr=1;t<=(1+j);t++) pr=(-1)\*pr; c\[j\]=pr\*determinant(b,order-1); } for(j=1,d=0.0;j<=order;j++) { d=d+(input\[1\]\[j\]\*c\[j\]); }
return(d);
}
main.c
float determinantmatrix;
determinantmatrix=determinant(covariance1,waveframesize);//covariance1 is the input matrix
maibam debina wrote:
//covariance1 is the input matrix
But you've not shown how it's defined. If you have something like:
float covariance1[5][8];
And you are trying to access it in
determinant()
like:for (int p = 1; p <= 5; p++)
covariance1[p][q]...You can expect an access violation.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
The
for()
loops are wrong."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Do you understand what I said about array access?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
-
maibam debina wrote:
//covariance1 is the input matrix
But you've not shown how it's defined. If you have something like:
float covariance1[5][8];
And you are trying to access it in
determinant()
like:for (int p = 1; p <= 5; p++)
covariance1[p][q]...You can expect an access violation.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles