首页 > 解决方案 > 动态分配的多维数组(矩阵)的乘法

问题描述

这里我使用两个多维数组来执行乘法。

int **matrix1, **matrix2;

malloc()接下来,我使用和填充其中的元素为两个矩阵分配内存。然后我将两个矩阵相乘并将结果矩阵元素存储在另一个矩阵(即第三个矩阵)中。直到两个矩阵的行和列都相同,我没有遇到任何问题,但是,

我们有一个矩阵乘法规则:

ColumnsOfFirstMatrix 应该等于RowsOfSecondMatrix.

在这里我面临问题:

所以rowsOfFirstMatrixcolumnsOfSecondMatrix可以在两个矩阵中不同。

我的示例代码:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int **matrix1, **matrix2;
    int rowsOfMat1, columnsOfMat1, rowsOfMat2, columnsOfMat2;
    clrscr();
    printf("\n\n  IN ORDER TO PERFOME MATRIX MULTIPLICATION COLUMNS OF FIRST MATRIX IS EQUAL TO ROWS OF SECOND MATRIX !!!");
    printf("\n\n  ENTER NO. OF ROWS OF MATRIX__1 : ");
    scanf("%d",&rowsOfMat1);
    printf("\n\n  ENTER NO. OF COLUMNS OF MATRIX__1 : ");
    scanf("%d",&columnsOfMat1);
    printf("\n\n  ENTER NO. OF ROWS OF MATRIX__2 : ");
    scanf("%d",&rowsOfMat2);
    printf("\n\n  ENTER NO. OF COLUMNS OF MATRIX__2 : ");
    scanf("%d",&columnsOfMat2);

    ////// ALLOCATING MEMORY FOR MATRIX-1 //////
    if(columnsOfMat1 != rowsOfMat2)
    {
    printf("\n\n  YOUR INPUT IS NOT MATCHING TO MATRIX MULTIPLICATION RULES");
    puts("\n\n  Press any key to continue...");
    getch();
    return 0;
    }
    matrix1 = (int**)malloc(sizeof(*matrix1) * rowsOfMat1);
    for(int i = 0; i < rowsOfMat1; i++)
    {
        matrix1[i] = (int*)malloc(sizeof(*matrix1[i]) * columnsOfMat1);
    }

    ////// ALLOCATING MEMORY FOR MATRIX-2 //////
    matrix2 = (int**)malloc(sizeof(*matrix2) * rowsOfMat2);
    for(int j = 0; j < rowsOfMat2; j++)
    {
        matrix1[i] = (int*)malloc(sizeof(*matrix2[j]) * columnsOfMat2);
    }

    ////// STORING VALUES IN MATRIX__1 //////
    printf("\n\n  FILL ELEMENTS IN MATRIX__1 !!");
    for(int k = 0; k < rowsOfMat1; k++)
    {
        for(int l = 0; l < columnsOfMat1; l++)
        {
            printf("\n  Enter element A[%d][%d] : ",k+1,l+1);
      
         scanf("%d",&matrix1[k][l]);
        }
    }

    ////// STORING VALUES IN MATRIX__2 //////
    printf("\n\n  FILL ELEMENTS IN MATRIX__2 !!");
    for(int m = 0; m < rowsOfMat2; m++)
    {
        for(int n = 0; n < columnsOfMat2; n++)
        {
            printf("\n  Enter element A[%d][%d] : ",m+1,n+1);
             scanf("%d",&matrix2[m][n]);
        }
    }

    //////ALLOCATING MEMORY FOR PRODUCT MATRIX //////
    int **productMatrix = (int**)malloc(sizeof(**productMatrix) * rowsOfMat1);
    for(int p = 0; p < rowsOfMat1; p++)
    {
        productMatrix[p] = (int*)malloc(sizeof(*productMatrix[p]) * columnsOfMat2);
    }
    ////// MULTIPLICATION OF MATRICES LOGIC //////
    /* 
    This is working fine when
    rows and columns are equal in both matrices
    but when they are not equal it is
    printing address    
    */
    int sum = 0;
    for(int q = 0; q < rowsOfMat1; q++)
    {
        for(int r = 0; r < columnsOfMat1; r++)
        {
            for(int s = 0; s < columnsOfMat2; s++)
            {
                sum += matrix1[q][s] * matrix2[s][r];
            }
        productMatrix[q][r] = sum;
        sum = 0;
        }
    }

    ////// PRINTING THE ELEMENTS OF RESULTANT MATRICES //////
    printf("\n\n  RESULTANT MATRIX !!\n");
    for(int t = 0; t < columnsOfMat1; t++)
    {
        for(int u = 0; u < rowsOfMat2; u++)
        {
            printf(" %3d",productMatrix[t][u]);
        }
    putchar('\n');
    }
    puts("\n\n Press any key to continu....");
// DEALLOCATING MEMORIES
for(int p = 0; p < rowsOfMat1; p++)
{
    free(productMatrix[p]);
}
free(productMatrix);
for(int i = 0; i < rowsOfMat1; i++)
{
    free(matrix1[i]);
}
free(matrix1);
for(int j = 0; j < rowsOfMat1; j++)
{
    free(matrix2[j]);
}
free(matrix2);
    getch();
    return 0;
}

标签: arrayscmultidimensional-arraydynamic-memory-allocation

解决方案


推荐阅读