Thread: Floating point exception

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Floating point exception

    My program is compiling correctly and I'm getting output also but out of 10 values only 9 are getting printed (have used a loop)and a floationg point exception (core dumped) message is getting printed. please help me.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Jayalakshmi!

    I'll take a wild guess and suggest you are trying to print outside the bounds of the array you have (or linked list).

    But no one can really help you without seeing the code - so post it up, and please use the code tags (in the advanced editor, click on that icon after highlighting your code).

    That will make your code easier (FAR easier) to read.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Code:
    #include<stdio.h>
    #include<math.h>
    int i,j,k,t,count,loc,min_dist=100;
    FILE *fp;
    char str1[10],str2[10],str3[10];
    int noc,not,u[50],total[50],capacity_c[150],capacity_t[150],bal[150],a[30][10],b[150][10],so[150][150],dist[50][150];
    void main(int argc,char* argv[])
    {
        
        if(argc!=2)
          exit(0);
        else
              fp= fopen(argv[1],"r");
              fscanf(fp,"%s", str1);
              fscanf(fp,"%s",str2);
              fscanf(fp,"%s", str3);
              fscanf(fp,"%d",&noc);
              //printf("%s,%d\n",str1,noc);
        for(i=0;i<noc;i++)
              for(j=0;j<3;j++)
                   fscanf(fp,"%d",&a[i][j]);
    
        for(i=0;i<noc;i++)
              for(j=0;j<3;j++)
                   printf("%d\n",a[i][j]);
               fscanf(fp,"%s", str1);
           fscanf(fp,"%s",str2);
           fscanf(fp,"%s", str3);
           fscanf(fp,"%d",&not);
           //printf("%s,%d\n",str1,not);
        for(i=0;i<not;i++)
              for(j=0;j<3;j++)
                   fscanf(fp,"%d",&b[i][j]);
        
        for(i=0;i<not;i++)
              for(j=0;j<3;j++)
                   printf("%d\n",b[i][j]);
        
        for(i=0;i<noc;i++)
              for(j=0;j<not;j++) 
              {
                 dist[i][j]=sqrt(pow(b[j][0]-a[i][0],(double)2)+pow(b[j][1]-a[i][1],(double)2));
                 //printf("%d\t",dist[i][j]);
              }
            for(i=0;i<noc;i++)
            for(j=0;j<not;j++)
               sol[i][j]=0;
        for(i=0;i<noc;i++)
              total[i]=0;
        for(i=0;i<noc;i++)
        {
              capacity_c[i]=a[i][2];
             // printf("%d\n",capacity_c[i]);
        }
        for(j=0;j<not;j++)
        {
            capacity_t[j]=b[j][2];
           // printf("%d\n",capacity_t[j]);
        }
        for(i=0;i<not;i++)
     u[i]=i;
    count=not;
        for(i=0;i<not;i++)
        {
            min_dist=100;
            k=rand()%count;
            printf("\n%d\t",k);
            t=u[k];
            u[k]=u[count--];
      for(i=0;i<noc;i++)
          {
           printf("%d\t",dist[i][t]);
            if(capacity_c[i]>=apacity_t[t])
            {
               if(dist[i][t]<min_dist)
       min_dist=dist[i][t];
               loc=i;
              }
      
        }
        sol[loc][t]=t;
        total[loc]+=1;
        capacity_c[loc]=capacity_c[loc]-capacity_t[t];
        printf("\t%d\t%d\t%d\t%d\t%d\t%d",sol[loc][t],total[loc],capacity_t[t],capacity_c[loc],min_dist,loc);
          
        }
    }
    Here is my code
    it is a terminal assignment problem. my input is a file i'm passing it using commnad line
    my input file is:
    Number of concentrators: 3
    19 76 12
    50 30 14
    23 79 13
    Number of terminals: 10
    54 28 5
    28 75 4
    84 44 4
    67 17 2
    90 41 3
    68 67 1
    24 79 3
    38 59 4
    27 86 5
    07 76 4

    help me.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by jayalakshmi View Post
    My program is compiling correctly
    Am I living in a parallel universum?

    You are now the third or fourth poster within the last hours who claims her/his posted code compiles correctly although that's not true:
    Code:
    $ gcc -ggdb3 -Wall -Wextra -o test test.c
    test.c:7:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    test.c: In function ‘main’:
    test.c:11:7: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
    test.c:11:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    test.c:47:12: error: ‘sol’ undeclared (first use in this function)
    test.c:47:12: note: each undeclared identifier is reported only once for each function it appears in
    test.c:66:9: warning: implicit declaration of function ‘rand’ [-Wimplicit-function-declaration]
    test.c:73:27: error: ‘apacity_t’ undeclared (first use in this function)
    Bye, Andreas

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Gah! Eleven huge arrays, some of them multi-dimensional, almost every variable badly named and impossible to guess the function of, with for loops nested all over the place (some of them parenthesised, some not, and a few not indented properly, just to make it even more tricky to see what's going on).

    And, no indication of where the problem is supposed to lie. I'd guess at the final for loop but without actually running it, that's literally a shot in the dark and still covers 10+ lines of code and huge amounts of data in doubly-indexed arrays which could hide the problem anywhere.

    Do yourself a favour before your code gets ripped to shreds - compile with debug options, run it, and then tell us what line the fault occurs on and the full error message (and DON'T change your code in between posting it and running the debug so the line numbers match up). Alternatively, run it in a debugger and do the same. If you REALLY can't do that, splat it with printf's until you find the EXACT line that actually causes that problem (i.e. if the printf before a line is printed, but not the one AFTER that line, the problem is at least causing THAT line to crash so we can look at what it does and why it might be crashing). At *MINIMUM* show us the output you expect and the output you actually get, with the full error.

    I can't even *begin* to work out what that program is supposed to do compared to what it actually does without single-stepping the entire damn thing and reformatting all the code. And then my first job would be to discover the line that *says* it's crashing to work backwards to find the actual problem - which you should do because you didn't give us any decent clue as to where you see that error occur.

    Also, I would assume that the point of the data file is that you are supposed to read the first line, spot the number 3 in it, and then get the code to read three lines out - NOT just read four lines because you KNOW the file has four lines, etc.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One possible issue... For this you should use the _hypot function:
    Code:
    dist[i][j]=sqrt(pow(b[j][0]-a[i][0],(double)2)+pow(b[j][1]-a[i][1],(double)2));
    The _hypot function doesn't suffer from the problem of overflow with the intermediate squared results. i.e.:
    Code:
    dist[i][j] = _hypot(b[j][0] - a[i][0], b[j][1] - a[i][1]);
    I'm mentioning this first because it may be of interest to several other people as well.

    The main issue I see though is that you have a bunch of arrays of seemingly random sizes. With 'a' and 'b' for example, the second dimension is 10, but it only needs to be 3. Then there's the disparity between 'capacity_c' which is 150 and 'a' which is only 30, and yet you copy between these!

    Do yourself a favour and use some real variable names.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating Point Exception
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 08-12-2011, 06:48 AM
  2. Floating point exception?
    By JM1082 in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2011, 08:13 PM
  3. Getting a floating point exception
    By SnertyStan in forum C Programming
    Replies: 13
    Last Post: 03-25-2008, 11:00 AM
  4. floating point exception
    By megastar in forum C Programming
    Replies: 6
    Last Post: 07-09-2007, 04:22 AM
  5. floating point exception? what causes these?
    By salvelinus in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2002, 12:12 PM