Dabagab
Dabagab

Reputation: 2855

compiler errors with multi dimensional arrays in c

This is the first time, when I'm using C. I have to do my numeric mathematics homework in C.

So I have problem using multi dimensional arrays. I don't now why I'm getting the following errors:

For example: I don't know why it is complaining about the 'float *' when I didn't use a float pointer.Searching on google didn't return any results, so I don't know what the error is. I don't understand this error 'subscripted value is neither array nor pointer nor vector'.

What can I do? How can I rewrite my source code to get rid of these compiler errors?

Sorry for my poor English.

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

void inMatrix(double matrix, int n)
     {
     int j, i;
        for (i = 0; i < n; i++)
        {
            for (j= 0; j < n; j++)
            {
               scanf("%lf", &matrix[i][j]);
            }
        }
     }

void inVector(double vektor, int n)
     {
     int k;
        for (k = 0; k < n; k++)
        {
            scanf("%lf", &vektor[k]);
        }
     }

void outVector(double vektor, int n)
     {
     int k;
        for (k = 0; k < n; k++)
        {
            printf("%.8lf", vektor[k]);
        }
     }

int plu(int k, float A[k][k], float b[k], float x[k])
    {
        int P[k];
        int n, m, p, i, j;
        int d=0;
        int r=0;
        int T=0;
        float t=0;
        float y[k];
        //seged vektorok
        for(n=1; n<k;n++)
        {
            x[n]=0;
            y[n]=0;
        }
        // Permutaciós matrix
        // permutation
        for(i=1; i<k; i++)
        {
            P[i]=i;
        }
        // Rendezes
        // sorting
        for(d=1; d<k-1;d++)
        {
            p=0;
            for(i=d; i<k; i++)
                {
                    t=A[i][d];
                    if(t<0)
                        t=-1*t;
                    if(t>p)
                    {
                        p=t;
                        r=i;
                    }
                }
                //Ha szingularis
                //If singular
                if(p==0)
                {
                   // printf("szingularis");
                    return 1;
                }
                //Matrix Csere
                //Matrix change
                T=P[r];
                P[r]=P[d];
                P[d]=T;

                //matrix elem csere
                //matrix value change
                for(i=1; i<k; i++)
                {
                    t=A[r][i];
                    A[r][i]=A[d][i];
                    A[d][i]=t;
                }
                for(i=d+1;i<k;i++)
                {
                    A[i][d]=A[i][d]/A[d][d];
                    for(j=d+1; j<k; j++)
                    {
                        A[i][j]=A[i][j]-A[i][d]*A[d][j];
                    }
                }
            }
            // Megoldas Vektorra
            // Solve for Vector
            for(n=1; n<k;n++)
            {
                t=0;
                for(m=1;m<=n-1;m++)
                {
                    t+=A[n][m]*y[m];
                }
                y[n]=b[P[n]]-t;
            }
            for(n=k-1;n>=1;n--)
            {
                t=0;
                for(m=n+1; m<k;m++)
                {
                    t+=A[n][m]*x[m];
                }
                x[n]=(y[n]-t)/A[n][n];
            }
        return 0;
    }

int main()
{
    //int i,j,k, num,value;
    // d as numbers of dimmension
    int d;
    // Read dimension of array
    scanf("%d", &d);

    float matrix[d][d];
    float vector[d];

    inMatrix(matrix[d][d], d);
    inVector(vector[d], d);

    float resVector[d];

    if(plu(d,matrix[d][d],vector[d], resVector[d])==1)
    {
        printf("szingularis");
    }
    else
    {
        outVector(resVector[d], d);
    }

    return 0;
}

Upvotes: 0

Views: 798

Answers (2)

nikhil
nikhil

Reputation: 9395

Here, I've fixed the compiler errors for you. I won't bother to explain in detail the errors as you clearly need to read a good book first.

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

