title: implementation of trapezoidal numerical integration in C
body: <p>I'm trying to implement numerical integration using the trapezoidal approximation using this  formula :
  <img src="https://i.stack.imgur.com/iI67G.png" alt="formel"></p>
  
  <p>My problem is I don't get how to implement this correctly. To test I wrote a file with <code>22050</code> double values all equal to 2  like :</p>
  
  <pre><code>....................
      value =2.0;
      for ( index = 0 ; index &lt; 22050;index++){           
              fwrite(&amp;value,sizeof(double),1,inp2);
          }
  </code></pre>
  
  <p>to keep the question simple, say I want to the  integral value of each 100 samples:</p>
  
  <pre><code> X Area                integral value 
  0-100               should be  200  
  100-200             should be  200
  .....                ...........
  22000-22050         should be  100
  </code></pre>
  
  <p>to do that I 've wrote a program that should do that but the result that get is <code>4387950</code> for <code>100 samples</code>  here is my code :</p>
  
  <pre><code>..............................
      // opening the files 
  
  double* inputData= NULL;
  unsigned int N = 100;
  double h= 0.0;
  unsigned int index= 0;
  FILE* inputFile=NULL;
  double value =0.0;
  int i =0,j=0;
  
  
      inputFile = fopen("sinusD","rb");
      outputFile=fopen("Trapez","wb+");
      if( inputFile==NULL || outputFile==NULL){
          printf("Couldn't open the files \n");
          return -1;
      }
      inputData = (double*) malloc(sizeof(double)*N);
  
      h=22050/2;
      while((i = fread(inputData,sizeof(double),N,inputFile))==N){
          value += inputData[0] +inputData[N];
          for(index=1;index&lt;N;index++){
              value+=(2*inputData[index]);
          }
          value *=h;
          fprintf(outputFile,"%lf ",value);
          value =0;
  
      }
      if(i!=0){
          value = 0;
          i=-i;
          printf("i value %i\n", i);
          fseek(inputFile,i*sizeof(double),SEEK_END);
          fread(inputData,sizeof(double),i,inputFile);
              for(index=0;index&lt;-i;index++){
              printf("index %d\n",index);
              value += inputData[0] +inputData[i];
              value+=(2*inputData[index]);
          }
          value *=h;
          fprintf(outputFile,"%lf ",value);
          value =0;
      }
      fclose(inputFile);
      fclose(outputFile);
      free(inputData);
      return 0;}
  </code></pre>
  
  <p>any idea how to do that ? </p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>while((i = fread(inputData,sizeof(double),N,inputFile))==N){
      value = (inputData[0] + inputData[N])/2.0;
      for(index=1;index&lt;N;index++){
          value+=inputData[index];
      }
      value *=h;
      fprintf(outputFile,"%lf ",value);
      printf(" value %lf\n",value);
      value =0;
  
  }
  </code></pre>
  
  <p>I get <code>199.000</code> as a result for each segment . </p>