# Problem during runtime: Cofactor

Discussion in 'C' started by back from retirement, Nov 13, 2008.

1. ### back from retirementNew Member

Joined:
Nov 9, 2008
Messages:
72
1
Trophy Points:
0
Occupation:
Student, UG 1st Yr., Jadavpur University
Location:
Uttarpara, West Bengal, India
Hello everyone, I am Arkaprava, a college student.....I am having huge problem regarding running the following program....please help me out. It has no compilation error but during runtime, it is giving rise to strange values.....please debug it....

Code:
```#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 3

int cofactor(int a[size][size], int m, int n)
{
int b[size-1][size-1];
int i,j;
if((m>size-1)||(n>size-1))
{
printf("\nERROR!.....Check the value of m,n\n");
exit(0);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[i][j];
}
}
for(i=m+1;i<size;i++)
{
for(j=n+1;j<size;j++)
{
b[i-1][j-1]=a[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=n+1;j<size;j++)
{
b[i][j-1]=a[i][j];
}
}
for(i=m+1;i<size;i++)
{
for(j=0;j<n;j++)
{
b[i-1][j]=a[i][j];
}
}
return b[size-1][size-1];
}

void main()
{
int a[size][size], b[size-1][size-1];
int i,j,m,n;
printf("Enter row index=");
scanf("%d", &m);
printf("Enter column index=");
scanf("%d", &n);
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("Enter the element a[%d][%d]=", i, j);
scanf("%d", &a[i][j]);
}
}
printf("\nThe matrix is\n");
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
getch();
b[size-1][size-1]=cofactor(a,m,n);
printf("\nCofactor of a[%d][%d] is\n", m,n);
for(i=0;i<size-1;i++)
{
for(j=0;j<size-1;j++)
{
printf("%d\t", b[i][j]);
}
printf("\n");
}
getch();
}
```
---------------
@rk@

2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
What input do you give it? What is the strange output? What output did you expect?

3. ### back from retirementNew Member

Joined:
Nov 9, 2008
Messages:
72
1
Trophy Points:
0
Occupation:
Student, UG 1st Yr., Jadavpur University
Location:
Uttarpara, West Bengal, India
No output....it is showing error in processing data whenever I enter row=1, column=1.
I expect to print the entire cofactor matrix of that corresponding element.

4. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
First of all please switch to a sensible way of getting data from the user:
Code:
```printf("enter a number: ");
char buf[32];
fgets(buf,30,stdin);
int num=atoi(buf);
```
scanf is a terminally broken way of getting user input, I have no idea why tutors insist on keeping teaching it; it's retarded and bugridden and nobody with any skill uses it. sscanf and fscanf to process preformatted data or files are both fine, but using scanf to get data from the user Just Doesn't Work(TM).

No output at all? Are you sure about that? After you've entered 1 for m and n I would expect to see at least "Enter the element a[0][0]=". If you're not seeing that then "scanf("%d", &n);" must be crashing.

By output I mean stuff displayed on the screen by the program, not necessarily final results. There must be some output if it's showing an error.
So what error is it showing? (The program doesn't display "error in processing data" at any point, unless you've added that since uploading the code.)

5. ### back from retirementNew Member

Joined:
Nov 9, 2008
Messages:
72
1
Trophy Points:
0
Occupation:
Student, UG 1st Yr., Jadavpur University
Location:
Uttarpara, West Bengal, India
Not error as such....it's showing an invalid output for the matrix {{1,2,3},{4,5,6},{7,8,9}}.
For row index 1 and column index 1 it is givin the cofactor {{-1,8653},{9119,22053}}......

Thanks for the advise regarding scanf....I did not know......could u teach me a bit more elaborately?? please????

6. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
What output did you expect? Sorry, I know nothing about cofactors so I don't know.
Have you tried adding debug statements to the code to determine where the silly values are coming from? For example you can display the values in the first for loop with something like:
Code:
```  printf("Assigning a[i=%d][j=%d]=%d to b[i][j]\n", i, j, a[i][j]);
b[i][j]=a[i][j];
```
I'm sorry, but I don't know how to teach you more elaborately about a scanf replacement than by directly giving you the code. Could you clarify what exactly you're expecting?

7. ### back from retirementNew Member

Joined:
Nov 9, 2008
Messages:
72
1
Trophy Points:
0
Occupation:
Student, UG 1st Yr., Jadavpur University
Location:
Uttarpara, West Bengal, India
Yes I can....help me in debugging the program.....

8. ### back from retirementNew Member

Joined:
Nov 9, 2008
Messages:
72
1
Trophy Points:
0
Occupation:
Student, UG 1st Yr., Jadavpur University
Location:
Uttarpara, West Bengal, India
Yes I have tried adding debugging code....but it did not help much....

9. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
> .help me in debugging the program

Yes, to that end I've asked some questions which so far you haven't answered.
- What output did you expect? (Pointing me to a tutorial on cofactors doesn't answer this)

If adding debugging code didn't help then try adding more. Those dodgy values must be coming from SOMEWHERE.
- What output did the code I suggested display?
- Did you try adding equivalent code to the other loops?

What exactly are you expecting "return b[size-1][size-1];" to return to the caller ( using test matrix {{1,2,3},{4,5,6},{7,8,9}} ) ?

10. ### back from retirementNew Member

Joined:
Nov 9, 2008
Messages:
72
1
Trophy Points:
0
Occupation:
Student, UG 1st Yr., Jadavpur University
Location:
Uttarpara, West Bengal, India
Dear sir....I beg thousand perdons....I expected my output to be {{1,3},{7,9}}....
And I ave tried ALL the codes you have referred....and it is still undone.....some of your codes are not working in my compiler....probably because it is very old one...
I have tried equivalent codes in each and every loop....
I will still try to work it out....I have spend the whole yesterday behind this one....
I will use more debugging codes and will give you the ultimate feedback.....thanks for all your advises....

11. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
It's no problem, the questions are designed to help, but perhaps you're not going to spot this one without a little direct help.

return b[size-1][size-1]; does NOT return the whole of array b, it just returns the single integer at b[size-1][size-1], i.e. b[2][2], which is in fact undefined because only b[0..1][0..1] are defined. The four integers at b[0..1][0..1] (in the case that size=3), are not returned, and so b[size-1][size-1]=cofactor(a,m,n); only assigns a single integer to b[2][2], which is in fact the famous buffer overflow bug you may have heard so much about, because the memory at b[2][2] is not part of b but belongs to some other part of your program.

My recommendation therefore is to pass b into cofactor() rather than trying to return it, i.e.:
Code:
```void cofactor(int a[size][size], int m, int n,int b[size-1][size-1])
{
...
}

and in main:

cofactor(a,m,n,b);
```
Then cofactor() updates the b in main directly, rather than trying to update its own copy. The return statement only returns a single value.
With this change I get the following output:
Code:
```Enter row index=1
Enter column index=1

The matrix is
1       2       3
4       5       6
7       8       9

Cofactor of a[1][1] is
1       3
7       9
```
which is what you were expecting.

Joined:
Nov 9, 2008
Messages:
72