c - 使用文件输入的 C 矩阵乘法

标签 c matrix-multiplication

我已经编写了一个矩阵乘法程序,但我想修改它。首先,我想将矩阵更改为first[a][b]而不是10,并从文件中读取矩阵的维度。我是否需要使用malloc根据矩阵的维度动态分配内存,或者我可以采取一些最大值,但是那会导致大量内存的浪费 我需要将矩阵的维度从文件存储在数组中吗?建议我需要做什么改变?我没有打开文件,只是将标准输入重定向到文件。我无法通过文件获取输入??

修改后的代码如下

#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;
 }

最佳答案

更改声明:

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));

printf("输入第二个矩阵的元素\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));

自由语句:(在程序结束时)(如果程序没有立即退出,则始终需要) p>

替换:

  }
  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));
  • 第二个 = malloc(p*q*sizeof(int));
  • 乘法 = malloc(m*q*sizeof(int));

用法:

  • first[c][k] 更改为 first[c*m+k]
  • 秒[k][d]更改为秒[k*p+d]
  • multiply[c][d] 更改为 first[c*m+d]

免费的:(无for循环)

  • 免费(第一);
  • 免费(第二);
  • 自由(乘法);

关于c - 使用文件输入的 C 矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14924229/

相关文章:

java - 线程数组 Java 矩阵

无法通过 strcpy 从参数复制到 char 数组

c - 如何在 C 中比较两个以上的字符串?

c - 具有相同变量的两个结构

c++ - 乘以存储在一维数组算法中的上三角矩阵

Python ijk 乘法,为什么我们使用下面的赋值符号或代码行?

c - 在没有新 malloc 的情况下从现有字符串动态构造一个数组

c - 范围之间的随机奇数,C 中单个数字除外

python - Python 中的 XOR 矩阵乘法

c++ - GSL-GNU 中的矩阵乘法