Pesquisar na Comunidade
Mostrando resultados para as tags ''passagem de matriz por argumento''.
Encontrado 1 registro
-
Qual o problema deste código? Ele simplesmente crasha do nada.
uma questão postou Tallahassee C, C++
#include <stdio.h> #include <stdlib.h> #include <locale.h> int matrixSum(int ** mA, int ** mB, int ** mC, int la, int lb, int ca, int cb); int main(){ int ** matrixA, ** matrixB, ** matrixC, lineA, columnA, lineB, columnB; int i, j; printf("Insert the numbers of lines desired for matrix A:\n>>>\n"); scanf("%d", &lineA); fflush(stdin); printf("Insert the numbers of columns desired for matrix A:\n>>>\n"); scanf("%d", &columnA); fflush(stdin); printf("Insert the numbers of lines desired for matrix B:\n>>>\n"); scanf("%d", &lineB); fflush(stdin); printf("Insert the numbers of columns desired for matrix B:\n>>>\n"); scanf("%d", &columnB); fflush(stdin); matrixA = malloc(lineA * sizeof(int *)); if(matrixA == NULL){ printf("ERROR! There isn't space on disc!\n"); return 1; } for(i=0; i<columnA; i++){ matrixA = malloc(sizeof(int)); if(matrixA == NULL){ printf("ERROR! There isn't space on disc!\n"); return 1; } } matrixB = malloc(lineB * sizeof(int *)); if(matrixB == NULL){ printf("ERROR! There isn't space on disc!\n"); return 1; } for(i=0; i<columnB; i++){ matrixB = malloc(sizeof(int)); if(matrixB == NULL){ printf("ERROR! There isn't space on disc!\n"); return 1; } } for(i=0; i<lineA; i++){ for(j=0; j<columnA; j++){ printf("\nMatrix A - Position [%d][%d]: ", i+1, j+1); scanf("%d", &matrixA[j]); fflush(stdin); } } for(i=0; i<lineB; i++){ for(j=0; j<columnB; j++){ printf("\nMatrix B - Position [%d][%d]: ", i+1, j+1); scanf("%d", &matrixB[j]); fflush(stdin); } } int ans; ans = matrixSum(matrixA, matrixB, matrixC, lineA, lineB, columnA, columnB); if(ans == 0){ printf("\nIt is impossible sum the arrays because the number of rows and columns are different!\n"); return 0; } else{ for(i=0; i<lineA; i++){ for(j=0; j<columnA; j++){ printf("\nMatrix C - Position[%d][%d] = %d\n", i, j, matrixC[j]); } } return 0; } } int matrixSum(int ** mA, int ** mB, int ** mC, int la, int lb, int ca, int cb){ int i, j; if(la != lb || ca != cb){ return 0; } else{ //Como as linhas e colunas precisam ser iguais, pode-se usar tanto la e ca, como lb e cb mC = malloc(la * sizeof(int *)); if(mC == NULL){ printf("ERROR! There is no space on disc!\n"); } for(i=0; i<ca; i++){ mC = malloc(sizeof(int)); if(mC == NULL){ printf("ERROR! There is no space on disc!\n"); } } for(i=0; i<lb; i++){ for(j=0; j<cb; j++){ mC[j] = mA[j] + mB[j]; } } } return 1; }