2013-02-17 47 views
2

我已經編寫了矩陣乘法程序,但是我想對其進行修改。首先,我想將矩陣改爲first [a] [b]而不是10,並且從文件中讀取矩陣的維數。我是否需要根據使用malloc的矩陣的維度動態分配內存,或者我可以取一些最大值,但是會導致大量內存的浪費我是否需要從文件中存儲矩陣的維數。建議我需要哪些更改?我不打開文件,只是將stdin重定向到文件。我無法通過文件獲得輸入?C中的矩陣乘法與來自文件的輸入

修改後的代碼作爲

#include <stdio.h> 

int main() 
{ 
int m, n, p, q, c, d, k, sum = 0; 
    int **first, **second, **multiply; 
    printf("Enter the number of rows and columns of first matrix\n"); 
    scanf("%d%d", &m, &n); 
    first = malloc(m*sizeof(int*)); 
    for (int i =0;i <m; i++) 
first[i] =malloc(n*sizeof(int)); 
    second = malloc(p*sizeof(int*)); 
    for(int i=0;i<p;i++) 
second[i] = malloc(q*sizeof(int)); 
multiply = malloc(m*sizeof(int)); 
for (int i=0;i<q;i++) 
multiply[i] = malloc(q*sizeof(int)); 
printf("Enter the elements of first matrix\n"); 
    for ( c = 0 ; c < m ; c++) 
    for (d = 0 ; d < n ; d++) 
     scanf("%d", &first[c][d]); 
    printf("Enter the number of rows and columns of second matrix\n"); 
    scanf("%d%d", &p, &q); 
    if (n != p) 
    printf(
    "Matrices with entered orders can't be multiplied with each other.\n"); 
    else { 
printf("Enter the elements of second matrix\n"); 
for (c = 0 ; c < p ; c++) 
    for (d = 0 ; d < q ; d++) 
    scanf("%d", &second[c][d]); 
for (c = 0 ; c < m ; c++) { 
    for (d = 0 ; d < q ; d++) { 
    for (k = 0 ; k < p ; k++) { 
     sum = sum + first[c][k]*second[k][d]; 
    } 
    multiply[c][d] = sum; 
    sum = 0; 
    } 
    } 
printf("Product of entered matrices:-\n"); 
for (c = 0 ; c < m ; c++) { 
    for (d = 0 ; d < q ; d++) 
    printf("%d\t", multiply[c][d]); 
    printf("\n"); 
} 
for (int i = 0; i < p; i++) 
    free(second[i]); 
free(second); 
for (int i = 0; i < q; i++) 
    free(multiply[i]); 
    free(multiply); 
} 
for (int i = 0; i < m; i++) 
free(first[i]); 
free(first); 
return 0; 
} 
+1

不要惡意破壞給的答案都是沒有意義的程度你的問題。人們努力幫助你;你可能不會使這種援助毫無意義。 – 2013-02-17 20:52:57

回答

3

變化的聲明:

int i, m, n, p, q, c, d, k, sum = 0; 
int **first, **second, **multiply; 

scanf("%d%d", &m, &n);

first = malloc(m*sizeof(int*)); 
for (i = 0; i < m; i++) 
    first[i] = malloc(n*sizeof(int)); 

BEF礦石printf("Enter the elements of second matrix\n");

second = malloc(p*sizeof(int*)); 
for (i = 0; i < p; i++) 
    second[i] = malloc(q*sizeof(int)); 

multiply = malloc(m*sizeof(int*)); 
for (i = 0; i < q; i++) 
    multiply[i] = malloc(q*sizeof(int)); 

免費聲明:

更換(在程序結束時)(如果出口權後的程序總是必填項):

} 
    return 0; 
} 

附:

for (i = 0; i < p; i++) 
     free(second[i]); 
    free(second); 
    for (i = 0; i < q; i++) 
     free(multiply[i]); 
    free(multiply); 
    } 
    for (i = 0; i < m; i++) 
    free(first[i]); 
    free(first); 
    return 0; 
} 

的另一種方法:分配的存儲器中的連續塊

聲明:

int *first, *second, *multiply; 

malloc的:(無for循環)

  • first = malloc(m*n*sizeof(int));
  • second = malloc(p*q*sizeof(int));
  • multiply = malloc(m*q*sizeof(int));

用法:

  • 變化first[c][k]first[c*m+k]
  • 變化second[k][d]second[k*p+d]
  • 變化multiply[c][d]first[c*m+d]

free的:(無for循環)

  • free(first);
  • free(second);
  • free(multiply);
+0

+1:請問,您可以在哪裏添加免費()來完成您的示例? – Aubin 2013-02-17 18:13:08

+0

@Aubin編輯。 – Dukeling 2013-02-17 18:17:29

+0

更改代碼後出現以下錯誤 – Learner 2013-02-17 19:03:25