0

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 ]);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • 4
    Well for starters [an array of arrays is not the same thing as a pointer to a pointer](https://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456). Without more information and a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) it's really hard to say anything else. And please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Oct 11 '17 at 08:51
  • I don't really understand, you need to use parameters that you give to your programm when you execute it : `./matrix n param1 param2 param3` or just some variables which are in your main function? – pierreafranck Oct 11 '17 at 09:01
  • i have this in my main function: 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 ]); –  Oct 11 '17 at 09:14
  • 3
    Please add your `main` function to the question. Where do the parameters com from and how do you call the function? – Gerhardh Oct 11 '17 at 09:20
  • First, `scanf` in `main` is incorrect. And the pointer needs the area it points to. `printf(x[1], x[2], x[3]);` --> `printf("%f, %f, %f\n", x[1], x[2], x[3]);` – BLUEPIXY Oct 11 '17 at 10:23
  • I call the fuction like this in my main function: resTinf (3,a, u,v); when i compile next error appears: undefined reference to resTinf(int, double**,double*,double*) how i have to call the function? –  Oct 12 '17 at 13:21

1 Answers1

1

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 ]);
AlexK
  • 59
  • 1
  • 6