C Error: invalid types double [int] 'for array index

I am creating a program to read a file and store its data in an array, and then I create a function to calculate its derivative, I was able to read the files and create a derivative, but when I try to create an external function to calculate the value of the derivative from the function I am facing the following error: "[error] invalid types" double [int] "for array index" I tried to solve it in different ways, but with no success. "

#include "biblioteca.h"

int main(){

FILE* fp;
fp = fopen("entrada.txt","rt");
if (fp == NULL) {
printf("Erro na abertura do arquivo!\n");
exit(1); }

int i; //grau do polinomio
fscanf(fp,"%d",&i);
printf("i = [%d]\n",i);
double mat[i-1][i+1];
int c=0; //colunas

while(c<i+1){
fscanf(fp,"%lf",&mat[0][c]);
c++;
}   

int h=i; //h grau do expoente
for(int l=0;l<i-1;l++){ //l linhas
int k=0; //contador
for(c=h;c>0;c--){
    mat[l+1][c-1]=mat[l][c]*(h-k);
    k++;
}
h--;
}

int k=0; //linhas
while(k<i){
for(c=0;c<i+1;c++){
    printf("%lf \t",mat[k][c]);}
k++;
printf("\n");
}

double x=mat[i-1][0]*-1/mat[i-1][1];
printf("x = %lf\n",x);

double x1;
x1=f(**mat,i,1,x);

system("pause");
return 0;
}

      

Library:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

double f(double mat,int i,int t,double x){
double x1=0;
for(int g=0;g<i+1;g++){
    if(x==0){
        x1=0;
    }
    if(x!=0){
        x1=mat[t][g]*pow(x,g)+x1;
    }
}
return x1;

      

}

+3


source to share


1 answer


When you call f function , you pass it double mat

as an argument. This only allows the double value to be passed as an argument to not the entire matrix. You need to change double mat

to double **mat

in the f declaration.

double f(double** mat,int i,int t,double x) 

      

However, you need to dynamically allocate the matrix to do this.



double** mat = (double**)malloc(sizeof(double*) * (i-1));
for (int l = 0; l < i-1; i++)
   mat[l] = (double*)malloc(sizeof(double) * (i+1)); 

      

Then I'll show you how to free memory :)

You created the math statically, so it's hard to pass it as an argument. However, you can also look here if you really want to pass a static 2D array as a parameter.

0


source







All Articles