1

好吧,我正在做矩阵乘法,我需要制作一个m x n数组和一个p x q数组。
但是,我不知道该怎么做。

这是我手动输入值时打印正确输出的程序:

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    /*
        Rows and columns for matrices.
    */
    int m , n; // rows and columns of the first matrix
    int p , q; // rows and columns of the second matrix

    /*
        1st matrix is a 2x3 matrix
    */
    m = 2;
    n = 3;

    /*
        2nd matrix is a 3x2 matrix
    */
    p = 3;
    q = 2;


    /*
        Create the matrices.
        Give them values.
    */
    int matrix1[m][n] = {
                            {2,3,4},
                            {5,6,7}
                        };
    int matrix2[p][q] = {
                            {1,7},
                            {3,9},
                            {5,11}
                        };

    /*
        Check if we can multiple the matrices.
        For matrix multiplication,
        the number of COLUMNS of FIRST matrix must be equal to
        the number of ROWS of SECOND matrix
    */
    if(n==p){
        /*
            Create a new matrix.
            The resulting matrix will have M rows and Q columns.
            That is, the matrix is a MxQ matrix.
        */
        int matrix3[2][2]; 

        /*
            We need three loops so we have 3 variables.
        */
        int i = 0; // iterates over matrix1 rows
        int j = 0; // iterates over matrix1 columns
        int k = 0; // iterates over matrix2 rows
        int l = 0; // iterates over matrix2 columns


        while(i < m){
            l = 0;
            while(l < q){
                int element = 0;
                while(j < n && k < p){
                    element += matrix1[i][j] * matrix2[k][l];
                    matrix3[i][l] = element;
                    j++;
                    k++;
                }
                printf("\t%d",element);
                l++;
                j = 0;
                k = 0;
            }
            printf("\n");
            i++;
        }

    }else{
        printf("Matrices can not be multiplied");
    }
}  

矩阵声明被标记为错误。我该如何解决?

4

3 回答 3

3

我该如何解决?

首先,不使用 VLA。对于此特定任务,您不需要 VLA。

至于实际问题是什么:无法初始化可变长度数组。您必须一个接一个地分配给他们的元素或使用一些大规模“分配”技术,例如memcpy().

于 2013-09-21T19:48:52.650 回答
1

可以选择使用C99吗?

可变长度数组

这在 C99 中 100% 有效:

  int m = 2;
  int n = 3;

  int matrix1[m][n];
于 2013-09-21T19:48:21.293 回答
1

根据当前的 c 标准,“本机”可变长度数组是一种可选的语言结构。您必须检查编译器文档以确定您使用的编译器是否支持可变长度数组。

在 C99 之前,可变长度数组需要在堆上显式分配并通过指针访问。对于矩阵问题,您有两种选择。

首先是分配一个有足够存储空间的数组,并在需要读取或修改矩阵元素时计算正确的索引。例如:

int* matrix_storage = malloc( sizeof( int ) * matrix_width * matrix_height );
// set row 0, column 1 to 0
matrix_storage[ ( 0 * matrix_width ) + ( 1 % matrix_width ) ] = 0; 

或者,您可以为每一行或每一列分配一个指针数组:

int** rows = malloc( sizeof( (int*) ) * matrix_height );
for( int i = 0; i < matrix_height; ++i )
{
    rows[i] = malloc( sizeof(int) * matrix_width );
}
row[0][1] = 0; // set row 0, column 1 to 0

这个实现浪费了一些内存,不管你选择什么实现考虑使用函数和struct关键字来隐藏驱动程序代码(实际上是变异或读取矩阵的代码)的实现。

一些注意事项:我使用了 c99for循环语法,对于较旧的编译器,int i需要在for循环外声明变量。另请注意,如果您的矩阵大小可以在编译时确定(IE 不依赖于用户输入),则您不需要 malloc 的开销并且可以硬编码您的数组大小。您发布的示例代码不需要动态分配。

于 2013-09-21T20:06:18.730 回答