作为C的新手,我写了一些代码来制作三对角矩阵。 但是我很茫然,因为这个矩阵在初始化和可视化之间变化。 使用printf查看矩阵“在纸上”,我甚至打印了索引i和j以检查那里是否存在问题,但显然没有。 我不明白A [2,2]如何从2变为-1,你能帮我吗? 这是我的输出:
[0][0]: 2.000000 [0][1]: -1.000000 [0][2]: 0.000000 [0][3]: 0.000000 [1][0]: -1.000000 [1][1]: 2.000000 [1][2]: -1.000000 [1][3]: 0.000000 [2][0]: 0.000000 [2][1]: -1.000000 [2][2]: 2.000000 [2][3]: -1.000000 [3][0]: 0.000000 [3][1]: 0.000000 [3][2]: -1.000000 [3][3]: 2.000000 [0][0]: 0.000000 [0][1]: 0.000000 [0][2]: -1.000000 [0][3]: 2.000000 [1][0]: 0.000000 [1][1]: 0.000000 [1][2]: -1.000000 [1][3]: 2.000000 [2][0]: 0.000000 [2][1]: 0.000000 [2][2]: -1.000000 [2][3]: 2.000000 [3][0]: 0.000000 [3][1]: 0.000000 [3][2]: -1.000000 [3][3]: 2.000000虽然我的代码看起来像这样:
float **A; int i, j = 0; A_row = (float *)malloc( N * sizeof(float)); A = (float **)malloc( N * sizeof(float *)); for (i = 0; i < N; i++){ A[i] = A_row; } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { A[i][j] = 0.; if(i == j){A[i][j] = 2.;} else if(i == j-1) {A[i][j]=-1;} else if(i == j+1) {A[i][j]=-1;} printf("[%d][%d]: %f ", i, j, A[i][j]); } printf("\n"); } //check A for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("[%d][%d]: %f ", i, j, A[i][j]); } printf("\n"); }我真的很抱歉这个麻烦。
Being new to C, I wrote a little code to make a tri-diagonal matrix. However I'm at a loss, since this matrix changes between initialization and visualization. Using printf to see the matrix "as on paper", I even printed the indices i and j to check if there was an issue there, but apparently not. I do not understand how A[2, 2] changes from 2 to -1, could you please help me? Here is the output I have:
[0][0]: 2.000000 [0][1]: -1.000000 [0][2]: 0.000000 [0][3]: 0.000000 [1][0]: -1.000000 [1][1]: 2.000000 [1][2]: -1.000000 [1][3]: 0.000000 [2][0]: 0.000000 [2][1]: -1.000000 [2][2]: 2.000000 [2][3]: -1.000000 [3][0]: 0.000000 [3][1]: 0.000000 [3][2]: -1.000000 [3][3]: 2.000000 [0][0]: 0.000000 [0][1]: 0.000000 [0][2]: -1.000000 [0][3]: 2.000000 [1][0]: 0.000000 [1][1]: 0.000000 [1][2]: -1.000000 [1][3]: 2.000000 [2][0]: 0.000000 [2][1]: 0.000000 [2][2]: -1.000000 [2][3]: 2.000000 [3][0]: 0.000000 [3][1]: 0.000000 [3][2]: -1.000000 [3][3]: 2.000000While my code looks like this:
float **A; int i, j = 0; A_row = (float *)malloc( N * sizeof(float)); A = (float **)malloc( N * sizeof(float *)); for (i = 0; i < N; i++){ A[i] = A_row; } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { A[i][j] = 0.; if(i == j){A[i][j] = 2.;} else if(i == j-1) {A[i][j]=-1;} else if(i == j+1) {A[i][j]=-1;} printf("[%d][%d]: %f ", i, j, A[i][j]); } printf("\n"); } //check A for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("[%d][%d]: %f ", i, j, A[i][j]); } printf("\n"); }I'm really sorry for the trouble.
最满意答案
矩阵中的所有行都相同,它们将等于最后一行。
A[i] = A_row应该
A[i] = malloc(N * sizeof(float));即为每一行分配N float ,就像你拥有它一样,你为每一行分配“ 相同的空间 ”。 改变该空间的内容将同时影响所有行( 或者不是真的,因为实际上它们是同一行 )。
All the rows in your matrix are the same, they will be equal to the last one.
A[i] = A_rowshould be
A[i] = malloc(N * sizeof(float));i.e. allocate N floats for each row, as you have it you are assigning the "same space" for every row. Altering the contents of that space will affect all the rows simultaneously (or not really, because in fact they are the same row).
更多推荐
printf,电脑培训,计算机培训,IT培训"/> <meta name="description" cont
发布评论