0

I'm having a problem with my schooljob where I have to do in C, which consists of, get the inverse array through the enclosed array. Every time I try to call a function that have a array as parameter, that error apear "Row 37 [Error] incompatible type for argument 2 of 'Invertible'".

So far this is what I could produce in C# and then I tried to rewrite it in C, but I'm not getting success because I do not know the language.

I'm using DEV C++ to compile my code

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

int mult = 0;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
float detLaplace(int n, float a[n][n]);
float det2(float array[2][2]);
float det3(float array[3][3]);
float * cafactor(int n, float array[n][n], int Row,int Column);
float * matcafactor(int n, float a[n][n]){
float * Transpose(int n, float array[n][n]);
float * Adjugate  (int n, float array[n][n]);
float * Invertible (int n, float array[n][n]);

//MAIN #TODO
int * main(int argc, char *argv[]) {

    int l;
    int c;
    int o;
    float v;

    printf("Insert the array order: ");
    scanf("%d\n",&o);

    static float array[100][100];

    for(l=0;l<o;l++){
        for(c=0;c<o;c++){
            printf("\nInsert Cell value[%i][%i]: ",&l,&c);
            scanf("%d",&v);
        }
            printf("\n");
    }   

    array = Invertible (o,array[100][100]);

    int op = 0
    for(l=o;o>0;o--){
        op=op+(l*l);
    }

    printf("\nOperation numbers=%d |",&op);

    printf("\narray Invertible :\n |");

    for(l=0;l<o;l++){
        for(c=0;c<o;c++){
            printf("%d |",&array[l][c]);
        }
            printf("\n");
    }   

    return 0;
}





/*
    |-------------------|
    |   FUNCTIONS       |
    |-------------------|
*/

//LAPLACE
float detLaplace(int n, float a[n][n]){

    if(n == 1){
        // array 1x1
        return a[0][0];
    }
    else{
        float det = 0;
        int i, row, col, j_aux, i_aux;

        //Select first line to calculate the cofactors
        for(i = 0; i < n; i++){
            //ignore the zeros (zero time anything equal zero)
            if (a[0][i] != 0) {
                float aux[n - 1][n - 1];
                i_aux = 0;
                j_aux = 0;
                //Generate the array to calculate the cofactors
                for(row = 1; row < n; row++){
                    for(col = 0; col < n; col++){
                        if(col != i){
                            aux[i_aux][j_aux] = a[row][col];
                            j_aux++;
                        }
                    }
                    i_aux++;
                    j_aux = 0;
                }
                float factor = (i % 2 == 0)? a[0][i] : -a[0][i];
                det = det + factor * detLaplace(n - 1, aux);
            }
        }
        return det;
    }
}

//Generate cofactor array
float * matcafactor(int n, float a[n][n]){
    static float matCof[n][n];
    float aux[n - 1][n - 1];    
    float det = 0;
    int i, row, col, j_aux, i_aux;

    for(i = 0; i < n; i++){
        i_aux = 0;
        j_aux = 0;
            //Generate the array to calculate the cofactors
        for(row = 1; row < n; row++){
            for(col = 0; col < n; col++){
                if(col != i){
                    aux[i_aux][j_aux] = a[row][col];
                    j_aux++;
                }
            }
            i_aux++;
            j_aux = 0;
        }
           float factor = (i % 2 == 0)? a[0][i] : -a[0][i];
           matCof[i][n] = factor * detLaplace(n - 1, aux);
    }
    //Return the cofactor array https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSWQrcWYyqWK9wUGoUS9bnaGn5iCmj6CD9016BF0jopfcg4Xsme
    return matCof;
}

//Transpose the array
float * Transpose(int n, float array[n][n]){

    int Row=0;
    int Column=0;
    static float[n][n] matTramp;

    for(Row< D,Row++){
        for(Column< D,Column++){
            matTramp[Column,Row] = array[Row,Column];
        }
    }

    //Return the Transpose the array
    return matTramp;
}

float * Adjugate  (int n, float array[n][n]){
    static float matAux[n][n];

    matAux = matcafactor(n,array[n][n]);    
    matAux = Transpose(n,matAux[n][n]);

    //return the Transpose cafactors array 
    return matAux;
}

float * Invertible (int n, float array[n][n]){
    static float matAux[n][n];
    float detPrincipal=0;
    int i;
    int j;

    //Get the determinant of the main array detPrincipal= mainDertminant
    detPrincipal = detLaplace(n, array[n][n]);  

    matAux = Adjugate (n,array[n][n]);  

    for(i = 0, i<n,i++){
        for(j = 0, i<n,i++){
            matAux[n][n] = matAux[n][n]*(1/detPrincipal);
        }
    }

    return matAux;
}
5
  • remove the [100][100] after the identifier for the array - what you are doing with this line is indexing the 100th column of the 100th row of array, thus you are passing an argument of type float to Invertible, who expects float [][] as the second argument - the line should look like this: array = Invertible (o,array); Commented Jun 18, 2018 at 23:24
  • 2
    Expressions line return marAux; are returning float (*)[n], not float *. There is a difference, and being unfamiliar with that is related to the root problem you already identified ("don't know the language"). This can't possibly compile regardless, as the misplaced { after what should be the matcafactor prototype. Also, you can't have static variable length arrays in C, so that won't work either. In short, the problem are numerous, and there is no quick fix to this. Commented Jun 18, 2018 at 23:32
  • To add to what @WhozCraig accurately said, you're likely going to be best off passing the result array to the functions. You can't pass array (of size 100x100) to functions that are told, for example, that the size is 4 and the array is supposed to be array[n][n] (where I hypothesize that n == 4). You're brave if you actually type 10,000 numbers for a 100x100 array. Also, you read the values into v (after misprinting by including the & in &l and &c in the call to printf()), and never set the array. As was said, you have major surgery to do to create a compilable program. Commented Jun 19, 2018 at 4:21
  • You need to find a C text book and learn about how to handle matrices using C99 variable length arrays. They're great, but you can't use them as shown. Commented Jun 19, 2018 at 4:22
  • [Column,Row] ==> [Column][Row] Commented Jun 19, 2018 at 5:11

1 Answer 1

1

You have many errors in your program.

  1. int * main(int argc, char *argv[]) should be int main(int argc, char *argv[])

    Main should return integer not pointer to integer.

  2. printf("\nInsert Cell value[%i][%i]: ",&l,&c); should be printf("\nInsert Cell value[%i][%i]: ",l,c);

    l and c are int's. Therefore the printf should use the value directly.

  3. scanf("%d",&v); should be scanf("%f",&v);

    v is a float. For float's you should use %f for printf and scanf`

  4. array = Invertible (o,array[100][100]); to Invertible (o,array);

    You have two issues here. Firstly, in the assignment you are using array[100]100]. Secondly, You are assigning the result back to array.

    The type of Invertible is float * Invertible (int n, float array[n][n]); You cannot assign a float pointer to an array.

  5. int op = 0 --> Missing semicolon.

  6. printf("\nOperation numbers=%d |",&op); and printf("%d |",&array[l][c]); Remove the & and use %f for the array.

  7. static float matCof[n][n]; -> You need to decide the size of this array in advance.

There are many more errors, but you can get going with these.

You should compile your code with the -Wall option to get these errors.

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

3 Comments

Thanks I see some points,, but how I could create a dinamic array for my aplication if I need this to work for an array[100][100], array[9][9] or array[n][n]. I have no idea how I could correct the points 4 and 7.
For no 7. The fix is as shown. For no 10. Look at stackoverflow.com/questions/1970698/…
@Raphaeldemelo accept and upvote the answer if it helped you. That acknowledgement encourages people to contribute.

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.