C语言编程题矩阵乘法

矩阵乘法是线性代数中的一种基本运算,广泛应用于计算机科学、工程和物理等领域。在C语言中,实现矩阵乘法通常涉及对矩阵元素的嵌套循环访问和累积计算。

1、基础实现

使用简单的嵌套循环来实现矩阵乘法。

#include <stdio.h>

#define ROW1 2
#define COL1 3
#define ROW2 3
#define COL2 2

void multiplyMatrices(int firstMatrix[ROW1][COL1], int secondMatrix[ROW2][COL2], int result[ROW1][COL2]) {
    for (int i = 0; i < ROW1; ++i) {
        for (int j = 0; j < COL2; ++j) {
            result[i][j] = 0;
            for (int k = 0; k < COL1; ++k) {
                result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
            }
        }
    }
}

void printMatrix(int matrix[ROW1][COL2]) {
    for (int i = 0; i < ROW1; ++i) {
        for (int j = 0; j < COL2; ++j) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int firstMatrix[ROW1][COL1] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    int secondMatrix[ROW2][COL2] = {
        {7, 8},
        {9, 10},
        {11, 12}
    };

    int result[ROW1][COL2];

    multiplyMatrices(firstMatrix, secondMatrix, result);

    printf("结果矩阵:\n");
    printMatrix(result);

    return 0;
}

2、使用动态内存分配

使用动态内存分配来处理任意大小的矩阵。

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

void multiplyMatrices(int **firstMatrix, int **secondMatrix, int **result, int row1, int col1, int col2) {
    for (int i = 0; i < row1; ++i) {
        for (int j = 0; j < col2; ++j) {
            result[i][j] = 0;
            for (int k = 0; k < col1; ++k) {
                result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
            }
        }
    }
}

void printMatrix(int **matrix, int row, int col) {
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int row1 = 2, col1 = 3, row2 = 3, col2 = 2;

    int **firstMatrix = (int **)malloc(row1 * sizeof(int *));
    for (int i = 0; i < row1; i++)
        firstMatrix[i] = (int *)malloc(col1 * sizeof(int));

    int **secondMatrix = (int **)malloc(row2 * sizeof(int *));
    for (int i = 0; i < row2; i++)
        secondMatrix[i] = (int *)malloc(col2 * sizeof(int));

    int **result = (int **)malloc(row1 * sizeof(int *));
    for (int i = 0; i < row1; i++)
        result[i] = (int *)malloc(col2 * sizeof(int));

    // 初始化矩阵
    int fm[2][3] = { {1, 2, 3}, {4, 5, 6} };
    int sm[3][2] = { {7, 8}, {9, 10}, {11, 12} };

    for (int i = 0; i < row1; i++)
        for (int j = 0; j < col1; j++)
            firstMatrix[i][j] = fm[i][j];

    for (int i = 0; i < row2; i++)
        for (int j = 0; j < col2; j++)
            secondMatrix[i][j] = sm[i][j];

    multiplyMatrices(firstMatrix, secondMatrix, result, row1, col1, col2);

    printf("结果矩阵:\n");
    printMatrix(result, row1, col2);

    // 释放分配的内存
    for (int i = 0; i < row1; i++)
        free(firstMatrix[i]);
    free(firstMatrix);

    for (int i = 0; i < row2; i++)
        free(secondMatrix[i]);
    free(secondMatrix);

    for (int i = 0; i < row1; i++)
        free(result[i]);
    free(result);

    return 0;
}

3、使用函数指针传递矩阵

用函数指针传递矩阵参数,实现矩阵乘法。

#include <stdio.h>

void multiplyMatrices(int *firstMatrix, int *secondMatrix, int *result, int row1, int col1, int col2) {
    for (int i = 0; i < row1; ++i) {
        for (int j = 0; j < col2; ++j) {
            *(result + i * col2 + j) = 0;
            for (int k = 0; k < col1; ++k) {
                *(result + i * col2 + j) += *(firstMatrix + i * col1 + k) * *(secondMatrix + k * col2 + j);
            }
        }
    }
}

void printMatrix(int *matrix, int row, int col) {
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            printf("%d ", *(matrix + i * col + j));
        }
        printf("\n");
    }
}

int main() {
    int row1 = 2, col1 = 3, col2 = 2;

    int firstMatrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    int secondMatrix[3][2] = {
        {7, 8},
        {9, 10},
        {11, 12}
    };

    int result[2][2];

    multiplyMatrices((int *)firstMatrix, (int *)secondMatrix, (int *)result, row1, col1, col2);

    printf("结果矩阵:\n");
    printMatrix((int *)result, row1, col2);

    return 0;
}

推荐阅读
cjavapy编程之路首页