C中的高斯Jacobi方法

时间:2017-04-30 21:14:46

标签: c++ c math

应用数学中Gauss-Jacobi方法的代码在编译时没有成功执行,尽管没有错误:

void main(){
    int a[3][4], i, j, k;
    float x,y,z;
    printf("Enter coeff of 3 equations and RHS :");
    for(i = 1; i <= 3; i++){
    printf("\nEQUATION %d",i);
    for(j = 1; j <= 4; j++){
    scanf("%d",&a[i][j]);
        }
    }
    x = (a[1][4])/(a[1][1]);
    y = (a[2][4])/(a[2][2]);
    z = (a[3][4])/(a[3][3]);
    printf("\nx0=%d y0=%d and z0=%d", x, y, z);
    printf("\nEnter no. of iterations:");
    scanf("%d", &k);
    i=0;
  while(i < k){
    i++;
    x = a[1][4]-(a[1][2]*y)-(a[1][3]*z);
    y = a[2][4]-(a[2][3]*z)-(a[2][1]*x);
    z = a[3][4]-(a[3][2]*y)-(a[3][1]*x);
    printf("\n after %d itr,\n x=%f\ny=%f\n z=%f", i, x, y, z);
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码存在以下问题:def f(x): if x in ("pk","email"): return User.objects.get(**{x:x}) return default 。这里x = (a[1][4])/(a[1][1]);是float var,你正在进行的计算都是int值。正如@PhilM所说,x,因为整数不是浮点数。要解决这个问题,你应该考虑进行投射。它会解决你的问题。

如何演示示例:

3/4 == 0

转换很简单,只需将#include <stdio.h> main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %f\n", mean ); } 放在变量前面即可。在您的情况下,(the type of var)