0

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:

  • subscripted value is neither array nor pointer nor vector
  • subscripted value is neither array nor pointer nor vector
  • In function ‘inMatrix’: error: subscripted value is neither array nor pointer nor vector|
  • In function ‘inVector’:| error: subscripted value is neither array nor pointer nor vector|
  • In function ‘outVector’:| error: subscripted value is neither array nor pointer nor vector|
  • In function ‘main’:| error: incompatible type for argument 2 of ‘plu’|
  • note: expected ‘float (*)[(long unsigned int)(k)]’ but argument is of type ‘float’| error: incompatible type for argument 3 of ‘plu’|
  • expected ‘float *’ but argument is of type ‘float’|

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;
}
nikhil
  • 8,925
  • 21
  • 62
  • 102
Dabagab
  • 2,497
  • 4
  • 26
  • 31
  • You may want to explore other useful posts regarding arrays in C, such as this: http://stackoverflow.com/questions/4051/passing-multidimensional-arrays-as-function-arguments-in-c – makes Oct 18 '11 at 17:21
  • Read section 6 of the [comp.lang.c FAQ](http://c-faq.com). It does a *very* good job of explaining the relationship between arrays and pointers in C. – Keith Thompson Oct 18 '11 at 17:46

2 Answers2

0

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.

nikhil
  • 8,925
  • 21
  • 62
  • 102
  • You having a bad day or something? Why so rude to the OP? He said out right that he's a beginner, no need to belittle him for it. – jb. Oct 18 '11 at 17:18
  • Its ok, but please take time to understand the relation between pointers and arrays. – nikhil Oct 18 '11 at 17:19
  • Your code declares `inMatrix` to take a specific (hardcoded) matrix size, and passes in a VLA with a possibly different size. And the arbitrary size restriction is questionable as well. – interjay Oct 18 '11 at 17:20
  • @jb. I'm not being rude. I have no issue helping out beginners but people do need to understand the concepts before writing code and this is all too apparent with the code that has been asked. I'm just giving free advice its for the OP to follow up. – nikhil Oct 18 '11 at 17:21
  • @interjay : I could very well replace that with a double **inMatrix and allocate memory from the heap, but I don't think that the OP would understand that at all. I've tried to keep things simple. This by no means is perfect code. – nikhil Oct 18 '11 at 17:23
  • 1
    @nikhil: If by "not perfect" you mean "will cause a segfault" then you are correct. And there's no need for `double**` or heap allocations. The correct solution would be a VLA parameter. – interjay Oct 18 '11 at 17:26
0

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.

Akron
  • 1,413
  • 2
  • 13
  • 28
  • oh my goodness. OK, I understand the alloc now the simple C languange – Dabagab Oct 18 '11 at 17:16
  • I've programmed in php and a little python. And there martix is more understable – Dabagab Oct 18 '11 at 17:17
  • Also, I should have said double myArray[4][4] for the example and also told you to declare the parameter as double** since it is a 2-dimensional array. I apologize for the confusion. – Akron Oct 18 '11 at 17:53