C语言实现矩阵乘法

本文将使用C语言实现矩阵乘法;分别生成阶为m*n和p*q的两个矩阵,以得到阶为m*q的结果矩阵,其中n等于p。

示例代码:

#include <stdio.h>
 
void main(){
	int m=3, n=3, p=3, q=3, c, d, k, sum = 0;
	int first[m][n], second[p][q], multiply[m][q];

	first[0][0] = 1; first[0][1] = 2; first[0][2] = 0;
	first[1][0] = 0; first[1][1] = 1; first[1][2] = 1;
	first[2][0] = 2; first[2][1] = 0; first[2][2] = 1;

	second[0][0] = 1; second[0][1] = 1; second[0][2] = 2;
	second[1][0] = 2; second[1][1] = 1; second[1][2] = 1;
	second[2][0] = 1; second[2][1] = 2; second[2][2] = 1;

	if ( n != p ){
		printf("Matrices with entered orders can't be multiplied with each other.\n");
	}

	for(c=0;c<m;c++){
		for(d=0;d<q;d++){
			for(k=0;k<p;k++){
				sum = sum + first[c][k]*second[k][d];
			}
			multiply[c][d] = sum;
			sum = 0 ;
		}
	}

	printf("Product of entered matrices:\n");

	for(c=0;c<m;c++){
		for(d=0;d<q;d++){
			printf("%d\t", multiply[c][d]);
		}
		printf("\n");
	}

}

运行效果:

C语言实现矩阵乘法

原创内容,如需转载,请注明出处;

本文地址: https://www.perfcode.com/p/matrix-multiplication.html

分类: 计算机技术
推荐阅读:
安装和更新Requests 要安装 Requests,只需要在你的终端(Windows下是CMD)运行以下命令
Linux下快速的重命名文件 在Linux系统下,不知道你们是怎么重命名文件的,虽然方法很多,但我这个绝对是最简单且最高效的,而且只有一条命令就可实现:
cannot fallthrough final case in switch的解决方法 在Go语言中,fallthrough 用于 switch 语句的 case 块中,它将会在当前 case 块执行完后执行下一个 case 块,不论下个 case 块条件是否匹配,但如果 fallthrough 后没有 case ,则会产生 cannot fallthrough final case in switch 的错误。
Implementation of the USB 2.0 controller not found!解决方案 你可能在使用VirtualBox启动虚拟机系统时碰到“不能为虚拟电脑打开一个新的任务”的错误提示,并提示 Implementation of the USB 2.0 controller not found!
C语言计算数组元素数量 使用sizeof()函数计算整个数组的内存占用大小,再计算单个元素的内存占用大小;以总内存占用大小除以单个元素的内存占用大小,得到的就是元素数量;
PySide6改变界面主题风格 在本文中,您将学会如何使用QApplication的静态函数setStyle()更改PySide6的主题风格;