Thread: Help with program

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

    Help with program

    Hey guys,
    I'm new to programming and I'm not sure why this program won't work. As soon as I enter the value of a on the console the rest of the program just won't run. I'm sure I'm just being stupid. Any help would be appreciated. Thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
        
    float integral(int n, int a);
    
    int main(int argc, char* argv[])
    {
        int n, a;
        
        printf("Enter your integer value of a: ");
        scanf("%i", &a);
        
        
        for (n = 1; n <= 10; n++)
        {    
            printf("%i        %g\n", n, integral(n, a));
        }
        return 0;
    }    
    
    float integral(int n, int a)
    {    
        int N = 10000;
        float x, y, strip, sum;
        
    
            
        for(x = 0; x < 1; x=x+(1/N))
        {
            y = (pow(x,n))/(x+a);
            strip = y/(N);
            sum += strip;
        }
        
        return(sum);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    float integral(int n, int a)
    {   
        int N = 10000;
        float x, y, strip, sum;
         
     
             
        for(x = 0; x < 1; x=x+(1/N))
        {
            y = (pow(x,n))/(x+a);
            strip = y/(N);
            sum += strip;
        }
         
        return(sum);
    }
    A couple of points.
    1. Initialise sum to 0
    2. Make N a float, otherwise 1/N is done in integer arithmetic, and the result is 0 (your loop never exits).
    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.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Thank you so much, I knew it would be something stupid haha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  2. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM