3

I am creating a program to read a file and stores its data in an array and then create a function to compute its derivative, was able to read the files and create the derivative, but when I try to create an external function to calculate the value of derivative of the function I encounter the following error: "[error] invalid types 'double [int]' for array subscript" Ja tried to solve it in different ways, but without 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;

}

2
  • 1
    double f(double mat,int i,int t,double x){ -> double f(int i, double mat[i-1][i+1], int t,double x){ Commented Nov 20, 2014 at 21:37
  • 1
    and call x1=f(i, mat, 1, x); BTW when double x[A][B], must be 1st index < A. I seems that your code is to be often beyond the range. Commented Nov 20, 2014 at 21:42

1 Answer 1

0

When you call f function, you pass double mat as argument. That passes just a double value as argument not the entire matrix. You need to change double mat to double **mat in f declaration.

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

However for that to work you need to allocate the matrix dynamically.

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 let you learn how to free the memory :)

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

Sign up to request clarification or add additional context in comments.

5 Comments

i change to double f(double mat,int i,int t,double x) and another error appears. [Error] cannot convert 'double ()[(((sizetype)(((ssizetype)(i + 1)) + -1)) + 1)]' to 'double' for argument '1' to 'double f(double*, int, int, double)'
Can you explain more what you have changed? Please read my updated post.
You can't pass a two-dimensional array of double to a function expecting type double **, that's never going to work.
had made the wrong change, I let x1 f as you said, now I change double mat [i-1] [i + 1] to double ** mat [i-1] [i + 1] and when I test program appears "[Error] invalid operands of types 'double **' and 'int' to binary 'operator *'" lines 26 and 40.
@PaulGriffiths sorry. Missed that. I'm updating my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.