Search code examples
cintegral

C: Riemann integral problems with infinite loop


I have to write a programm to calculate a sine loop. The first task was, to create arrays for which puts the sine loop into several intervals, like [0.0 to 0.1], [0.1 to 0.2], ... and there should be a maximal input form the Operator After that i should calculate the area for each interval.

Everything goes fine, but after the 14th interval my program breaks and i have no idea why.. maybe you can help me, here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ARR_MAX 1000

float f(float x) {
    float sinus = (float)sin((float)x);
    return(sinus);
}

float max(float a, float b) { 
    if (a < b)
        return(b);
    else
        return(a);
}


float min(float a, float b) { 
    if (a < b)
        return(a);
    else
        return(b);
} 

int main(void) {
    printf("Dieses Programm berechnet Ober und Untersummen einer Sinuskurve\n");    
    float x; int xmax; float result;
    int arr[ARR_MAX];
    printf("Geben Sie die Zahl xmax ein: "); // put in the max amount of numbers
    scanf("%d\n", & xmax); 
    if (ARR_MAX > xmax) {   
        xmax = xmax*10;             
        for (int i = 0; i <= xmax; i += 1) {    // here i create arrays for my intervals
            x = i;      
            arr[i] = i;
            x = x/10;

            float a = x - 0.1; 
//left interval is always 0.1 lower than variable for example you get x = 0.3 --> 0.3 - 0.1 = 0.2; so interval is a = 0.2, b = 0.3
            float b = x; 
            float T = 0.1; //accuracy

            float uppersum; 
            float lowersum;
            long numberintervals = 10; 

            do {                            

// i use this do while operation to get the result for each interval
                float lengthintervals = b/numberintervals; 
                uppersum = 0.0; 
                lowersum = 0.0; 
                int i; 
                for (i = 0; i < numberintervals;i++) { 
                    float x = a + i*lengthintervals; 
                    float y1 = f(x); 
                    float y2 = f(x + lengthintervals); 
                    float upperamount = max(y1, y2); 
                    float loweramount = min(y1, y2); 
                    uppersum += upperamount*lengthintervals; 
                    lowersum += loweramount*lengthintervals; 
                } 
            } while (uppersum - lowersum > T); 

            result = result + lowersum; 


            printf("arr[%d] = %f  -  ", arr[i], x);
            printf("Flächensumme = %f  -  ", lowersum); 
            printf("neues Ergebnis = %f\n", result);
        }
    } else {
        return EXIT_SUCCESS;        
    }

}

Solution

  • The i used in arr[i] can be out of the array bounds, because i can be greater than ARR_MAX

    int arr[ARR_MAX];
    ...
    if (ARR_MAX > xmax) {   
            xmax = xmax*10;             
            for (int i = 0; i <= xmax; i += 1) {
                ...      
                arr[i] = i;