矩阵乘法模型的问题
在过去的几个小时里,我一直在尝试构建一个 C++ 模块,在请求用户输入 2 个矩阵的大小和内容(仅限于可以相乘的矩阵)时,然后将它们相乘并返回第三个答案矩阵的值。虽然矩阵输入命令似乎基于彻底的测试工作,但我似乎无法从乘法命令中得到正确的答案,尽管了解了算法的每个步骤以及它如何对应于实际的矩阵乘法。答案矩阵的第一个值是正确的,但由于某些因素,所有后续值都是不正确的。有谁知道可能出了什么问题吗?
#include <iostream>
#include <cmath>
using namespace std;
int r, c, a1, a2, b1, b2; //defines row and column indices
double m[1][1], m2[1][1], a[1][1]; //initializes matrices
double b;
int inflag = true;
int repflag = false;
void defmatrix() { //Defines the matrix size of the first inputted matrix
cout << "Matrix Rows: ";
cin >> r;
cout << "Matrix Columns: ";
cin >> c;
}
void fillmatrix() { //Fills the matrix with automatic or user-inputted values
if (inflag == true) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << "Number in row " << i + 1 << " and column " << j + 1 << ": ";
cin >> b;
m[i][j] = b;
}
}
} else {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
m[i][j] = 1;
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << m[i][j] << " ";
}
}
}
void matrixmult() { //Multiplication function for matrix math
if (repflag == false) {
cout << "\n" << "Your second matrix will have " << c << " rows" << "\n";
b1 = c;
a1 = r;
r = c;
cout << "Second matrix columns: ";
cin >> c;
a2 = c;
double m2[r][c] = {};
if (inflag == true) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << "Number in row " << i + 1 << " and column " << j + 1 << ": ";
cin >> b;
m2[i][j] = b;
}
}
} else {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
m2[i][j] = 1;
}
}
}
a[a1][a2];
for (int i = 0; i < a1; i++) {
for (int j = 0; j < a2; j++) {
b = 0;
for (int d = 0; d < b1; d++) {
b = b + (m[i][d] * m2[d][j]);
}
a[i][j] = b;
}
}
for (int i = 0; i < a1; i++) {
for (int j = 0; j < a2; j++) {
cout << a[i][j] << " ";
}
}
}
}
int main() { //main file
defmatrix();
double m[r][c] = {};
fillmatrix();
matrixmult();
}
提前致谢!
For the past few hours I have been trying to build a C++ module that, upon requesting the input of the user on the size and contents of 2 matrices (restricted to ones that can be multiplied together), then proceeds to multiply them together and return the values of a third answer matrix. While the matrix input commands seem to work based on thorough testing, I can't seem to get a correct answer from my multiplication command, despite going through each step of the algorithm and how it corresponds to actual matrix multiplication. The first value of the answer matrix is correct, and then all succeeding values are incorrect by some factor. Does anyone have any insight as to what could be going wrong?
#include <iostream>
#include <cmath>
using namespace std;
int r, c, a1, a2, b1, b2; //defines row and column indices
double m[1][1], m2[1][1], a[1][1]; //initializes matrices
double b;
int inflag = true;
int repflag = false;
void defmatrix() { //Defines the matrix size of the first inputted matrix
cout << "Matrix Rows: ";
cin >> r;
cout << "Matrix Columns: ";
cin >> c;
}
void fillmatrix() { //Fills the matrix with automatic or user-inputted values
if (inflag == true) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << "Number in row " << i + 1 << " and column " << j + 1 << ": ";
cin >> b;
m[i][j] = b;
}
}
} else {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
m[i][j] = 1;
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << m[i][j] << " ";
}
}
}
void matrixmult() { //Multiplication function for matrix math
if (repflag == false) {
cout << "\n" << "Your second matrix will have " << c << " rows" << "\n";
b1 = c;
a1 = r;
r = c;
cout << "Second matrix columns: ";
cin >> c;
a2 = c;
double m2[r][c] = {};
if (inflag == true) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << "Number in row " << i + 1 << " and column " << j + 1 << ": ";
cin >> b;
m2[i][j] = b;
}
}
} else {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
m2[i][j] = 1;
}
}
}
a[a1][a2];
for (int i = 0; i < a1; i++) {
for (int j = 0; j < a2; j++) {
b = 0;
for (int d = 0; d < b1; d++) {
b = b + (m[i][d] * m2[d][j]);
}
a[i][j] = b;
}
}
for (int i = 0; i < a1; i++) {
for (int j = 0; j < a2; j++) {
cout << a[i][j] << " ";
}
}
}
}
int main() { //main file
defmatrix();
double m[r][c] = {};
fillmatrix();
matrixmult();
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几乎每一行都有问题。更容易指出好的部分。计算矩阵乘积的中央 for 循环似乎没问题。大多数其他内容都需要被丢弃并重写。这包括所有声明和所有函数接口。
这是我开始编写程序的方式。
现在我们需要声明矩阵,但问题来了。人们会天真地想写一些类似的东西
,但是可惜,这个不是合法的 C++。该声明可能适用于您的编译器,也可能不适用于您的编译器。有多种方法可以正确声明矩阵。最简单的就是为每个保留固定数量的内存,只使用一部分:
当然你需要检查用户提供的大小不超过10。
另一种方法是使用C++版本的变长数组,称为[向量]。
a
的声明将如下所示:使用第二种方法,对矩阵大小没有真正的限制。
这两种方法都不足以适用于任何真正的软件,但它们足以完成您的 C++ 练习。真正的软件使用更高级的 C++ 概念(例如类)来定义矩阵。只是需要注意一些事情。
我不会继续以同样的详细程度来写它。这是你的任务。我将仅展示
main
的其余部分可能是什么样子。注意使用参数的函数调用。实现这些功能,你就可以开始了。请注意,如果您选择向量,则需要通过引用传递一些参数。在这种情况下,您也可以这样编写函数:
但这是改天再谈的。
There is a problem in almost every line. It is easier to point out the good parts. The central
for
loop that calculates the matrix product seems OK. Most everything else needs to be thrown out and rewritten. This includes all declarations and all function interfaces.Here's how I would start writing the program.
Now we need to declare the matrices, but here comes a problem. One would naïvely want to write something like
but alas, this is not legal C++. This declaration may work with your compiler, or it may not. There are several ways to declare the matrices correctly. The simplest one is to reserve a fixed amount of memory for each one, and just use a portion:
Naturally you need to check that the sizes provided by the user do not exceed 10.
Another method is to use the C++ version of variable length array, called [vector]. The declaration of
a
will look something like this:With the second method, there is no real limitation on the matrix sizes.
Neither method is adequate for any real software, but they are enough to complete your C++ exercise. Real software uses more advanced C++ concepts like classes to define matrices. Just something to be aware of.
I will not continue writing it at the same level of detail. This is your task. I will just show what the rest of
main
could look like.Note function calls using arguments. Implement these functions, and you are good to go. Note, if you choose vectors, you will need to pass some arguments by reference. In this case you could also write the functions this way:
but this is a talk for another day.
尽管您要求在代码中找到错误,但由于您的任务很有趣,但我还是决定为您实现自己的解决方案。即使这是不可接受的答案,它仍然可能对教育目的有用。
我以类
矩阵
的形式制作了代码,该代码支持两个匹配矩阵(例如a *= b;
)之间的乘法操作。与您一样,我还实现了两种方法.input()
和.output()
,它们相应地从std :: cin
和输出中读取矩阵到std :: Cout
。作为奖励,还有一个方法
.at(i,j)
,它返回带有界限检查矩阵元素的引用,类似于std :: vector
。当然可以将类扩展到许多其他矩阵操作(例如加法
a += b;
或减法a- = b;
),但是我只将我的类限于这些事情您在原始代码中使用的。要使用我的课程做您想做的事情很简单,因为少数代码行(
main()
以下代码段的函数进一步位于代码片段):完整代码:
在线尝试!
输入:
输出:输出:
Although you're asking to find mistakes in your code, as your task is interesting I decided to implement my own solution from scratch for you. Even if it is unacceptable answer, still it might be useful for educational purpose.
I made code in a form of class
Matrix
that supports multiplication operation between two matching matrices likeA *= B;
. As you did, I also implemented two methods.Input()
and.Output()
that correspondingly read matrix fromstd::cin
and output tostd::cout
.As a bonus there is also a method
.At(i, j)
that returns reference to element of a matrix with bounds checking, similar tostd::vector
.Sure class can be extended to many other matrices operations (like addition
A += B;
or subtractionA -= B;
), but I limited my class to only those things that you used in your original code.To do what you wanted using my class is simple as few lines of code (that are located further in
main()
function of following code snippet):Full code:
Try it online!
Input:
Output: