2017-02-22 5 views
-1

割り当ては、マトリックスを追加するコードを完成させるように私に要求して、私は何を返すと思いますか。それはランタイムエラー があると私に伝えますが、私はそれを修正する方法を知らない。誰かが私を助けてください、それはかなりすぐに予定されています!Cで行列コードを追加するHELP!私は何が間違っているのか分からない

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

int** getMatrix(int n, int m); 
int** allocateMatrix(int n, int m); 
int** addMatrices(int** A, int** B, int n, int m); 
void printMatrix(int** A, int n, int m); 
void deallocateMatrix(int** A, int n); 


// This program reads in two n by m matrices A and B and 
// prints their sum C = A + B 
// 
// This function is complete, you do not need to modify it 
// for your homework 
int main() { 
    int n = 0, m = 0; 
    printf("Enter the number of rows and columns: "); 
    scanf("%d %d", &n, &m); 
    assert(n > 0 && m > 0); 
    printf("Enter matrix A:\n"); 
    int** A = getMatrix(n, m); 
    printf("Enter matrix B:\n"); 
    int** B = getMatrix(n, m); 
    int** C = addMatrices(A, B, n, m); 
    printf("A + B = \n"); 
    printMatrix(C, n, m); 
    deallocateMatrix(A, n); 
    deallocateMatrix(B, n); 
    deallocateMatrix(C, n); 
} 

// Creates a new n by m matrix whose elements are read from stdin 
// 
// This function is complete, you do not need to modify it 
// for your homework 
int** getMatrix(int n, int m) { 
int** M = allocateMatrix(n, m); 
int i, j; 

for (i = 0; i < n; i++) { 
    printf("Input row %d elements, separated by spaces: ", i); 
    for (j = 0; j < m; j++) { 
     scanf("%d", &M[i][j]); 
    } 
} 
return M; 
} 

// Allocates space for an n by m matrix of ints 
// and returns the result 
int** allocateMatrix(int n, int m) { 
// Homework TODO: Implement this function 
int i, j; 
int** L; 

L = (int**)malloc(sizeof(int) * n); 
for(i = 0; i < n; i++) { 
    for(j = 0; j < m; j++){ 
     L[i] = (int*) malloc(sizeof(int) * m); 
    } 
} 
} 

// Adds two matrices together and returns the result 
int** addMatrices(int** A, int** B, int n, int m) { 
// Homework TODO: Implement this function 
int j, i; 
int** C; 

for(i = 0; i < n; i++) { 
    for(j = 0; j < m; j++) { 
     C[i][j] = A[i][j] + B[i][j]; 
    } 
} 

} 

// Prints out the entries of the matrix 
void printMatrix(int** A, int n, int m) { 
// Homework TODO: Implement this function 
int i, j; 
int** C; 

for (i = 0; i < n; i++) { 
    for (j = 0 ; j < m; j++) { 
    printf("%d\t", C[i][j]); 
    } 
    printf("\n"); 
    } 
} 

// Deallocates space used by the matrix 
void deallocateMatrix(int** A, int n) { 
int i; 

for (i = 0; i < n; i++) { 
    free(A[i]); 
} 
free(A); 
} 
+1

あなたの問題には関係ありませんが、[read this discussion](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) malloc'。 –

+0

申し訳ありません。ありがとうございました! – Sara

答えて

1

いくつかのこと:allocateMatrix

  • あなたはn整数、ないポインタ整数にのためのスペースを割り当てます。

  • 多くの機能では、初期化されていないポインタCを使用します。

  • 何かを返すと宣言された関数の多くは、何も返さない。

これらの事、そしておそらく他の人は、未定義の動作につながります。

+0

私は最近、スペースを割り当てる方法を学びました。私はmallocを使うべきかを本当に理解していません。また、私はそれらを "int ** C;"で初期化していませんか? – Sara

+0

私は次のように返すべきですか? "return C **;"?と "リターンL **;"? – Sara

+0

@Saraポインタは、それが有効であるためにどこかを指さなければなりません。それを初期化したり、割り当てたりする必要があります。そして、はい、あなたはすべきです。あなたの 'allocateMatrix'関数で' return L; 'を返します。 –

0

あなたはn整数ポインタ(int* S)のための部屋を割り当て、各列に、m整数(int S)のためのメモリを割り当てなければならない@Someprogrammerdude's answer

  • のビットを拡張。だから、私はthey aren't recommendedので、キャストを削除した

    // Allocates space for an n by m matrix of ints 
    // and returns the result 
    int** allocateMatrix(int n, int m) { 
        int i, j; 
        int** L; 
    
        L = malloc(sizeof(int*) * n);  /* Make `n` rows */ 
        for(i = 0; i < n; i++) {   /* For each row, */ 
         L[i] = malloc(sizeof(int) * m); /* Make room for `m` ints */ 
        } 
    
        return L; /* Return the allocated variable. See 3rd point ↓ */ 
    } 
    

    使用しています。私はあなたにエラーmallocを残しておきます。

  • C未初期化:int** C;を使用します。あなたはABのためにやったようにメモリを割り当てる:

    int** C = allocateMatrix(n, m); 
    
  • あなたが関数から変数を返すのを忘れていました。関数allocateMatrixaddMatricesの最後にそれぞれreturn L;return C;を追加します。

関連する問題