#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define ROWS 4
#define COLUMNS 5
/**
* Two dimensional array.
*/
typedef struct TwoDArray{
int rows;
int columns;
int** elements;
} TwoDArray, *TwoDArrayPtr;
/**
* Two dimensional array.
*/
typedef struct TwoDStaticArray{
int rows;
int columns;
int elements[ROWS][COLUMNS];
} TwoDStaticArray, *TwoDStaticArrayPtr;
/**
* Initialize the two-dimensional array.
*/
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));
}//Of for i
return resultPtr;
}// Of initTwoDArray
/**
* Randomize the data of the array.
*/
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;
}//Of for j
}//Of for i
}// Of randomizeTwoDArray
/**
* Print the array
*/
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]);
}//Of for j
printf("\r\n");
}//Of for i
}// Of initTwoDArray
/**
* Matrix Multiply.
*/
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;
}//Of if
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];
}//Of for k
resultPtr->elements[i][j] = sum;
printf("sum = %d, ", sum);
}//Of for j
}//Of for i
return resultPtr;
}// Of matrixMultiply
/**
* Test the generation of the 2D array.
*/
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);
}//Of twoDArrayTest
/**
* Initialize the two-dimensional array.
*/
TwoDStaticArrayPtr initTwoDStaticArray(){
int i, j;
TwoDStaticArrayPtr resultPtr = (TwoDStaticArrayPtr)malloc(sizeof(struct 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
/**
* The entrance.
*/
int main(){
twoDArrayTest();
TwoDStaticArrayPtr tempPtr = initTwoDStaticArray();
return 1;
}// Of main
05-20
3558
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)