[英]Array prints right the first time, but not for the 2nd, 3rd, etc.. time
因此,當我檢查數組中的值時,我注意到我的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
我想知道代碼有什么問題。
隨着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]);
...
}
您嘗試為不存在的元素/數組之外的元素分配一個值,因為mat2
有rows - 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
的循環條件保持不變,盡管數組大小逐漸減少。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.