2

나는 숙제를 위해이 프로그램을 만들었고 실행하면 오류없이 충돌합니다.가변 길이 다차원 배열

또한 수정 한 후 코딩 방법의 효율성을 높이기위한 제안 사항에 만족합니다.

처음 전역 변수로 m, n, p, q를 선언했지만 함수에만 배열을 전달했지만 프로그램이 이상하게 동작했습니다.

그런 다음 모든 함수의 인수로 배열의 크기를 포함시키고 어디에서나 선언했습니다. CRASH

* VLA은 VLA의 배열 인덱스를 사용할 때 UB를하지 않도록하는 것이

//functions on matrices 
#include<stdio.h> 

int i, j; 
void getMatrix(int m, int n, int values[m][n]); 
void displayMatrix(int m, int n, int values[m][n]); 
void transposeMatrix(int m, int n, int values[m][n]); 
void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]); 
void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]); 

int main() 
{ 
    int m, n, p, q, A[m][n], B[p][q]; 

    printf("Enter the no. of Rows of the first Matrix : "); 
    scanf("%d", &m); 
    printf("Enter the no. of Columns of the first Matrix : "); 
    scanf("%d", &n); 

    printf("Enter the elements of the first matrix: \n"); 
    getMatrix(m, n, A); 
    printf("The entered Matrix:\n"); 
    displayMatrix(m, n, A); 
    printf("The transpose of the entered Matrix:\n"); 
    transposeMatrix(m, n, A); 

    printf("Enter the no. of Rows of the second Matrix : "); 
    scanf("%d", &p); 
    printf("Enter the no. of Columns of the second Matrix : "); 
    scanf("%d", &q); 

    printf("Enter the elements of the secong matrix: \n"); 
    getMatrix(p, q, B); 
    printf("The entered Matrix:\n"); 
    displayMatrix(p, q, B); 
    printf("The transpose of the entered Matrix:\n"); 
    transposeMatrix(p, q, B); 

    printf("Addition of the Matrices:\n"); 
    addMatrices(m, n, p, q, A, B); 
    printf("Multiplication of the Matrices:\n"); 
    multiplyMatrices(m, n, p, q, A, B); 

    return 0; 
} 

void getMatrix(int m, int n, int values[m][n]) 
{ 
    for(i = 0; i < m; ++i) 
    for(j = 0; j < n; ++j) 
     scanf("%d", &values[i][j]); 
} 

void displayMatrix(int m, int n, int values[m][n]) 
{ 
    for(i = 0; i < m; ++i) 
    { 
    for(j = 0; j < n; ++j) 
     printf("%3d ", values[i][j]); 

    printf("\n"); 
    } 
} 

void transposeMatrix(int m, int n, int values[m][n]) 
{ 
    int transpose[n][m]; 

    for(i = 0; i < n; ++i) 
    for(j =0; j < m; ++j) 
     transpose[i][j] = values[j][i]; 

    displayMatrix(n, m, transpose); 
} 

void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]) 
{ 
    int C[m][n]; 

    if(m == p && n == q) 
    { 
    for(i = 0; i < m; ++i) 
    for(j = 0; j < n; ++j) 
     C[i][j] = A[i][j] + B[i][j]; 

    displayMatrix(m, n, C); 
    } 
    else 
    printf("Cannot add these Matrices!\n"); 
} 

void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]) 
{ 
    int C[m][q], k, sum = 0; 
    if(n == p) 
    { 
    for(i = 0; i < m; ++i) 
     for(j = 0; j < q; ++j) 
     { 
     for(k = 0; k < n; ++k) 
      sum += A[i][j] * B[j][i]; 

     C[i][j] = sum; 
     sum = 0; 
     } 

    displayMatrix(m, q, C); 
    } 
    else 
    printf("Cannot multiply these Matrices!\n"); 
} 
+0

@BLUEPIXY : 당신은 너무 빠르다 ... 그러나 나는 동시에 해당 소스 코드가 같은 라인'INT의 M, N, P, Q에 컴파일 어떻게 – coderredoc

+0

에 거의 대답, A [m] [n], B [p] [q]; 경고도없이? –

답변

5

Initiliazie mn을 지원했다. 곱셈 행렬

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

에서

Unsed 루프는 당신이 필요하지 않는

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

전역 변수를 사용하지 마십시오 될 것입니다. for 루프 루프의 인덱스 변수를 만드는 것이 좋습니다.

for(int i=0;i<n;i++) 
... 
+0

하지만 같은 함수 내에서 많은 for 루프를 사용해야한다면? 여전히 색인을 로컬로 만드는 연습을해야합니까? –

+0

이러한 변수에 전역 변수를 사용하는 것은 좋지 않습니다 ... 그렇지만 당연히 추적 할 수는 있지만 추적하기는 어렵습니다. – coderredoc

+0

당신은 매우 빨리 대답합니다! 정말 감사합니다... –