user7734947
user7734947

Reputation:

Parameters in a function

I have this function that calculates x [0], x [1], x [2] which n are the rows, L the lower matrix (0's on top diagonal), x the unknown and the b the solution. The problem is that they ask me to call it from the main and do not know how to pass the parameters to the function, I leave it here:

void resTinf (int n, double **L, double *x, double *b){
    int i, k;
    x[0]=b[0];

    for (i = 1, i<n, i++){
        x[i]=b[i];

        for (k = 0, k<i, k++){
            x[i] = x[i]-L[i][k]*x[k];
        }
    }
    x[2] = b[2] - L[2][0]*x[0]-L[2][1]*x[1];
    printf(x[1], x[2], x[3]);
}

main :

int n = 3;
double **a, *v, *u;

scanf ("% le " , &v[0 ]);
scanf ("% le " , &v[1 ]);
scanf ("% le " , &v[2 ]);
scanf ("% le " , &a[1][ 0]);
scanf ("% le " , &a[2 ][ 0]);
scanf ("% le " , &a[2 ][ 1 ]);

Upvotes: 0

Views: 100

Answers (1)

AlexK
AlexK

Reputation: 69

First, you need to allocate memory for your dynamic array or simply create static arrays at the beginning of the function. Like that:

#include <stdlib.h>  

main:

int n = 3;
double **a, *v, *u;
v = (double*)malloc(n * sizeof(double));
u = (double*)malloc(n * sizeof(double));
a = (double**)malloc(n * sizeof(double *));
for(int i = 0; i < n; i++){
 a[i] = (double*)malloc(n * sizeof(double));
}


scanf ("% le " , &v[0 ]);
scanf ("% le " , &v[1 ]);
scanf ("% le " , &v[2 ]);
scanf ("% le " , &a[1][ 0]);
scanf ("% le " , &a[2 ][ 0]);
scanf ("% le " , &a[2 ][ 1 ]);

or

 int n = 3;
double a[n][n], v[n], u[n];

scanf ("% le " , &v[0 ]);
scanf ("% le " , &v[1 ]);
scanf ("% le " , &v[2 ]);
scanf ("% le " , &a[1][ 0]);
scanf ("% le " , &a[2 ][ 0]);
scanf ("% le " , &a[2 ][ 1 ]);

Upvotes: 1

Related Questions