void inMatrix(int n,double matrix[n][n])
     {
     int j, i;
        for (i = 0; i < n; i++)
        {
            for (j= 0; j < n; j++)
            {
               scanf("%lf", &matrix[i][j]);
            }
        }
     }

void inVector(double vektor[], int n)
     {
     int k;
        for (k = 0; k < n; k++)
        {
            scanf("%lf", &vektor[k]);
        }
     }

void outVector(double vektor[], int n)
     {
     int k;
        for (k = 0; k < n; k++)
        {
            printf("%.8lf", vektor[k]);
        }
     }

int plu(int k, double A[k][k], double b[k], double x[k])
    {
        int P[k];
        int n, m, p, i, j;
        int d=0;
        int r=0;
        int T=0;
        float t=0;
        float y[k];
        //seged vektorok
        for(n=1; n<k;n++)
        {
            x[n]=0;
            y[n]=0;
        }
        // Permutaciós matrix
        // permutation
        for(i=1; i<k; i++)
        {
            P[i]=i;
        }
        // Rendezes
        // sorting
        for(d=1; d<k-1;d++)
        {
            p=0;
            for(i=d; i<k; i++)
                {
                    t=A[i][d];
                    if(t<0)
                        t=-1*t;
                    if(t>p)
                    {
                        p=t;
                        r=i;
                    }
                }
                //Ha szingularis
                //If singular
                if(p==0)
                {
                   // printf("szingularis");
                    return 1;
                }
                //Matrix Csere
                //Matrix change
                T=P[r];
                P[r]=P[d];
                P[d]=T;

                //matrix elem csere
                //matrix value change
                for(i=1; i<k; i++)
                {
                    t=A[r][i];
                    A[r][i]=A[d][i];
                    A[d][i]=t;
                }
                for(i=d+1;i<k;i++)
                {
                    A[i][d]=A[i][d]/A[d][d];
                    for(j=d+1; j<k; j++)
                    {
                        A[i][j]=A[i][j]-A[i][d]*A[d][j];
                    }
                }
            }
            // Megoldas Vektorra
            // Solve for Vector
            for(n=1; n<k;n++)
            {
                t=0;
                for(m=1;m<=n-1;m++)
                {
                    t+=A[n][m]*y[m];
                }
                y[n]=b[P[n]]-t;
            }
            for(n=k-1;n>=1;n--)
            {
                t=0;
                for(m=n+1; m<k;m++)
                {
                    t+=A[n][m]*x[m];
                }
                x[n]=(y[n]-t)/A[n][n];
            }
        return 0;
    }

int main()
{
    //int i,j,k, num,value;
    // d as numbers of dimmension
    int d;
    // Read dimension of array
    scanf("%d", &d);

    double matrix[d][d];
    double vector[d];

    inMatrix(d,matrix);
    inVector(vector, d);

    double resVector[d];

    if(plu(d,matrix,vector, resVector)==1)
    {
        printf("szingularis");
    }
    else
    {
        outVector(resVector, d);
    }

    return 0;
}

Your errors show that you don't understand the basics of C at all. Take some time to review your concepts.

Upvotes: 0

Akron
Akron

Reputation: 1533

Well, I'm not totally clear on what your assignment was but lets look at the first function

I see you are trying to iterate through a square matrix of size n and read double-values from stdin into the matrix. The input parameter for this should not be (double, int). It should be (double*, int). A double* is an array of doubles. If you declare this function with just a double (like you did), then the program will allocate enough space for one double on the stack and your for-loop will be writing on important memory (and by that I mean it will most likely just crash) for array sizes bigger than one. Also, declaring it this way means the memory is found on the stack for this function call. This means that once the function is over, your array values you set are lost forever.

Instead, you need to declare it like:

void inMatrix(double* matrix, int n)

and pass in an array when you call it.

double myArray[16];

inMatrix(myArray, 4);

I hope this is helpful. Im not sure how much you understand about pointers/arrays.

Upvotes: 0

Related Questions