首页 > 解决方案 > 数组第一次打印正确,但不是第二次、第三次等。时间

问题描述

因此,当我检查数组中的值时,我注意到我的mat2数组第一次打印正确,但不是第二次、第三次等……还有第二次、第三次等……它打印的时间,他们都是一样的。

这是更好地说明的代码:

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

int main(void) {
    int rows;
    int cols = 2;
    int i, j, k;
    double value;

    printf("Input number of data points: ");
    scanf("%d", &rows);
    printf("\n\n");

    double matA[rows][cols];
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("Input element at [%d][%d]: ", i, j);
            scanf("%lf", &matA[i][j]);
        }
    }

    double input;
    printf("\nInput the x-value you are interpolating: ");
    scanf("%lf", &input);

    double mat1[rows - 1];
    for(i = 0; i < rows; i++) {
        j = 0;
        mat1[i] = (matA[i + 1][j + 1] - matA[i][j + 1]) / (matA[i + 1][j] - matA[i][j]);
        j++;
    }

    printf("\n");

    for(i = 0; i < rows - 1; i++) {
        printf("%.9lf\n", mat1[i]);
    }

    double mat2[rows - 2];
    for(i = 0; i < rows; i++) {
        j = 0;
        mat2[i] = (mat1[i + 1] - mat1[i]) / (matA[i + 2][j] - matA[i][j]);
        j++;
    }

    printf("\n");

        // printing mat2 array for the first time
    for(i = 0; i < rows - 2; i++) {
        printf("%.9lf\n", mat2[i]);
    }

    double mat3[rows - 3];
    for(i = 0; i < rows; i++) {
        j = 0;
        mat3[i] = (mat2[i + 1] - mat2[i]) / (matA[i + 3][j] - matA[i][j]);
        j++;
    }

    printf("\n");

    for(i = 0; i < rows - 3; i++) {
        printf("%.9lf\n", mat3[i]);
    }

        // printing mat2 array multiple times after printing it the first time
    printf("\n");

    for(i = 0; i < rows - 2; i++) {
        printf("%.9lf\n", mat2[i]);
    }

    printf("\n");

    for(i = 0; i < rows - 2; i++) {
        printf("%.9lf\n", mat2[i]);
    }

    printf("\n");

    for(i = 0; i < rows - 2; i++) {
        printf("%.9lf\n", mat2[i]);
    }
        // end of printing mat2 array multiple times
    return 0;
}

例如,我输入以下内容:

4
8
1
9
4
10
5
11
10
9.2

这是我得到的输出(代码如何运行):

Input number of data points: 9.2


Input element at [0][0]: 8
Input element at [0][1]: 1
Input element at [1][0]: 9
Input element at [1][1]: 4
Input element at [2][0]: 10
Input element at [2][1]: 5
Input element at [3][0]: 11
Input element at [3][1]: 10

Input the x-value you are interpolating: 9.2

3.000000000
1.000000000
5.000000000

-1.000000000     // this is mat2 printing the first time, which is correct
2.000000000

1.000000000

0.105371901      // this is mat2 printing multiple times after printing it the first time
-0.520661157

0.105371901
-0.520661157

0.105371901
-0.520661157     // end of printing mat2 multiple times after printing it the first time

我想知道代码有什么问题。

标签: carrays

解决方案


定义为mat2

double mat2[rows - 2];

以及循环内的赋值:

for(i = 0; i < rows; i++)    // Note `i < rows` but not `i < (rows - 1)`.
{
...
mat2[i] = (mat1[i + 1] - mat1[i]) / (matA[i + 2][j] - matA[i][j]);
...
}

您尝试将值分配给不存在的元素/超出数组,因为mat2rows - 2元素,但没有rows - 1元素,但循环经过了rows多次。

另请注意,您尝试mat1在最后一次迭代中读取超出赋值表达式内部边界的值,因为mat1只有rows - 1元素,但没有rows元素:

mat1[i + 1]   // mat1 has only `rows - 1` elements. 

二维数组也是如此matA,但这里是维度而不是元素的问题:

matA[i + 2][j]  // matA has only `rows - 1` dimensions, not `rows + 1`.

超出数组范围的写入或读取意味着行为未定义

其他mat数组和在其各自循环中的赋值也是如此,越来越多的“越界”违规,因为循环i < rows内部用于为数组元素赋值的循环条件保持不变,尽管数组大小变化减少.


推荐阅读