C++中实现数组乘法
在原来两个二维数组的基础上需要再引入一个变量加入for循环。
实现二维数组相乘函数时传入数组的注意事项
#include<iostream>
using namespace std;
#include <stdio.h>
#define np 4
#define nt 3
//实现数组乘法的函数
int ArrMul(int arr1[][nt], int size1,int arr2[][1],int size2,int c[][1],int size3)
{
for (int i = 0; i<np; i++)
{
for (int j = 0; j<1; j++)
{
for (int k = 0; k < nt; k++)
{
c[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
return c[np][1];
}
int main()
{
int a[np][nt] = { 1,2,1,2,3,4,5,6,7,8,9,10 };
int b[nt][1] = { 1,1,0 };
int c[np][1] = {0,0,0,0};
ArrMul(a, nt, b, 1,c,1);
for (int i = 0; i < np; i++)
{
for (int j = 0; j < 1; j++)
{
printf("%2d", c[i][j]);
}
cout << endl;
}
system("pause");
return 0;
}