area - Simpson's rule in C - Incorrect answer -


I've written a program to calculate the area under the curve for the function: f = tan (x) Fixed (i.e. should be more than 61 dimension) so that the program is compiled and runs but wrong answer gives output! I am thinking that the error is for the loop which is summarized over Tan [j], but it is not sure where ...

  #include & lt; Stdio.h & gt; # Include & lt; Math.h> // Question 3A Create a program that calculates the area below the curve: // y = tan (x) to 0 - & gt; PI / 3 / TODO: Announce the job to change the degree to radians in the form of x. 3 practical float digitizer (float AGR); Float pi = 3.1415927; Int main (zero) {int i, j, sum1 = 0, sum2 = 0; Int slow; // loop index, counter, array dimension float a, b, rd, h, tan [dim], region; // return value of function, result array, field, coefficient B = (float) (pi) / (float) (3); H = ((float) (b - a)) / ((float) (dim - 1)); Printf ("\ nPlease input an input for step size. \ N"); Scanf ("% d", & amp; dim); // TODO: Receive the table of tan as pre 3 j = 0; For (i = 0; i & lt; = 60; i ++) {rad = degtorad (i); Tan [h] = tan (raad); J = j + 1; } / * TODO: Counting is using Simpson's law. Simpson's law is a combination of the rule of the tripozide and the midpoint rule, so different steps depend on whether the move is or is strange. * / // Check if the dimension is weird (slow% 2 == 1) {for (i = 0; i  

Thanks for your help!

You tan with size dim , which you It did not start before using:

  int dim; // dim float tan [dim] is not initialized; // array of size tan?  
< P> Therefore, dim is undefined and such is the size of tan .

As soon as you get You need to call before user slow

  scanf ("% d", & amp; dim);  

/ Pre>

ADDIT : However, after receiving user input i with -loop for your and j 0 to 60 (inclusive), regardless of user input. You want to loop it from 0 to dim (unique):

 for  (i = 0; i  

And, since every running has the same value as i and j , you j : < / P> is not required Rad = DZRAD (i); Tan [i] = tan (rad); }

Your compiler should warn you about this, if he did not enable all warnings (usually with -value option). If this happens: Never ignore compiler warnings!


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

  function 'main': 19: 7: warning : 'A' is not used in this function [-dinification] H = ((float) (b - a)) / ((float) (dim - 1));  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -