Thread: Program crashed, no compilation errors

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Program crashed, no compilation errors

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int primecheck(int N, int Y);
    void main()
    {
    int u,l,x;
    float z,a;
    while(scanf("%d %d",&u,&l)==2){
    if(l>u){
    u=a;
    l=u;
    a=u;
    }
    x=primecheck(u,l);
    z=(x/(l-u))*100;
    printf("%f",x);
    }
    }
    
    int primecheck(int N, int Y)
    {
        int c=0,i,k;
        for(i=N; i<=Y; i++){
        k=(i*i)+i+41;
        if(k%2!=0){
        c++;
        }
        }
         return c;
    }

    no compilation errors. but program crashed. newbie here T_T

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by kaopei View Post
    no compilation errors. but program crashed. newbie here T_T
    Please note that in future "program crashed" is not in any way a good description of what's happening. You need to give as much information as you can. I only saw when I ran it that the program gives a floating point exception which, in most circumstances, is caused by dividing by zero. Since the only division in your program is this line:

    Code:
    z=(x/(l-u))*100;
    It must be that l - u is 0, so l and u are equal. I see earlier in your code that this happens:

    Code:
    if (l > u) {
        u=a;
        l=u;
        a=u;
    }
    (oh and please use proper indentation in your code, it makes it so much easier to read)

    What do you think this code does? It actually sets all the variables u, l and a to be the same as the (originally uninitialised, and hence garbage) value of a, which I'm sure is not what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. consumer loan program errors
    By helloalyssa in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2010, 05:00 PM
  2. using fprintf crashed my program
    By kiros88 in forum C Programming
    Replies: 4
    Last Post: 11-08-2010, 01:14 PM
  3. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  4. C Program Compilation in Linux
    By sanjibdsharma in forum C Programming
    Replies: 1
    Last Post: 05-06-2005, 09:55 AM