矩阵乘法定义
完整代码
#include <stdio.h>
#include <stdlib.h>
#define ROWS 4
#define COLUMNS 5
typedef struct TwoDArray{
int rows;
int columns;
int** elements;
}TwoDArray,*TwoDArrayPtr;
typedef struct TwoDStaticArray{
int rows;
int columns;
int elements[ROWS][COLUMNS];
}TwoDStaticArray,*TwoDStaticArrayPtr;
TwoDArrayPtr iniTwoDArray(int pRows,int pColumns){
int i;
TwoDArrayPtr result = (TwoDArrayPtr)malloc(sizeof(struct TwoDArray));
result->rows = pRows;
result->columns = pColumns;
result->elements = (int**)malloc(pRows*sizeof(int*));
for(i = 0;i < pRows;i++){
result->elements[i] = (int*)malloc(pColumns * sizeof(int));
}
return result;
}
TwoDStaticArrayPtr initTwoDStaticArray(){
int i, j;
TwoDStaticArrayPtr result = (TwoDStaticArrayPtr)malloc(sizeof(struct TwoDStaticArray));
result->rows = ROWS;
result->columns = COLUMNS;
for (i = 0; i < ROWS; i ++){
for (j = 0; j < COLUMNS; j ++) {
result->elements[i][j] = i * 10 + j;
printf("(%d, %d): %d\n", i, j, &(result->elements[i][j]));
}
}
return result;
}
void randomizeTwoDArray(TwoDArrayPtr pPtr,int pLowerBound,int pUpperBound){
int i,j;
for(i = 0;i < pPtr->rows;i++){
for(j = 0;j < pPtr->columns;j++){
pPtr->elements[i][j] = rand() % (pUpperBound - pLowerBound) + pLowerBound;
}
}
}
void printTwoDArray(TwoDArrayPtr pPtr){
int i,j;
for(i = 0;i < pPtr->rows;i++){
for(j = 0;j < pPtr->columns;j++){
printf("%d,",pPtr->elements[i][j]);
}
printf("\n");
}
}
TwoDArrayPtr matrixMultiply(TwoDArrayPtr pPtr1,TwoDArrayPtr pPtr2){
int i,j,k,sum;
if(pPtr1->columns != pPtr2->rows){
printf("Matrices cannot be multiplied.\r\n");
return NULL;
}
TwoDArrayPtr result = iniTwoDArray(pPtr1->rows,pPtr2->columns);
for(i = 0;i < pPtr1->rows;i++){
for(j = 0;j < pPtr2->columns;j++){
sum = 0;
for(k = 0;k < pPtr1->columns;k++){
sum += pPtr1->elements[i][k] * pPtr2->elements[k][j];
}
result->elements[i][j] = sum;
printf("sum = %d;",sum);
}
}
printf("\n");
return result;
}
void twoDArrayTest(){
TwoDArrayPtr tempPtr1, tempPtr2, tempPtr3;
tempPtr1 = iniTwoDArray(3, 2);
randomizeTwoDArray(tempPtr1, 1, 5);
printf("The first matrix:\r\n");
printTwoDArray(tempPtr1);
tempPtr2 = iniTwoDArray(2, 4);
randomizeTwoDArray(tempPtr2, 4, 9);
printf("The second matrix:\r\n");
printTwoDArray(tempPtr2);
tempPtr3 = matrixMultiply(tempPtr1, tempPtr2);
printf("The result:\r\n");
printTwoDArray(tempPtr3);
}
int main(){
twoDArrayTest();
TwoDStaticArrayPtr tempPtr = initTwoDStaticArray();
for (int i = 0; i < ROWS; i ++){
for (int j = 0; j < COLUMNS; j ++) {
printf("(%d, %d): %d\n", i, j, (tempPtr->elements[i][j]));
}
}
return 0;
}
运行结果
The first matrix:
2,4,
3,1,
2,1,
The second matrix:
7,7,6,8,
4,4,5,6,
sum = 30;sum = 30;sum = 32;sum = 40;sum = 25;sum = 25;sum = 23;sum = 30;sum = 18;sum = 18;sum = 17;sum = 22;
The result:
30,30,32,40,
25,25,23,30,
18,18,17,22,
(0, 0): 12348360
(0, 1): 12348364
(0, 2): 12348368
(0, 3): 12348372
(0, 4): 12348376
(1, 0): 12348380
(1, 1): 12348384
(1, 2): 12348388
(1, 3): 12348392
(1, 4): 12348396
(2, 0): 12348400
(2, 1): 12348404
(2, 2): 12348408
(2, 3): 12348412
(2, 4): 12348416
(3, 0): 12348420
(3, 1): 12348424
(3, 2): 12348428
(3, 3): 12348432
(3, 4): 12348436
(0, 0): 0
(0, 1): 1
(0, 2): 2
(0, 3): 3
(0, 4): 4
(1, 0): 10
(1, 1): 11
(1, 2): 12
(1, 3): 13
(1, 4): 14
(2, 0): 20
(2, 1): 21
(2, 2): 22
(2, 3): 23
(2, 4): 24
(3, 0): 30
(3, 1): 31
(3, 2): 32
(3, 3): 33
(3, 4): 34