2017-04-30 4 views
-6

Der Code auf Gauß-Jacobi-Verfahren in Angewandte Mathematik nicht erfolgreich auf Kompilierung ausführen, obwohl es keine Fehler:Gauss Jacobi-Verfahren in C

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); 
    } 
} 
+0

Fehlende Eingabewerte und erwartete Ergebnisse. –

+0

'void main' ist ungültig. 'main' sollte' int' zurückgeben. – melpomene

+1

Keine Ahnung, was Gauss-Jacobi ist, aber ich gehe weiter und nehme an, dass das Problem darin besteht, dass die ganzzahlige Division eine ganze Zahl zurückgibt. ZB "3/4" == 0, weil ganze Zahlen keine Fließkommazahlen sind. Wenn Sie jedoch '((float) 3)/4 'getan haben, erhalten Sie einen Float zurück. –

Antwort

1

Das Problem Ihr Code sind die Dinge wie diese: x = (a[1][4])/(a[1][1]);. Hier x ist float var und die Berechnung, die Sie tun, sind beide Int-Werte. Wie @PhilM sagte, 3/4 == 0, weil ganze Zahlen keine Gleitkommazahlen sind. Um dies zu beheben, sollten Sie überlegen, Casting zu machen. Es wird dein Problem beheben.

Wie Beispiel werfen:

#include <stdio.h> 

main() { 

    int sum = 17, count = 5; 
    double mean; 

    mean = (double) sum/count; 
    printf("Value of mean : %f\n", mean); 

} 

Casting ist einfach, Sie setzen nur (the type of var) vor der Variablen. In Ihrem Fall (float)(a[1][4])/ (float)(a[1][1]);.