/////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
#include
#define SIZE 20 //預(yù)定義要進(jìn)行轉(zhuǎn)置的矩陣最大大小為 20*20
//為了函數(shù)參數(shù)傳遞的方便,將行和列的具體大小定義為全局變量
int a_column;
int a_row ;
int b_row;
int c[SIZE][SIZE]={0};
//矩陣的輸入函數(shù)
void inputMatrix(int a[][SIZE] , int n, int m){//二維數(shù)組參數(shù)下標(biāo)必須要確定
int i,j;
for(i = 0;i < n;i++){
for(j = 0;j < m;j++){
scanf("%d",&a[i][j]);
}
}
}
//矩陣的輸出函數(shù)
void outputMatrix(int c[][SIZE] , int n, int m){
int i,j;
for(i = 0;i < n;i++){
for(j = 0;j < m;j++){
printf("%d",c[i][j]);
}
printf("n");//每次打印完一行后進(jìn)行換行
}
}
//矩陣的乘法算
void matrixMultiplication(int a[][SIZE], int b[][SIZE]){
int i,j,k;
for(i = 0;i < a_column;i++){
for(j = 0; j < b_row ;j++){
for(k = 0; k< a_row;k++){
c[i][j]= c[i][j]+ a[i][k] * b[k][j];
}
}
}
}
int main()
{
//定義數(shù)組并初始化
int a[SIZE][SIZE]={0};
int b[SIZE][SIZE]={0};//定義數(shù)組,注意要初始化
//矩陣行,列數(shù)的確定
printf("請輸入第一個(gè)矩陣的行數(shù) : ");
scanf("%d",&a_column);
printf("n請輸入您要進(jìn)行轉(zhuǎn)置的矩陣的列數(shù) :");
scanf("%d",&a_row);
//函數(shù)調(diào)用及主功能實(shí)現(xiàn)
printf("請輸入矩陣A ( %d X %d 形式)n" , a_column ,a_row);
inputMatrix(a, a_column ,a_row);
printf("注意:根據(jù)數(shù)學(xué)原理,您將輸入的第二個(gè)矩陣的行數(shù)為 %dn",a_row);
printf("請輸入矩陣B的列數(shù) : ");
scanf("%d",&b_row);
printf("請輸入矩陣B ( %d X %d 形式) : n" , a_row ,b_row);
inputMatrix(b, a_row , b_row);
//調(diào)用相乘函數(shù)
matrixMultiplication(a,b);
printf("A與B相乘后的矩陣C是 :n");
outputMatrix(c, a_column , b_row);
getchar();
return 0;
}
運(yùn)行結(jié)果:
愛華網(wǎng)



