2013-02-17 82 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