对于这个练习(我们必须扫描 txt 文件并返回行、列、矩阵 1 和矩阵 2;然后求矩阵 1 和 2 的和),“使用动态内存分配”有额外的学分,即前面几章。鉴于下面的程序,我将如何去做呢?我知道动态内存分配的4个函数(malloc、calloc、free和realloc),但还不知道如何使用它们。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *inFile;
int i,j;
int rows, cols;
inFile = fopen ("Matrix.txt", "r");
if (inFile == NULL)
{
printf("\n\nFile does not exist or cannot be opened.\n");
exit(1);
}
fscanf(inFile,"%d %d", &rows, &cols);
int m1[rows][cols],m2[rows][cols],m3[rows][cols];
/*Scan and store Matrix 1 and Matrix 2*/
for (i = 0; i < rows; i++)
{
for ( j = 0;j<cols;j++)
{
fscanf(inFile,"%d", &m1[i][j]);
}
}
for (i = 0; i < rows; i++)
{
for ( j = 0;j<cols;j++)
{
fscanf(inFile,"%d", &m2[i][j]);
}
}
fclose(inFile);
printf("\nThe sum of the two matrices:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
printf(" %d", m3[i][j]);
}
printf("\n");
}
}
最佳答案
我没有您的“Matrix.txt”文件,因此我无法调试我编写的代码,但无论如何它都在这里。
两个答案合二为一。惰性方式和二维数组方式。 在大多数情况下,我倾向于将 2D 数组作为 1D 数组来处理,因为它的代码更少,需要跟踪的东西也更少。
为了测试它,只需注释或取消注释#define
。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define IF_LAZY (1) // comment/uncomment this line if you like lazy solutions or not.
int main() {
FILE *inFile;
int i, j;
int rows, cols;
inFile = fopen("Matrix.txt", "r");
if (inFile == NULL) {
printf("\n\nFile does not exist or cannot be opened.\n");
exit(1);
}
fscanf(inFile, "%d %d", &rows, &cols);
#ifdef IF_LAZY
/* Dynamic allocation using 1D arrays. */
int* m1 = (int*) malloc(sizeof(int) * rows * cols);
int* m2 = (int*) malloc(sizeof(int) * rows * cols);
int* m3 = (int*) malloc(sizeof(int) * rows * cols);
#else
/* Dynamic allocation using 2D arrays made out of pointers to pointers. */
int** m1 = (int**) malloc(sizeof(int*) * rows);
for(int i = 0; i < rows; i++) {
m1[i] = (int*) malloc(sizeof(int) * cols);
}
int** m2 = (int**) malloc(sizeof(int*) * rows);
for(int i = 0; i < rows; i++) {
m2[i] = (int*) malloc(sizeof(int) * cols);
}
int** m3 = (int**) malloc(sizeof(int*) * rows);
for(int i = 0; i < rows; i++) {
m3[i] = (int*) malloc(sizeof(int) * cols);
}
#endif
#ifdef IF_LAZY
/*Scan and store Matrix 1 and Matrix 2*/
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
fscanf(inFile, "%d", &m1[i * cols + j]);
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
fscanf(inFile, "%d", &m2[i * cols + j]);
}
}
fclose(inFile);
printf("\nThe sum of the two matrices:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
m3[i * cols + j] = m1[i * cols + j] + m2[i * cols + j];
printf(" %d", m3[i * cols + j]);
}
printf("\n");
}
#else
/*Scan and store Matrix 1 and Matrix 2*/
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
fscanf(inFile, "%d", &m1[i][j]);
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
fscanf(inFile, "%d", &m2[i][j]);
}
}
fclose(inFile);
printf("\nThe sum of the two matrices:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
m3[i][j] = m1[i][j] + m2[i][j];
printf(" %d", m3[i][j]);
}
printf("\n");
}
#endif
}
关于c - 在C中动态分配二维数组的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49436608/