Problem with matrix editing outside of main()

Asked

Viewed 65 times

0

So, guys, I’m in over my head with the code below. I started studying C pointers a few days ago and I believe it’s the solution to my problem, but I don’t know how to handle the code.

The problem is the following. In the function multiplicarMatrizes() I cannot access the matrix valuesC obviously because it was declared inside the main. But the problem is that I cannot declare it 'globally' out of main due to the fact that I need to read the number of rows/columns of each matrix at the beginning of the program (I cannot declare them in the code with a constant number of rows or columns).

Code:

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

            #define G 5

            void lerMatrizes(int matrizX[][G], int matrizY[][G]);

            void multiplicaMatrizes(int A[][G], int B[][G]);    

            int linhasA, colunasA, linhasB, colunasB;

            int main() {
                int i, j;

                printf("\nInforme a quantidade de linhas da matriz A : ");
                scanf("%d",&linhasA);

                printf("\nInforme a quantidade de colunas da matriz A : ");
                scanf("%d",&colunasA);

                printf("\nInforme a quantidade de linhas da matriz B : ");
                scanf("%d",&linhasB);

                printf("\nInforme a quantidade de colunas da matriz B : ");
                scanf("%d",&colunasB);

                int valoresA[linhasA][colunasA], valoresB[linhasB][colunasB], valoresC[colunasA][linhasB];

                lerMatrizes(valoresA, valoresB);

                if (colunasA == linhasB) {
                    multiplicaMatrizes(valoresA, valoresB);
                } else {
                    printf("\n\nNão é possivel multiplicar matrizes neste formato. O numero de colunasA tem que ser igual linhasB");
                }

                return 0;
            }


            void lerMatrizes(int matrizX[][G], int matrizY[][G]) {
                int i, j;

                // Gera Valores p Matriz1
                for(i=0; i<linhasA; i++) {
                    for(j=0; j<colunasA; j++) {
                        matrizX[i][j] = i+1 * j+1;
                    }
                }

                // Gera Valores p Matriz2
                for(i=0; i<linhasB; i++) {
                    for(j=0; j<colunasB; j++) {
                        matrizY[i][j] = i+1 * j+1;
                    }
                }   
            }

            void multiplicaMatrizes(int A[][G], int B[][G]) {
                int i, j, k, temp=0;

                for(i=0; i<colunasA; i++) {
                    for(j=0; j<linhasB; j++) {
                        valoresC[i][j]=0;

                        for(k=0; k<G; k++)
                            temp += A[i][k] * B[k][j];

                        valoresC[i][j] = temp;
                        temp=0;
                    }
                }

            }

I made the code using a constant number of rows/columns, then when I went to change it I stopped at this part there. I could not also pass the matrix by parameter, the solution would be a pointer?

  • What mistake or problem you had to pass the matrix valoresC as a parameter (in the same way as A and B)?

1 answer

0

I recommend allocating the matrices dynamically, and passing their pointers to the functions. Ex:

//aloca um array de ponteiros
int ** matriz = (int**)malloc(sizeof(int*) * nLinhas);
for (int i = 0; i != nLinhas; i++) {
    //alloca um array de inteiros em cada posição do array anterior
    matriz[i] = (int*)malloc(sizeof(int) * nColunas);
}

There the function multiplicaMatrizes(int A[][G], int B[][G]) could be multiplicaMatrizes(int** A, int** B, int** C);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.