user3306583
user3306583

Reputation: 119

Simpson's rule in C - Incorrect answer

I have written a program to calculate the area under the curve for function: f = tan(x). I made a few fixes (i.e. dim must be greater than 61) so the program compiles and runs but outputs the wrong answer! I am thinking the error is in the for loops that are summing up the Tan[j]s but not sure where...

#include <stdio.h>
#include <math.h>

//Question 3a. Create a program that calculates the area under the curve of:
// y = tan(x) from 0 --> pi/3


//TODO: Declare function to change degrees to radians as in Ex. 3 of practical

float degtorad(float arg);
float pi = 3.1415927;

int main(void) {

 int i, j, sum1 = 0, sum2 = 0;
 int dim; //Loop index, Counter, Array dimension
 float a, b, rad, h, Tan[dim], area ; //Return value of the function, Result array, Area, Coefficient
 b = (float)(pi)/(float)(3);
 h = ((float)(b - a))/((float)(dim - 1));

 printf("\nPlease input a value for the step size.\n");
 scanf("%d", &dim);
 //TODO: Get table of tan as in Ex. 3
 j=0;
 for (i=0; i<=60; i++) {
    rad = degtorad(i);
    Tan[j] = tan(rad);
    j=j+1;
    }

 /*
 TODO: Calculate the are using Simpson's rule. Simpson's rule is the combination of the 
 trapezoid rule and the midpoint rule so different steps apply depending on if the step is 
 even or odd. */ 


//Check if dimension is odd
 if (dim%2 == 1) {
    for (i =  0; i < dim - 1; i++) {
        //Rule for even number. This is where the trapezoid rule is applied. 
        if (i%2 == 0) {
        sum1 = sum1 + Tan[i];
        }
        //Else is for the odd iterations that come from the midpoint rule. 
        else { 
        sum2 = sum2 + Tan[i];
        }
    }
    //Calculating the area using Simpson's rule.

    area = (h/3) * ((tan(a)) + (4 * sum2) + (2 * sum1) + (tan(b)));
    printf("\nThe area under the curve is: %1.8f\n", area); 
    }

    return 0;

}

//TODO: Definition of the function as in Ex. 3

float degtorad(float arg) {
    return( (pi * arg)/180.0 );

}

Thanks for your help!

Upvotes: 1

Views: 268

Answers (1)

Kninnug
Kninnug

Reputation: 8053

You declare Tan with size dim, which you didn't initialize before using it:

int dim; // dim isn't initialized
float Tan[dim]; // array Tan of size ?

So, dim is undefined and so is the size of Tan.

Seeing as you get dim from the user, you first need to call

scanf("%d", &dim);

and then declare Tan with it:

scanf("%d", &dim);
float Tan[dim];

ADDIT: However, your for-loop after getting the user input runs with i and j from 0 to 60 (inclusive), regardless of the user's input. You want to loop this from 0 to dim (exclusive):

for(i = 0; i < dim; i++){

And, since i and j have the same value every iteration, you don't need j:

    rad = degtorad(i);
    Tan[i] = tan(rad);
}

Your compiler should have warned you about this, if it didn't enable all warnings (usually with the -Wall option). If it did: never ignore compiler warnings!


In addition I get the following warning when acutally compiling your program:

In function 'main':
19:7: warning: 'a' is used uninitialized in this function [-Wuninitialized]
      h = ((float)(b - a))/((float)(dim - 1));

Upvotes: 3

Related Questions