【问题标题】:C: Riemann integral problems with infinite loopC:无限循环的黎曼积分问题
【发布时间】:2014-11-11 19:15:26
【问题描述】:

我必须编写一个程序来计算正弦循环。第一项任务是创建数组,将正弦循环放入多个区间,如 [0.0 到 0.1]、[0.1 到 0.2],......并且应该有一个来自 Operator 的最大输入 之后我应该计算每个间隔的面积。

一切都很好,但是在第 14 次间隔之后,我的程序中断了,我不知道为什么.. 也许你可以帮助我,这是我的代码:

#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;        
    }

}

【问题讨论】:

  • my program breaks -- 怎么破?
  • 您的输入是什么导致“中断”?
  • 在第 14 个间隔后我没有输出,但它仍然运行
  • 我看到的一个问题是您检查了if(ARR_MAX &gt; xmax),然后在for 循环中使用xmax 之前检查了xmax = xmax*10
  • 您的innermoset do 循环将只运行一次或无限次运行,因为您不会更改问题的“全局状态”中的任何内容。也许您应该在每个循环中增加numberintervals,从而提高准确性?

标签: c integral


【解决方案1】:

arr[i] 中使用的i 可以超出数组范围,因为i 可以大于ARR_MAX

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    相关资源
    最近更新 更多