1

I'm having trouble with this function. It's a LU factorization program. I'm trying to read columns instead of rows because I wanna put the elements of the array b there. If you could help me, I'd be thankful.

void lower(int N, double *b){
int i, j, k, Z=0;
double **L;

L = (double **) calloc(N, sizeof(double*));
for(i=0; i<N; i++){
    L[i] = (double *) calloc(N, sizeof(double));
    for(j=0; j<N; j++){
        for(k=i; k<N; k++){
            if(k > j){
                L[k][j] = b[Z];
                Z++;
            }
            else if (k == j)
                L[k][j] = 1;

        }
    }
}

print_matriz(N, L);
free(L);}

If I do this:

`for(i=0; i<N; i++){
        L[i] = (double *) calloc(N, sizeof(double));
        for(j=0; j<N; j++){
            if(i > j){
                L[i][j] = b[Z];
                Z++;
            }
            else if(i == j)
                L[i][j] = 1;

        }
    }`

I get these results below: I'm trying to make a matrix like this (example):

1 0 0 0 
2 1 0 0
3 5 1 0
4 6 7 1

But instead, I'm getting this:`

1 0 0 0 
2 1 0 0
3 4 1 0
5 6 7 1

`

juhgoomes
  • 11
  • 2
  • 1
    You do not need a cast for `calloc` in C, it is bad practice. Please google this – Ed Heal Jun 17 '17 at 15:56
  • 1
    I am in a good mood - here is the link https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Ed Heal Jun 17 '17 at 15:57
  • @EdHeal I didn't know, man. My teacher does it. But thanks! – juhgoomes Jun 17 '17 at 16:05
  • 3
    Please tell you teacher that they are wrong – Ed Heal Jun 17 '17 at 16:17
  • 1
    Compile with all warnings and debug info (`gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...) then **use the debugger** `gdb`. Read carefully http://floating-point-gui.de/. But your fix-my-code or do-my-homework question is off-topic. – Basile Starynkevitch Jun 18 '17 at 05:53
  • when calling any of the heap allocation functions: (calloc, malloc, realloc), always check (!=NULL) the returned value to assure the operation was successful. The returned value has type `void*` so can be assigned to any other pointer. Casting the value just clutters the code, making it more difficult to understand, debug, etc – user3629249 Jun 19 '17 at 16:50
  • for ease of readability and understanding: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* 2) separate code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line. – user3629249 Jun 19 '17 at 16:53
  • variable names should indicate `content` or `usage` (or better, both). Variable names like: `N` and `L` are meaningless, even in the current context – user3629249 Jun 19 '17 at 16:55

1 Answers1

0

Edited:

As described in the below comment, I think you're aiming for something like this? (can't verify it currently, sorry)

void lower(int N, double *b){
    int i, j, k, Z=0;
    double **L;

    L = calloc(N, sizeof(double*));
    for(i=0; i<N; i++){
        L[i] = calloc(N, sizeof(double));
    }
    for(j=0; j<N; j++){
        for(k=0; k<N; k++){
            if(k > j){
                L[k][j] = b[Z];
                Z++;
            }
            else if (k == j)
                L[k][j] = 1;
            }
        }
    }

    print_matriz(N, L);

    for(i=0; i<N; i++){
      free(L[i]);
    }
    free(L);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Shuri2060
  • 729
  • 6
  • 21
  • It's still crashing and I can't figure out why. But thanks though! – juhgoomes Jun 17 '17 at 16:08
  • You said you were getting 1 0 0 0 2 1 0 0 3 4 1 0 5 6 7 1 in the first place so I assumed there were no actual errors? – Shuri2060 Jun 17 '17 at 16:10
  • It depends on the 'for'. Depending on the switch only the upper part works, and if I try to change the lower part, it crashes, and vice-versa. I'll edit the question – juhgoomes Jun 17 '17 at 16:12
  • @juhgoomes Shouldn't the `j` and `k` loop be outside of the `i` loop as the `i` loop just allocates memory? – Shuri2060 Jun 17 '17 at 16:12