I'm working on a matrix addition/multiplication/transpose/inverse program and after entering the values of matrix A, I don't know how to call these values.

int  setMatrixA(int r1, int c1, int i, int j){
        printf(" Enter the number of rows:\n");
            scanf("%d", &r1);
        printf(" Enter the number of columns:\n");
            scanf("%d", &c1);
                
        for(i=0; i<r1; i++) {
                for(j=0; j<c1; j++) {
                printf("Please enter A[%d][%d]\n", i, j);
                scanf("%d",&A[i][j]);
                }
        }
        
        printf("A= ");
            
        for(i=0;i<r1;i++)
            {
                for(j=0;j<c1;j++)
                    printf("\t%d",A[i][j]);
                printf("\n");
            }
                
        return A[i][j];
        }

I can only think of return A[j] and that doesn't work.

where is the array A declared ?

>>I don't know how to call these values.
You don't call values -- you call functions. So I don't really know what you are asking here.

>>I can only think of return A[j] and that doesn't work
why not? what are the some of the errors your compiler spits out ?

do you mean you don't know how (or where) to declare the variable, "A"?

to make a long story short, for now you can just try declaring it as a global variable (outside the "main( )" routine) like so:

int A[MAX_ROWS][MAX_COLS];

where MAX_ROWS and MAX_COLS can be #define'd or simply hard coded as a direct number ("int A[30][30]" for example...). of course if your user tries to define a matrix with more rows/columns than you've allowed, the program will crash.

whenever you want to return an entire array (matrix), you would just

return A;

but in this case, you won't actually return it. because the variable is being held globally so theres no need to return it. its values will just "be there" . in this case you would just "return 0". You could also conditionally return a non-zero number to differentiate whether or not the function "passed" or "failed", if you want to get fancy...

but another problem this exposes, is that you don't want to pass those row and column variables to the function from the caller. just declare them locally, inside the SetMatrixA function itself. so now the function call will look like this

int SetMatrixA(void) {
     int j, k, r1, c1;

     <rest of function>

declaring the matrix "A" as a global variable is not the best way to go about this, but the way it should be done involves pointers, and we can save that for another time. this will at least get you started.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.