【发布时间】:2017-11-16 22:09:58
【问题描述】:
我正在尝试使用 clear 函数与随机生成的值进行矩阵乘法。因此,当矩阵作为参数发送时,我希望使用一个函数(mat_def)生成矩阵,并使用另一个函数(mat_mul)将它们相乘。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
double mat_def(int n) //how to return the matrix
{
double a[n][n];
double f;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
f= rand();
cout<<f ;
a[i][j]=f;
}
}
return 0;
}
double mat_mul( int n, double a[n][n], double b[n][n]) //how to send matrix as parameter
{
return 0;
}
int main()
{
/* initialize random seed: */
srand (time(NULL));
mat_def(10);
}
【问题讨论】:
-
您的问题是什么?你想知道如何将两个矩阵相乘或其他什么吗?
-
在 C++ 中实现 Matrix 类的方法有很多。经典地,查找重载
::operator[]并返回对内部类的引用,该内部类也重载::operator[],因此您可以使用实际的矩阵语法。 -
给您的问题:矩阵的大小在编译时是否已知?矩阵是否应该只是方阵?
标签: c++ matrix matrix-multiplication