c - C中使用指针的矩阵乘法

标签 c pointers matrix

我想使用c中的指针来乘以2个矩阵。棘手的是,我必须在Linux终端中使用 gcc -Werror -o run filename file.c,所以我的代码中甚至不能有警告。

我有 myArray 矩阵 n x n (其中 n 是偶数 - 4,6,8...),我必须将其分为 4 部分,之后,我必须将这部分相乘像:

1x2=a、ax3=b 和 bx4=c(其中 1,2,3,4 是我的初始矩阵的 4 个部分)。

问题很简单,但我处理不了。 代码是:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//---------------------------------------------initialize and adding elements!!!
void init_matrix(int **myArray, int n)
{
  int i,j;
    printf("Insert data:\n");
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {
            scanf("%d", &*(*(myArray+i)+j));
        }

    printf("The introduced Array:\n");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
            printf("%d ", *(*(myArray+i)+j));
          printf("\n");
        }
        printf("\n");
        {
    }

}

//---------------------------------------------devisation in 4 parts!!!
void first_matrix(int **myArray,int n)
{  int i,j,m;

    printf("\nThe first matrix is: \n");

       for (i = 0; i < n/2; i++)
    {
        for (j = 0; j < m/2; j++)
            printf("%d ", *(*(myArray+i)+j));
                  printf("\n"); 
        }
        printf("\n");
        {
    }
}

void second_matrix(int **myArray,  int n)
{  int i,j,m;

    printf("\nThe second matrix is: \n");

       for (i = 0; i < n/2; i++)
    {
        for (j = m/2; j < m; j++)
            printf("%d ", *(*(myArray+i)+j));
          printf("\n");
        }
        printf("\n");
        {
    }
}

void third_matrix(int **myArray,  int n)
{  int i,j,m;

    printf("\nThe third matrix is: \n");

       for (i = n/2; i < n; i++)
    {
        for (j = 0; j < m/2; j++)
            printf("%d ", *(*(myArray+i)+j));
          printf("\n");
        }
        printf("\n");
        {
    }
}

void fourth_matrix(int **myArray,  int n)
{  int i,j,m;

    printf("\nThe fourth matrix is: \n");

       for (i = n/2; i < n; i++)
    {
        for (j = m/2; j < m; j++)
            printf("%d ", *(*(myArray+i)+j));
          printf("\n");
        }
        printf("\n");
        {
    }
}

int main(void)//----------------------------------------------------main function!!!
{
    int **myArray,i, n;

        printf("Enter the dimmension of Array: ");
        scanf("%d", &n);

    //alloc memory
    myArray = (int **) malloc( n * sizeof(int*));
    for(i = 0; i< n; i++)
    myArray[i] = (int *) malloc(sizeof(int));


    init_matrix(myArray,n);
    first_matrix(myArray,n);
    second_matrix(myArray,n);
    third_matrix(myArray,n);
    fourth_matrix(myArray,n);

     //some methods for mutiply

    //free memory
    for(i = 0; i< n; i++)   
    free(myArray[i]);
    free(myArray);

   printf("\n");
    return 0;
}

最佳答案

The tricky thing is that I must use gcc -Werror -o run filename file.c in linux terminal, so I can't have even warnings in my code

对于初学者来说,所有代码都应该在没有警告的情况下进行编译。警告是编译器说的方式“在解决这个问题之前,您的代码可能不会以可预测的方式或您认为的方式运行。”编译器很少会出错。

接下来,使用指针进行一般矩阵乘法,只需要您遵循矩阵乘法的规则。这意味着您可以乘以 m x n矩阵 ( matrix_a ) 具有 n x p矩阵 ( matrix_b ),结果的维度为 m x p (product matrix)。请参阅Matrix Multiplication了解详情。

您需要分配空间来容纳 product matrix 。最简单的方法是声明一个乘法函数,该函数返回一个指向乘积矩阵所需类型的指针。您需要通过matrix_a , matrix_b以及dimensions for each为了分配、计算并返回 product 。以下是该实现的一种方法。

注意:因为您正在为 product matrix 分配空间。您也有责任释放它。另注意,建议初始化 product matrix0 。选择calloc行分配可以为您自动执行此操作,而无需显式归零。 (下面显示了错误检查,但未实现)

/* 
    if mtrx_a is (m x n) and mtrx_b is (n x p), 
    the product is an (m x p) matrix
*/
int **mtrx_mult (int m, int n, int p, int **mtrx_a, int **mtrx_b)
{
    int **result = malloc (m * sizeof (*result));
    // if (!result) throw error

    register int i=0, j=0, k=0;
    for (i = 0; i < m; i++)
    {
        /* calloc initializes all to '0' */
        result[i] = calloc (p, sizeof (**result));
        // if (!result[i]) throw error
    }

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < p; j++)
        {
            for (k = 0; k < n; k++)
            {
                result [i][j] += mtrx_a [i][k] * mtrx_b [k][j];
            }
        }
    }

    return result;
}

关于c - C中使用指针的矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494196/

相关文章:

c++ - 每个版本更新的代码更改

pointers - 如果在 P 设置为 nil 后将 C 类的字段 F 分配给指针 P,在 FreePascal 中会发生什么?

c++ - Eigen 浮点精度

c - 根据参数值展开宏

c - fgetc() 无法读取 float

c - 未定义的函数在内核linux中插入新模块

c++ - 使用引用指向节点的指针的函数删除链表中的节点?

c - 在C函数中对数组排序

matlab - 如果矩阵的第一列提供索引值,则求子矩阵的均值

python - 列值到行值的 Numpy 转换