minus zero


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdio.h>


float A[5][5],E[5][5],D=1,ms=1;
int n;

int getdata(int n){
    int x,y;
    for(x=0;x<n;x++){
        for(y=0;y<n;y++){
            printf("A[%d][%d] = ",x,y);//n*x+y+1);
            //A[x][y]=(float)n*x+y+1;
            scanf("%f",&A[x][y]);
        }
    }
    return 0;
}

int showdata(int n){
    int x,y;
    for(x=0;x<n;x++){
        printf("\n");
        for(y=0;y<n;y++){
            printf("\t%3.1f",A[x][y]);
        }
    }
    printf("\n");
    return 0;
}

int row_echelon(n){
    int x,y,j;
    float m,s;
    for(x=y=0;y<n;y++,x=y){
        if((A[x][y]!=1)&&(A[x][y]!=0)){
            m=1/A[x][y];
            ms*=m;
            for(j=y;j<n;j++){
                A[x][j]*=m;
            }
        }
        for(x=y+1;x<n;x++){
            if(A[x][y]!=0){
                s=A[x][y];
                for(j=y;j<n;j++){
                    A[x][j]-=A[y][j]*s;
                }
            }
        }
    }
    return 0;
}
                
    
int determinant(int n){
    for(;n>0;n--){
        D*=A[n-1][n-1];
    }
    D/=ms;
    return 0; 
}
    
    



int main(){
    printf("Enter the size of the matrix : ");
    scanf("%d",&n);
    getdata(n);
    showdata(n);
    printf("\nThe row-echelon form of the matrix : \n");
    row_echelon(n);
    showdata(n);
    determinant(n);
    printf("\nThe Determinant of the matrix : %3.1f\n",D);
    
    return 0;      
}


the output:

Enter the size of the matrix : 3
A[0][0] = 23
A[0][1] = 45
A[0][2] = 56
A[1][0] = 12
A[1][1] = 34
A[1][2] = 22
A[2][0] = 98
A[2][1] = 03
A[2][2] = 01

        23.0    45.0    56.0
        12.0    34.0    22.0
        98.0    3.0     1.0

The row-echelon form of the matrix :

        1.0     2.0     2.4
        0.0     1.0     -0.7
        0.0     -0.0    1.0

The Determinant of the matrix : -88832.0


i don't understand how this is happening
how can i get a -0??
It isn't zero, you are only showing it to one decimal place. Have a good read of this: http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Short answer: change the %3.1f in showdata to %3.5f
stupid me!

i didn't realize my algo was wrong
i was so sure that the value is zero that i didnt unmask the float vallue

i guess its again time to dry run :(


edit:

wait a sec if the algo is wrong how is the determinant value correct?
Last edited on
I'm sorry, looks like I was wrong - it is working fine when I compile it.

Change:
int row_echelon(n)
To:
int row_echelon(int n)

Works for me.
Last edited on
closed account (z05DSL3A)
On line 5 you have a global int named n, this will be hidden by any parameter also named n, such as determinant(int n). Not the problem but worth keeping an eye on.

Try using doubles instead of floats.
int row_echelon(n) was a typo

i corrected it before handing it in

i was wondering
1
2
3
4
5
6
7
8
9
int determinant(int n){
    for(;n>0;n--){
        D*=A[n-1][n-1];
    }
    D/=ms;
    return 0; 
}
    
    

or
1
2
3
4
5
6
7
8
9
10
int determinant(int n){
    for(;n>0;n--){
        if(A[n-1][n-1]==0){
             D=0;
             return 0;
        }
    }
    D/=ms;
    return 0; 
}


which is more efficient?
in the 2nd version there is an xtra 'if' check but the loop only runs as long necessary
so is betr?


Topic archived. No new replies allowed.