⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrixmultiply2.cpp

📁 datastucutre and algorithms, application, in C
💻 CPP
字号:
// multiply compatible matrices

#include <iostream>
#include "make2dArrayNoCatch.h"

using namespace std;

template<class T>
void matrixMultiply(T **a, T **b, T **c, int m, int n, int p)
{// Multiply the m x n matrix a and the n x p matrix b
 // to get c.
   for (int i = 0; i < m; i++)
      for (int j = 0; j < p; j++)
      {
         T sum = 0;
         for (int k = 0; k < n; k++)
            sum += a[i][k] * b[k][j];
         c[i][j] = sum;
      }
}

int main()
{
   int **a, **b, **c;
   make2dArray(a,2,2);
   make2dArray(b,2,2);
   make2dArray(c,2,2);

   a[0][0] = 1; a[0][1] = 2; a[1][0] = 3; a[1][1] = 4;
   b[0][0] = 5; b[0][1] = 6; b[1][0] = 1; b[1][1] = 2;

   cout << "The first matrix is" << endl;
   cout << a[0][0] << ' ' << a[0][1] << endl;
   cout << a[1][0] << ' ' << a[1][1] << endl;

   cout << "The second matrix is" << endl;
   cout << b[0][0] << ' ' << b[0][1] << endl;
   cout << b[1][0] << ' ' << b[1][1] << endl;

   matrixMultiply(a,b,c,2,2,2);

   cout << "Their product is" << endl;
   cout << c[0][0] << ' ' << c[0][1] << endl;
   cout << c[1][0] << ' ' << c[1][1] << endl;

   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -