乘法阵列
Multiplying arrays
本文关键字:阵列 更新时间:2023-10-16
如何让矩阵重载构造函数将向量a和向量b组合起来,形成一个作为外积的矩阵对象。对Vector c和Vector d也要这样做。问题是构造函数过载,并且能够使用它创建矩阵。目前,它只能在需要使用向量a和向量b时使用它们。打印成员功能需要根据用户输入的值打印矩阵。
#include <iostream>
using namespace std;
const int rows=3;
const int columns=3;
const int elements=3;
class Vector{
private:
double data[elements];
public:
Vector();
void read();
double get_element(int);
};
Vector::Vector(){
int i=0;
while(i<elements){data[i++]=0;}
}
void Vector::read(){
int j=0;
cout<<"Enter "<<elements<<" elements of vector "<<endl;
while(j<elements){cin>>data[j++];}
}
double Vector:: get_element(int n){
while(n<elements)
return data[n];
}
Vector a,b,c,d;
class Matrix {
private:
double data [rows*columns];
public:
Matrix(Vector &, Vector &);
void add (const Matrix &);
void mult (double);
double trace();
double norm();
void print ();
};
Matrix::Matrix(Vector &, Vector &){
int d,f;
for (d=0; d<rows; d++){
for (f=0; f<columns;f++){
data[d*f]=a.get_element(d)*b.get_element(f);
}
}
}
Matrix A (a, b);
Matrix B (c, d);
void Matrix::print(){
cout.setf(ios::showpoint|ios::fixed);
cout.precision(3);
for (int i=0; i<rows; i++) {
cout << endl;
for (int j=0; j<columns; j++) {
cout << " " << data[i*j];
}
}
}
首先,不要使用命名空间std。您不希望仅为了使用cout和cin而将所有std
导入到类中。
现在让我们开始学习基础知识。您有一个类,它应该封装/保存自己的数据。这个类可以传递和处理。每个类都有自己的向量/矩阵类的唯一数据集。
在上面的例子中,您有使用全局变量的类。这很糟糕,因为每个向量都有相同的精确数据。它们共享相同的变量(全局)!
因此,我们需要以某种方式让Vector包含它自己的数据。我们通过将变量放入向量类本身来实现这一点。我们将其设为私有,这样就不能在类之外访问它。
接下来,我们需要一种初始化data
的方法。我们不能再给它elements
了,因为元素不再是全局的和恒定的。因此,我们现在必须在构造函数中动态分配elements
数量的doubles,并在析构函数中删除它。
为了防止现在出现奇怪的行为,我们禁止复制和分配。你真的应该看看关于类和封装的教程。。这个答案很不完整,但应该会有所帮助,并解决一些问题。。
如果您100%确定向量必须只有3个元素,不多也不少,那么您可以将double* data
更改为double data[3]
,并从构造函数中删除new data[...]
,从析构函数中删除delete[] data
。
#include <iostream>
class Vector
{
private:
double* data; //will point to an array of elements.
int elements; //amount of elements this vector has.
Vector(const Vector& other); //copying not allowed.
Vector& operator = (const Vector& other); //copy assignment not allowed.
public:
Vector(int elements); //constructor that tells us how large of an array we need.
~Vector(); //destructor to delete the dynamically allocated array when done.
int size() const; //returns the amount of elements.
double get_element(int n) const;
void set_element(double value, int index);
};
//This is our constructor. It stores the amount of elements we allocated within our class.
//It also initialises data to point to the `new` array.
Vector::Vector(int elements_) : elements(elements_), data(new double[elements_]())
{
}
Vector::~Vector()
{
delete[] data; //before the class gets destroyed, we clean up our dynamically allocated array. ALWAYS!
}
double Vector::get_element(int n) const
{
return data[n];
}
void Vector::set_element(double value, int index)
{
data[index] = value;
}
int Vector::size() const
{
return elements;
}
/** We do the same for the matrix class.**/
class Matrix
{
private:
double* data;
int rows, columns; //instead of elements, we have rows and columns.
Matrix (const Matrix &other); //prevent copying.
Matrix& operator = (const Matrix &other); //prevent assignment.
public:
Matrix(const Vector &a, const Vector &b); //constructor takes TWO vectors.
~Matrix();
void add (const Matrix&);
void mult (double);
double trace();
double norm();
void print ();
};
/** Data is initialized to an array of doubles[rows * columns] **/
/** We also store the amount of rows and columns allocated **/
Matrix::Matrix(const Vector &a, const Vector &b) : data(new double[a.size() * b.size()]), rows(a.size()), columns(b.size())
{
int d, f;
for (d = 0; d < rows; d++)
{
for (f = 0; f < columns; f++)
{
data[d * f] = a.get_element(d) * b.get_element(f);
}
}
}
Matrix::~Matrix()
{
delete[] data; //Before the class is destroyed, we must delete the array.
}
void Matrix::print()
{
std::cout.setf(std::ios::showpoint | std::ios::fixed);
std::cout.precision(3);
for (int i = 0; i < rows; i++)
{
std::cout << std::endl;
for (int j = 0; j < columns; j++)
{
std::cout << " " << data[i * j];
}
}
}
int main()
{
Vector a(3); //Now we can a vector.
Vector b(3); //another vector.
Vector c(2); //notice we can choose how many elements the vector can hold.. 2 elements = a point. Nevertheless..
a.set_element(5.0d, 0);
b.set_element(10.0d, 2);
Matrix m(a, b); //Create a matrix from our two vectors.
m.print(); //Print it..
}
如前所述,如果你100%确定所有矩阵都是3x3,并且所有向量只有3个元素,那么下面是类的样子:
#include <iostream>
class Vector
{
private:
static const int elements = 3; //static. All vector instances will have the same amount of elements.
double data[elements]; //stack allocation. 3 elements only.
Vector(const Vector& other);
Vector& operator = (const Vector& other);
public:
Vector();
~Vector();
void read();
int size() const;
double get_element(int n) const;
};
Vector::Vector() : data() {} //nothing in the constructor. data is filled with 0's.
Vector::~Vector() {} //nothing to delete in the destructor..
double Vector::get_element(int n) const
{
return data[n];
}
int Vector::size() const
{
return elements;
}
class Matrix
{
private:
static const int rows = 3, columns = 3; //all matrix instances will have 3 rows, 3 columns.
double data[rows * columns];
Matrix (const Matrix &other);
Matrix& operator = (const Matrix &other);
public:
Matrix(const Vector &a, const Vector &b);
~Matrix();
void add (const Matrix&);
void mult (double);
double trace();
double norm();
void print ();
};
Matrix::Matrix(const Vector &a, const Vector &b) //nothing to allocate in the constructor.
{
for (int d = 0; d < rows; d++)
{
for (int f = 0; f < columns; f++)
{
data[d * f] = a.get_element(d) * b.get_element(f);
}
}
}
Matrix::~Matrix() {} //nothing to delete in the destructor.
void Matrix::print()
{
std::cout.setf(std::ios::showpoint | std::ios::fixed);
std::cout.precision(3);
for (int i = 0; i < rows; i++)
{
std::cout << std::endl;
for (int j = 0; j < columns; j++)
{
std::cout << " " << data[i * j];
}
}
}
int main()
{
Vector a;
Vector b;
Matrix m(a, b);
m.print();
}
相关文章:
- OpenMP阵列性能较差
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 位阵列上的快速AND运算
- 阵列必须使用大括号封闭的初始器进行初始化
- 没有从阵列<float>到阵列<int>的可行转换
- C++动态安全 2D 交错阵列
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 库特<<恩德尔;不适用于打印 2D 阵列
- 数组类 阵列的打印输出
- 从较小的阵列到较大的阵列的元素级转换
- 仅在大型阵列上出现合并排序分段错误
- 从阵列C++中删除重复项
- 阵列的大小在C++中是否灵活?
- 反转阵列中的正序列
- 具有可变尺寸的 C++ 2D 阵列
- 二维阵列的动态分配
- 重新增长阵列时出错:写入时缓冲区溢出