Problem during runtime: Cofactor

back from retirement's Avatar, Join Date: Nov 2008
Contributor
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....
Thanks in advance....

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@
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What input do you give it? What is the strange output? What output did you expect?
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.)
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
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????
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
Yes I can....help me in debugging the program.....
for cofactor, please see here.....
[link]http://en.wikipedia.org/wiki/Cofactor_(linear_algebra)[/link]
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
Yes I have tried adding debugging code....but it did not help much....
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> .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}} ) ?
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
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....