提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define ROWS 4
#define COLUMNS 5
typedef struct TwoDArray{
int rows;
int columns;
int** elements;
}TwoDArray,*TwoDArrayPtr;
typedef struct TwoStaticArray{
int rows;
int columns;
int elements[ROWS][COLUMNS];
}TwoDStaticArray,*TwoDStaticArrayPtr;
TwoDArrayPtr initTwoDArray(int paraRows,int paraColumns){
int i;
TwoDArrayPtr resultPtr =(TwoDArrayPtr)malloc(sizeof(struct TwoDArray));
resultPtr->rows=paraRows;
resultPtr->columns=paraColumns;
resultPtr->elements=(int**)malloc(paraRows*sizeof(int*));
for(i=0;i<paraRows;i++){
resultPtr->elements[i]=(int*)malloc(paraColumns*sizeof(int));
}
return resultPtr;
}
void randomizeTwoDArray(TwoDArrayPtr paraPtr,int paraLowerBound,int paraUpperBound){
int i,j;
for(i=0;i<paraPtr->rows;i++){
for(j=0;j<paraPtr->columns;j++){
paraPtr->elements[i][j] = rand() % (paraUpperBound - paraLowerBound) + paraLowerBound;
}
}
}
void printTwoDArray(TwoDArrayPtr paraPtr){
int i,j;
for(i=0;i<paraPtr->rows;i++){
for(j=0;j<paraPtr->columns;j++){
printf("%d, ",paraPtr->elements[i][j]);
}
printf("\r\n");
}
}
TwoDArrayPtr matrixMultiply(TwoDArrayPtr paraPtr1, TwoDArrayPtr paraPtr2){
int i,j,k,sum;
if(paraPtr1->columns!=paraPtr2->rows){
printf("Matrices cannot be multiplied.\r\n");
return NULL;
}
TwoDArrayPtr resultPtr =initTwoDArray(paraPtr1->rows,paraPtr2->columns);
for(i=0;i<paraPtr1->rows;i++){
for(j=0;j<paraPtr2->columns;j++){
sum=0;
for(k=0;k<paraPtr1->columns;k++){
sum+=paraPtr1->elements[i][k]*paraPtr2->elements[k][j];
}
resultPtr->elements[i][j]=sum;
printf("sum = %d, ", sum);
}
} return resultPtr;
}
void twoDArrayTest(){
TwoDArrayPtr tempPtr1,tempPtr2,tempPtr3;
tempPtr1=initTwoDArray(3,2);
randomizeTwoDArray(tempPtr1,1,5);
printf("The first matrix:\r\n");
printTwoDArray(tempPtr1);
tempPtr2 = initTwoDArray(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);
}
TwoDStaticArrayPtr initTwoDStaticArray(){
int i, j;
TwoDStaticArrayPtr resultPtr = (TwoDStaticArrayPtr)malloc(sizeof(TwoDStaticArray));
resultPtr->rows = ROWS;
resultPtr->columns = COLUMNS;
for (i = 0; i < ROWS; i ++){
for (j = 0; j < COLUMNS; j ++) {
resultPtr->elements[i][j] = i * 10 + j;
//Print their addresses.
printf("(%d, %d): %d; ", i, j, &(resultPtr->elements[i][j]));
}//Of for j
}//Of for i
return resultPtr;
}// Of initTwoDStaticArray
int main(){
twoDArrayTest();
TwoDStaticArrayPtr tempPtr = initTwoDStaticArray();
return 1;
}
结果如下
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, The result:
30, 30, 32, 40,
0, 0, 0, 0,
0, 0, 0, 0,
(0, 0): 10582136; (0, 1): 10582140; (0, 2): 10582144; (0, 3): 10582148; (0, 4): 10582152; (1, 0): 10582156; (1, 1): 10582160; (1, 2): 10582164; (1, 3): 10582168; (1, 4): 10582172; (2, 0): 10582176; (2, 1): 10582180; (2, 2): 10582184; (2, 3): 10582188; (2, 4): 10582192; (3, 0): 10582196; (3, 1): 10582200; (3, 2): 10582204; (3, 3): 10582208; (3, 4): 10582212;