c語言中計算矩陣的乘積


c語言中計算矩陣的乘積。

矩陣相乘的條件:左側矩陣的列數等於右側矩陣的行數。

矩陣相乘的結果:行數為左側矩陣的行數,列數為右側矩陣的列數。

 

#include <stdio.h>

int main(void) { int i, j, k, a[4][6], b[6][7], c[4][7] = {0}; puts("please input the elements of matrix a."); for(i = 0; i < 4; i++) { for(j = 0; j < 6; j++) { printf("a[%d][%d] = ", i, j); scanf("%d", &a[i][j]); } } puts("\nshow the matrix for of matrix a."); for(i = 0; i < 4; i++) { for(j = 0; j < 6; j++) { printf("%4d", a[i][j]); } putchar('\n'); } puts("\nplease input the elements of matrix b."); for(i = 0; i < 6; i++) { for(j = 0; j < 7; j++) { printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]); } } puts("\nshow the matrix form of matrix b."); for(i = 0; i < 6; i++) { for(j = 0; j < 7; j++) { printf("%4d", b[i][j]); } putchar('\n'); } puts("\n==================================="); for(i = 0; i < 4; i++) { for(j = 0; j < 7; j++) { for(k = 0; k < 6; k++) { c[i][j] += a[i][k] * b[k][j]; } } } puts("show the product of the two matrixes."); for(i = 0; i < 4; i++) { for(j = 0; j < 7; j++) { printf("%4d", c[i][j]); } putchar('\n'); } return 0; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM