Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
February 2nd, 2013, 06:16 PM
 spdpsin
Registered User

Join Date: Jan 2013
Posts: 6
Time spent in forums: 2 h 16 m 35 sec
Reputation Power: 0

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!!!

#2
February 2nd, 2013, 06:28 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,130
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 43 m 21 sec
Reputation Power: 1949
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.

#3
February 3rd, 2013, 03:28 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 3,838
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
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 1-based rather than 0-based.

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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Linker error???

## Developer Shed Advertisers and Affiliates

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |