Problem with array editing outside main ()

0

So, guys, I'm in extreme doubt with the code below. I started to study pointers in C a few days ago and I believe it is the solution to my problem, but I do not know how to handle the code.

The problem is as follows. In the multiplyMatters () function I can not access the array C values obviously because it was declared inside the main. But the problem is that I can not declare it 'globally' off the main because I need to read the number of rows / columns of each array at the start of the program (I can not 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 was changing it I caught this part there. I could not also pass the array by parameter, would the solution be a pointer?

    
asked by anonymous 07.03.2016 / 07:12

1 answer

0

I recommend allocating arrays dynamically, and passing their pointers to 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);
}

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

    
07.03.2016 / 07:46