Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I am not able to fix the error in my code: value subscripted is neither array nor pointer nor vector.

+2 votes
asked Mar 6, 2023 by Nirali Patel (750 points)
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Element {

    int value;

    int row;

    int column;

    struct Element *rowElement;

    struct Element *columnElement;

};

struct Header {

    int index;

    struct Header *header;

    struct Element *element;

};

struct Matrix {

    struct Header *headRowHeader;

    struct Header *headColumnHeader;

    int rows;

    int columns;

    int fillValue;

};

struct Matrix MTRX_alloc(char *filename, int fillValue){

}

void MTRX_free(struct Matrix* m){

}

int MTRX_getElement(struct Matrix *m, int row, int column){

}

void MTRX_write(struct Matrix *m, char *filename){

}

struct Matrix MTRX_add(struct Matrix *a, struct Matrix *b, int fillValue){

int m, n, p, q;

int i, j;

if(m == n){

for(i = 0; i < m; i++){

for(j = 0; j < n; j++){

a[i][j] = m[i][j] + n[i][j];

b[i][j] = m[i][j] + n[i][j];

printf("%d\t", m[i][j]);

printf("%d\t", n[i][j]);

}

}

for(i = 0; i < p; i++){

for(j = 0; j < q; j++){

a[i][j] = p[i][j] + q[i][j];

b[i][j] = p[i][j] + q[i][j];

printf("%d\t", a[i][j]);

printf("%d\t", b[i][j]);

}

}

fillValue = a[i][j];

fillValue = b[i][j];

}

else

printf("\n");

}

struct Matrix MTRX_multiply(struct Matrix *a, struct Matrix *b, int fillValue){

int m, n, p, q;

int i, j;

if (n == p){

for(i = 0; i < m; i++){

for(j = 0; j < n; j++){

a[i][j] += m[i][j] * n[i][j];

b[i][j] += n[i][j] * n[i][j];

}

}

for(i = 0; i < p; i++){

for(j = 0; j < q; j++){

a[i][j] += m[i][j] * n[i][j];

b[i][j] += q[i][j] * p[i][j];

}

}

fillValue=[m][n];

}

}

/*printing the product for two matrices */

for(i = 0; i < m; i++){

for(j = 0; j < n; j++){

printf("%d\t", a[i][j]);

printf("%d\t", b[i][j]);

printf("\n");

}

}

for(i = 0; i < m; i++){

for(j = 0; j < n; j++){

printf("%d\t", a[i][j]);

printf("%d\t", b[i][j]);

printf("\n");

}

}

}

struct Matrix MTRX_transpose(struct Matrix *m){

int n, p, q, a, b;

int i, j;

for(i = 0; i < m; i++){

for(j = 0; j < n; j++){

a[i][j] = b[i][j];

b[i][j] = a[i][j];

}

}

for(i = 0; i < p; i++){

for(j = 0; j < q; j++){

a[i][j] = b[i][j];

b[i][j] = a[i][j];

}

}

/* printing out the transpose operation by flipping the rows and columns */

for(i = 0; i < m; i++){

for(j = 0; j < n; j++){

printf("%d\t", b[i][j]);

printf("%d\t", a[i][j]);

printf("\n");

}

}

for(i = 0; i < p; i++){

for(j = 0; j < n; j++){

printf("%d\t", b[i][j]);

printf("%d\t", a[i][j]);

printf("\n");

}

}

return 0;

}

int main() {

    struct Matrix a, t;

    a = MTRX_alloc("matrixA.txt", 0);

    t = MTRX_transpose(&a);

    MTRX_write(&t, "matrixT.txt");

    MTRX_free(&t);

    MTRX_free(&a);

    return 0;

}

1 Answer

0 votes
answered Mar 6, 2023 by Peter Minarik (86,180 points)
edited Mar 8, 2023 by Peter Minarik

Compilation Error

The error message is rather self-explanatory. You're applying subscription (indexing) to a type, that is not an array-like type.

The compiler even references you the line where the problem happens:

main.c: In function ‘MTRX_add’:
main.c:45:5: error: subscripted value is neither array nor pointer nor vector
   45 | a[i][j] = m[i][j] + n[i][j];
      |     ^

If you look at your function's signature, you'll see the following:

struct Matrix MTRX_add(struct Matrix *a, struct Matrix *b, int fillValue)

The type of a is a pointer to a Matrix structure.

Your code indexes a (the pointer to Matrix), then tries to index the result of that (the Matrix itself), but that has no support for such an operation.

Logic Error

What you probably should have done instead is to make the Matrix type hold the dimensions of the matrix (number of columns and number of rows) and all the elements (two-dimensional array).

Let's forget about passing arguments to functions via pointers (to save performance) to reduce confusion. You can do it later when the rest of the code works).

So with this, you'd have something like the code below. I've done a proof of concept for the add operation (not complete as it's missing checks).

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

typedef struct
{
    int columnCount;
    int rowCount;
    float **elements;
} Matrix;

Matrix MatrixNew(int columnCount, int rowCount)
{
    Matrix matrix;
    matrix.columnCount = columnCount;
    matrix.rowCount = rowCount;
    matrix.elements = (float**)malloc(sizeof(float*) * columnCount);
    for (int column = 0; column < columnCount; column++)
        matrix.elements[column] = (float*)malloc(sizeof(float) * rowCount);
    
    return matrix;
}

Matrix MatrixAdd(Matrix a, Matrix b)
{
    // TODO: check if the two matrices have the same dimensions.
    Matrix newMatrix = MatrixNew(a.columnCount, a.rowCount);
    for (int column = 0; column < newMatrix.columnCount; column++)
        for (int row = 0; row < newMatrix.rowCount; row++)
            newMatrix.elements[column][row] = a.elements[column][row] + b.elements[column][row];

    return newMatrix;
}

void MatrixPrint(Matrix m)
{
    for (int column = 0; column < m.columnCount; column++)
    {
        for (int row = 0; row < m.rowCount; row++)
        {
            printf("% 10.3f", m.elements[column][row]);
        }
        printf("\n");
    }
}

int main()
{
    Matrix a = MatrixNew(2, 3);
    a.elements[0][0] = 1;
    a.elements[0][1] = 2;
    a.elements[0][2] = 3;
    a.elements[1][0] = 4;
    a.elements[1][1] = 5;
    a.elements[1][2] = 6;
    
    Matrix b = MatrixNew(2, 3);
    b.elements[0][0] = 10;
    b.elements[0][1] = 20;
    b.elements[0][2] = 30;
    b.elements[1][0] = 40;
    b.elements[1][1] = 50;
    b.elements[1][2] = 60;
    
    Matrix sum = MatrixAdd(a, b);
    MatrixPrint(sum);

    return 0;
}

What Is a Matrix?

What you tried to implement for matrix multiplication and transposition is not what mathematicians mean by such matrix operations.

Read how matrices work here.

Good luck!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...