February 2nd, 2013, 07:16 PM

Linker error???
i am building a program that finds the longest path to calculate the solution...
this is the code:
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define _GNU_SOURCE
int startIndex = 0;
int countOneBranchLength(int** mtrx, int rowcount, int curRow){
int res = 1;
int i = startIndex;
while (i < rowcount) {
if (mtrx[i][1] == mtrx[curRow][2]){
int nodesFollowing = mtrx[i][4];
if ( nodesFollowing > 0) {
int presStartIndex = startIndex;
startIndex = 0;
res += countLength(mtrx, rowcount, i, nodesFollowing) 2;
startIndex = presStartIndex;
}
startIndex = i+1;
}
i++;
}
return res + 1;
}
int countLength(int** mtrx, int rowcount, int curRow, int nodesFound){
int l = 0;
int lt = 0;
int j = 0;
while (j < nodesFound) {
lt = countOneBranchLength(mtrx,rowcount,curRow) + 1;
if (lt > l)
l = lt;
j++;
}
return l;
}
int main(int argc, const char * argv[])
{
int m,k,i,j,s1,s2,res = 0,currlength;
FILE *fp,*fp2;
fp=fopen("scidinner.in","r");
res = 0;
fscanf(fp,"%d %d",&m,&k);
int** mtrx;
mtrx=(int**)malloc(k*sizeof(int*));
for (i=0; i<k; i++)
mtrx[i]=(int*)malloc(4*sizeof(int));
i = 0;
while(!feof(fp) && i < k)
{
fscanf(fp,"%d %d",&s1,&s2);
mtrx[i][1] = s1;
mtrx[i][2] = s2;
mtrx[i][3] = 1;
mtrx[i][4] = 0;
i++;
}
fclose(fp);
i=0;
while (i < k) {
s2 = mtrx[i][2];
j=0;
while (j < k) {
if (mtrx[j][1] == s2){
mtrx[j][3] = i;
mtrx[i][4]++;
}
j++;
}
i++;
}
i=0;
while (i < k) {
if (mtrx[i][4] > 0){
startIndex = 0;
currlength = countLength(mtrx, k, i, mtrx[i][4]) ;
if (currlength > res)
res = currlength;
}
i++;
}
fp2=fopen("scidinner.out","w");
fprintf(fp2,"%d", res);
fclose(fp2);
for (i=0; i<k; i++)
free((void*)mtrx[i]);
free((void*)mtrx);
return res;
}
But my debug is giving me these error reports:
In function `main':
[Linker error] undefined reference to `countLength'
ld returned 1 exit status
and I can't find my error...Please Heeelp!!!
February 2nd, 2013, 07:28 PM

Code not formatted, rendering it unreadable. So I didn't bother to even try to read it. Nor will anybody else want to bother.
This was your sixth post here, so you have been told this already: use code tags! Here is what it looks like when you do; compare this with your opening post and see the difference.
Code:
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define _GNU_SOURCE
int startIndex = 0;
int countOneBranchLength(int** mtrx, int rowcount, int curRow){
int res = 1;
int i = startIndex;
while (i < rowcount) {
if (mtrx[i][1] == mtrx[curRow][2]){
int nodesFollowing = mtrx[i][4];
if ( nodesFollowing > 0) {
int presStartIndex = startIndex;
startIndex = 0;
res += countLength(mtrx, rowcount, i, nodesFollowing) 2;
startIndex = presStartIndex;
}
startIndex = i+1;
}
i++;
}
return res + 1;
}
int countLength(int** mtrx, int rowcount, int curRow, int nodesFound){
int l = 0;
int lt = 0;
int j = 0;
while (j < nodesFound) {
lt = countOneBranchLength(mtrx,rowcount,curRow) + 1;
if (lt > l)
l = lt;
j++;
}
return l;
}
int main(int argc, const char * argv[])
{
int m,k,i,j,s1,s2,res = 0,currlength;
FILE *fp,*fp2;
fp=fopen("scidinner.in","r");
res = 0;
fscanf(fp,"%d %d",&m,&k);
int** mtrx;
mtrx=(int**)malloc(k*sizeof(int*));
for (i=0; i<k; i++)
mtrx[i]=(int*)malloc(4*sizeof(int));
i = 0;
while(!feof(fp) && i < k)
{
fscanf(fp,"%d %d",&s1,&s2);
mtrx[i][1] = s1;
mtrx[i][2] = s2;
mtrx[i][3] = 1;
mtrx[i][4] = 0;
i++;
}
fclose(fp);
i=0;
while (i < k) {
s2 = mtrx[i][2];
j=0;
while (j < k) {
if (mtrx[j][1] == s2){
mtrx[j][3] = i;
mtrx[i][4]++;
}
j++;
}
i++;
}
i=0;
while (i < k) {
if (mtrx[i][4] > 0){
startIndex = 0;
currlength = countLength(mtrx, k, i, mtrx[i][4]) ;
if (currlength > res)
res = currlength;
}
i++;
}
fp2=fopen("scidinner.out","w");
fprintf(fp2,"%d", res);
fclose(fp2);
for (i=0; i<k; i++)
free((void*)mtrx[i]);
free((void*)mtrx);
return res;
}
In the first function, countOneBranchLength, you call another function, countLength, which the compiler doesn't know anything about. countLength shows up later, but that doesn't do any good. You need to add a function prototype for countLength that is placed above countOneBranchLength.
Normally, this kind of mistake does not result in linker error, but rather in a "undefined reference" compiler warning.
Also, if this a C++, then because of function overloading you may need to check that you always give it the right argument types.
February 3rd, 2013, 04:28 AM

Code:
mtrx[i][1] = s1;
mtrx[i][2] = s2;
mtrx[i][3] = 1;
mtrx[i][4] = 0;
As far as I can tell, every single reference to the minor dimension of this matrix is 1based rather than 0based.
So EVERY reference to mrtx[x][4] (really, what did you save by not saying matrix everywhere) is an out of bounds access.
> int m,k,i,j,s1,s2,res = 0,currlength;
Use meaningful variable names.
i,j are fine, when they're just the loop control variables.
But m,k  what are those?
How about something like numRows and numCols.
As well as being readable, it's a lot harder to make a mistake (and a lot easier to spot a mistake) than a scattering of single letter identifiers.
A single letter variable takes only ONE key press to make the mistake, and the resulting bug is almost invisible to spot.