Calculating an integral

This is a discussion on Calculating an integral within the C Programming forums, part of the General Programming Boards category; i have to write a program to calculate the area under cos^2(x) but no matter what i do i keep ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Calculating an integral

    i have to write a program to calculate the area under cos^2(x) but no matter what i do i keep getting 0

    I've tried simplifying it down to this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(int argc, char* argv[])
     { 
      FILE         *input;
      double 	   b;
      const char    inp_fn[]="px270prog3a.dat";
      int            i;
      double       x[10000], f[10000], integral;
      
      input = fopen(inp_fn, "r");
    
      b=1;
    
    	for(i=0; i <10000; i++)
    		{
    			fscanf(input, "%f %f", &x[i], &f[i]); 
    				
    		}
    
       integral=0;
    
    	for(i=0; x[i]<=b; i++)
    		{
    		integral += f[i-1]*(x[i]-x[i-1]);
    			
    		}
    		
    		printf("Area = %.4lf \n", integral);
    			
    return(0);
    }
    but it's still always 0.

    Any help?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a look at your last for() loop... Is x[i] always going to be less than b, which is defined as 1 earlier on?

    If x[i] is a positive number, the loop will exit leaving you with 0 in your integral variable.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > for(i=0; x[i]<=b; i++)
    b is 1, how many times around this loop does it go?

    Use a debugger to find out where the code goes. How many times does it execute each loop?

    Practice on a file with only 10 lines, not 10000 lines.

    Add some printf statements if you can't manage a debugger.
    Eg.
    printf("Here in loop, counter=%d\n", i );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(int argc, char* argv[])
     { 
      FILE         *input;
      const char    inp_fn[]="cos.dat";
      int            i;
      double       x[10], f[10], integral;
      
      input = fopen(inp_fn, "r");
    
    
    	for(i=0; i <10; i++)
    		{
    			fscanf(input, "%f" "%f", &x[i], &f[i]); 
    			printf(" x= %lf", x);
    			printf(" f= %lf", f);
    		}
    		
    printf("Here in loop, counter=%d\n", i ); 
    
       integral=0;
    
    	for(i=1; i<10; i++)
    		{
    		integral += f[i-1]*(x[i]-x[i-1]);
    			printf("Here in loop, counter=%d\n", i ); 
    			printf("Area = %.4lf \n", integral);
    		}
    				
    printf("Here in loop, counter=%d\n", i ); 
    		printf("Area = %.4lf \n", integral);
    			
    return(0);
    }
    i've changed it to this. and it's printing out x and f as huge numbers which are different in the file but im not sure what's wrong with the fscanf stuff?

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    ok, i changed x,f to x[i] and f[i] and it's giving me smaller but still not correct numbers

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    All your %f's are screwed up in your printf/scanf calls (scanf in particular)
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function ‘main’:
    foo.c:18: warning: format ‘%f’ expects type ‘float *’, but argument 3 has type ‘double *’
    foo.c:18: warning: format ‘%f’ expects type ‘float *’, but argument 4 has type ‘double *’
    foo.c:19: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
    foo.c:19: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘double *’
    foo.c:20: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
    foo.c:20: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘double *’
    foo.c:31: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
    foo.c:35: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
    foo.c:18: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
    foo.c:6: warning: unused parameter ‘argc’
    foo.c:6: warning: unused parameter ‘argv’
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 02:42 PM
  2. Calling functions, calculating integral
    By dakarn in forum C Programming
    Replies: 4
    Last Post: 11-18-2008, 12:14 AM
  3. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  4. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  5. Calculating window sizes
    By Mox in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2001, 08:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21