在 C 中动态分配二维数组的总和



对于这个练习(我们必须扫描一个 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("nnFile 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阵列的方式。在大多数情况下,我倾向于将我的 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("nnFile 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
}

相关内容

  • 没有找到相关文章

最新更新