【问题标题】:Problems in generating random matrices in C在 C 中生成随机矩阵的问题
【发布时间】:2018-08-30 14:09:35
【问题描述】:

我编写了一个串行程序来生成 2 个随机矩阵,将它们相乘并显示结果。我为每个任务编写了函数,即生成随机矩阵,将矩阵相乘并显示结果。我无法弄清楚为什么两个生​​成的矩阵是相同的。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int **matrix_generator(int row,int col);
int **multiply_matrices(int **matrix_A,int **matrix_B,int rowsA, int colsA,int rowsB,int colsB);
void display_result(int **matrix,int cols,int rows);
void display_matrix(int **matrixA,int cols,int rows);

void main()
{
    int **matrix_A,**matrix_B,**matrix_result,i,j,k,tid,rowsA,colsA,rowsB,colsB;
    printf("Enter the dimensions of Matrix A:\n");
    scanf("%d%d",&rowsA,&colsA);
    printf("Enter the dimensions of Matrix B:\n");
    scanf("%d%d",&rowsB,&colsB);

    if(colsA==rowsB)
    {
        matrix_A = matrix_generator(rowsA,colsA);
        matrix_B = matrix_generator(rowsB,colsB);
        matrix_result = multiply_matrices(matrix_A,matrix_B,rowsA,colsA,rowsB,colsB);

        printf("Matrix A:\n");
        display_matrix(matrix_A,rowsA,colsA);
        printf("\n\n");

        printf("Matrix B:\n");
        display_matrix(matrix_B,rowsB,colsB);
        printf("\n\n");

        display_matrix(matrix_result,rowsB,colsA);

    }
    else
    {
        printf("Check the dimensions of the matrices!\n");
        exit(-1);
    }
}

int **matrix_generator(int row, int col)
{
    int i, j, **intMatrix;

    intMatrix = (int **)malloc(sizeof(int *) * row); 
    srand(time(0));

    for (i = 0; i < row; i++)
    {
        intMatrix[i] = (int *)malloc(sizeof(int *) * col);
        for (j = 0;j<col;j++)
        {
            intMatrix[i][j]=rand()%10;
        }
    }
    return intMatrix;
}

int **multiply_matrices(int **matrix_A,int **matrix_B,int rowsA, int colsA,int rowsB,int colsB)
{
    int i, j, k, **resMatrix;

    resMatrix = (int **)malloc(sizeof(int *) * rowsB); 

    for (i = 0; i < rowsA; i++)
    {
        resMatrix[i] = (int *)malloc(sizeof(int *) * colsA);
        for (j = 0;j<colsB;j++)
        {
            for (k = 0; k < colsA; k++)
                resMatrix[i][j] = resMatrix[i][j] + matrix_A[i][k] * matrix_B[k][j];        
        }
    }
    return resMatrix;
}

void display_matrix(int **matrix, int rows,int cols)
{
    int i,j;
    for (i = 0; i < rows; i = i + 1)
    {
        for (j = 0; j < cols; j = j + 1)
            printf("%d ",matrix[i][j]);
        printf("\n");
    }        
}

输出:

Enter the dimensions of Matrix A:
4
4
Enter the dimensions of Matrix B:
4
4
Matrix A:
8 7 8 4 
9 8 3 9 
1 2 0 4 
6 0 2 3 


Matrix B:
8 7 8 4 
9 8 3 9 
1 2 0 4 
6 0 2 3 


159 128 93 139 
201 133 114 147 
50 23 22 34 
68 46 54 41 

有人可以帮我理解我哪里出错了吗?我有一个很好的主意,那就是 matrix_generator() 函数,但似乎无法弄清楚出了什么问题。另外,它只是乘方矩阵,如果尺寸不同,比如 4X5 和 5X4,我会遇到分段错误。

【问题讨论】:

  • 您只需为rand() 函数播种一次。在 main() 函数的开头执行 srand(time(0)); 并将其从其他位置删除。
  • 您的问题显然是srand 前面的cmets 指出的。另一个问题是这里的内存泄漏。
  • time() 以秒为单位返回时间。如果您快速连续调用 2 次(
  • 您在resMatrix[i] = (int *)malloc(sizeof(int *) * colsA);sizeof(int *)sizeof(int))中分配的大小不正确。我建议你阅读Correctly allocating multi-dimensional arrays

标签: c matrix memory-leaks segmentation-fault


【解决方案1】:

您的代码中存在一些问题:

1) 你错误地分配了内存:

multiply_matrices中应该是

resMatrix[i] = (int *)malloc(sizeof(int) * colsB);

matrix_generator

intMatrix[i] = (int *)malloc(sizeof(int) * col);

2) 在 main 如果你想打印 matrix_result 调用

display_matrix(matrix_result,rowsA,colsB);

[rowsA,colsA] x [rowsB,colsB]的维度是rowsA x colsB

3) malloc 返回指向未初始化内存的指针,因此您应该在求和之前将 resMatrix 元素设置为零

multiply_matrices 中第二个 for 循环的内容应该是

    resMatrix[i][j] = 0;
    for (k = 0; k < rowsB; k++)  // CHANGED to rowsB
        resMatrix[i][j] = resMatrix[i][j] + matrix_A[i][k] * matrix_B[k][j];     

【讨论】:

  • 谢谢。这修复了我的代码。我需要更详细地阅读指针。
【解决方案2】:

正如 cmets 中所指出的:您只需为 rand() 函数播种一次。做 srand(time(0));在您的 main() 函数的开头,并将其从其他地方删除。

非方阵:multiply_matrices 中存在错误/错字

线

        for (k = 0; k < colsA; k++)

应该是

        for (k = 0; k < rowsA; k++)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2019-10-14
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多