Home > Back-end >  taking input in a matrix of variable size in c
taking input in a matrix of variable size in c

Time:05-01

I am trying to check the orthogonality of a matrix A in c . For that I am declaring a "MAX" value of number of rows and columns, then I am trying to get the number of rows and columns for the matrix, hence declaring it. After that I am trying to get the input for the matrix elements by using pointer to the rows of the matrix and so on ..

#include <stdio.h>
#define MAX 10
int arows,acolumns; /*line 4*/
void input_matix(float (*arr)[MAX],int arows,int acolumns)
{/*asks user for input of individual elements*/}

void transpose(float (*T)[MAX],float (*A)[MAX],int arows,int acolumns)
{/*gets me the transpose*/}
void product(float (*A)[MAX],float (*T)[MAX],float (*P)[MAX])
{/*multiplies two matrices and places the product in P matrix*/}

int orthogonal_array(float (*arr)[MAX]){/*returns 1 if arr is ortho*/}
int main()
{
    //int arows,acolumns;
    printf("Enter the no. of rows and columns of matrix A: \n");
    scanf("%d %d",&arows,&acolumns);
    float A[arows][acolumns];

    if (arows != acolumns)
    {
        printf("Not a orthogonal matrix.\n");
    }
    else
    {
        input_matix(A,arows,acolumns);
        orthogonal_array(A);
    }
}

When compiling it I am getting the error

cannot convert 'float (*)[acolumns] to float (*)[10]' 

I have tried replacing line 4 with

extern int arows,acolumns;

and replacing float (*arr)[MAX] by float arr[][acolumns] similarly other parameters in the functions but then I get the error that

"expression must have a constant value"
"array bound is not a integer constant before "]" token */

Please suggest me what should I do, so as to make these functions work properly

CodePudding user response:

The root cause is that in C VLA (Varibale Length Arrays) are not allowed. It is not a part of the language C so far.

float A[arows][acolumns];

is invalid C code.

In C you can do, but not in C

CodePudding user response:

In C, you can't create double index array or change its size during the program. So yes, you have to tell the programme what's the size of A before compiling.

You can create this array with its max size and send "arows" and "acolumns" with it in your functions to work only on "used space" instead.

  • Related