2013-09-21 154 views
1

那麼,我正在做矩陣乘法,我需要做一個m x n陣列和p x q陣列。
但是,我不知道如何去做。如何在C中聲明一個可變大小的數組?

這是我的節目,當我在值手動送入它打印正確的輸出:

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */ 

int main(int argc, char *argv[]) { 
    /* 
     Rows and columns for matrices. 
    */ 
    int m , n; // rows and columns of the first matrix 
    int p , q; // rows and columns of the second matrix 

    /* 
     1st matrix is a 2x3 matrix 
    */ 
    m = 2; 
    n = 3; 

    /* 
     2nd matrix is a 3x2 matrix 
    */ 
    p = 3; 
    q = 2; 


    /* 
     Create the matrices. 
     Give them values. 
    */ 
    int matrix1[m][n] = { 
          {2,3,4}, 
          {5,6,7} 
         }; 
    int matrix2[p][q] = { 
          {1,7}, 
          {3,9}, 
          {5,11} 
         }; 

    /* 
     Check if we can multiple the matrices. 
     For matrix multiplication, 
     the number of COLUMNS of FIRST matrix must be equal to 
     the number of ROWS of SECOND matrix 
    */ 
    if(n==p){ 
     /* 
      Create a new matrix. 
      The resulting matrix will have M rows and Q columns. 
      That is, the matrix is a MxQ matrix. 
     */ 
     int matrix3[2][2]; 

     /* 
      We need three loops so we have 3 variables. 
     */ 
     int i = 0; // iterates over matrix1 rows 
     int j = 0; // iterates over matrix1 columns 
     int k = 0; // iterates over matrix2 rows 
     int l = 0; // iterates over matrix2 columns 


     while(i < m){ 
      l = 0; 
      while(l < q){ 
       int element = 0; 
       while(j < n && k < p){ 
        element += matrix1[i][j] * matrix2[k][l]; 
        matrix3[i][l] = element; 
        j++; 
        k++; 
       } 
       printf("\t%d",element); 
       l++; 
       j = 0; 
       k = 0; 
      } 
      printf("\n"); 
      i++; 
     } 

    }else{ 
     printf("Matrices can not be multiplied"); 
    } 
} 

矩陣聲明被標記爲錯誤。 我該如何解決?

+0

您的編譯器*支持* VLA嗎?並檢查你的錯誤。即使支持,VLA也不支持初始化程序。 – WhozCraig

+0

@WhozCraig什麼? : -/VLA =可變長度數組。我使用Dev-C++,它使用GCC V 4.71 –

+2

這個代碼中甚至沒有*需要*用於VLA。矩陣可以是常量大小的。 – WhozCraig

回答

3

如何解決這個問題呢?

首先,不使用VLA。你不需要這個特定任務的VLA。

至於什麼是實際問題:變長數組無法初始化。您必須逐個分配元素或使用一些質量「分配」技術,如memcpy()

+0

你好,碳酸。我明白了。我不能使用VLA :( –

+0

@LittleChild嗯,我說的是無論你是否可以 - 你根本就不需要它們。把你的數組聲明爲int mat [2] [3 ]'和繁榮,你可以突然初始化它 – 2013-09-21 19:50:42

+0

我知道我可以,如果我這樣做,我也可以用宏來做到這一點,但是同樣在自己的價值中打孔:) –

1

是否可以使用C99

Variable Length Arrays

這是100%的C99有效:

int m = 2; 
    int n = 3; 

    int matrix1[m][n]; 
+0

我不知道。數據結構在C中被教授在Uni中,所以我的知識僅限於我需要編寫的基本程序:) –

+0

C99是C的最後一個版本,它支持VLA。我相信你應該問@LittleChild。用C99描述鏈接更新我的答案。 –

+3

「這是C99中的100%有效」 - 不,它不是,無法初始化VLA。只有靜態大小的數組才能被初始化。 – 2013-09-21 19:54:14

1

根據當前的c標準,「本機」可變長度數組是一個可選的語言結構。您必須檢查您的編譯器文檔以確定您使用的編譯器是否支持可變長度數組。

在C99之前,變長數組需要在堆上顯式分配並通過指針訪問。對於矩陣問題你有兩個選擇。

首先是分配一個有足夠存儲空間的數組,並在需要讀取或修改矩陣元素時計算正確的索引。例如:

int* matrix_storage = malloc(sizeof(int) * matrix_width * matrix_height); 
// set row 0, column 1 to 0 
matrix_storage[ (0 * matrix_width) + (1 % matrix_width) ] = 0; 

或者,也可以分配指針數組到每一行或列:

int** rows = malloc(sizeof((int*)) * matrix_height); 
for(int i = 0; i < matrix_height; ++i) 
{ 
    rows[i] = malloc(sizeof(int) * matrix_width); 
} 
row[0][1] = 0; // set row 0, column 1 to 0 

此實現浪費一些存儲器,無論選擇的執行考慮使用功能和struct關鍵字從驅動程序代碼(實際上是變異或讀取矩陣的代碼)隱藏實現。

一些注意事項:我已經使用c99 for循環語法,而較老的編譯器int i變量將需要在for循環之外聲明。另外請注意,如果您的矩陣大小可以在編譯時確定(IE不依賴用戶輸入),則不需要malloc的開銷,並且可以硬編碼您的數組大小。您發佈的示例代碼不需要動態分配。

相關問題