思路:
1、首先我们要定义三个二维数组来存储第一个矩阵、第二个矩阵和相乘后得到的新矩阵。
2、对第一个、第二个矩阵的每个进行赋值(要注意的是我们需要让第一个矩阵的列数等于第二个矩阵的行数),然后将其打印出来。
3、使用for循环语句,先遍历第二个矩阵的第j列的每一个元素,然后将其与第一个元素的第i列的每一个元素进行相乘并相加。将其运算后的结果放在对应第i行第j列的新数组中。
4、再利用for循环将得到的新数组进行一个元素一个元素的输出。
老师的代码
#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
运行结果
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): 7356432; (0, 1): 7356436; (0, 2): 7356440; (0, 3): 7356444; (0, 4): 7356448; (1, 0): 7356452; (1, 1): 7356456; (1, 2): 7356460; (1, 3): 7356464; (1, 4): 7356468; (2, 0): 7356472; (2, 1): 7356476; (2, 2): 7356480; (2, 3): 7356484; (2, 4): 7356488; (3, 0): 7356492; (3, 1): 7356496; (3, 2): 7356500; (3, 3): 7356504; (3, 4): 7356508; Press any key to continue
我的代码
#include<iostream>
using namespace std;
int main(){
int n,m,k;
int a[100][100],b[100][100],c[100][100]={0};
cin >> n >> m >> k;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin >> a[i][j];//矩阵A
}
}
for(int i=0;i<m;i++){
for(int j=0;j<k;j++){
cin >> b[i][j];//矩阵B
}
}
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
for(int z=0;z<m;z++){
c[i][j]+=a[i][z]*b[z][j];//矩阵C(i,j)等于A的i行的元素与B的第j列的元素对应乘积再累计乘积之和
}
cout << c[i][j] << " ";
}
cout << endl;
}
return 0;
}