使用动态分配的数组进行矩阵乘法;结果不正确
Matrix Multiplication with Dynamically Allocated Arrays; Result Incorrect
我一直在尝试调试这段代码一段时间,但是当值位于动态分配的内存中时,我不确定如何实际访问IDE中的值。该程序应该根据用户的输入初始化两个矩阵,然后在可能的情况下将它们相乘。
我发现了一些错误并纠正了它们,但我不确定是什么导致了这个问题。
#include <iostream>
using namespace std;
typedef int* IntArrayPtr;
int main() {
int r1, c1, r2, c2;
do {
//GET DIMENSIONS OF MATRICIES
cout << "Welcome! This program takes two matricies and multiplies them together.n"
<< "Enter the number of rows and number of columns for Matrix 1: ";
cin >> r1 >> c1;
cout << "Enter the number of rows and number of columns for Matrix 2: ";
cin >> r2 >> c2;
//DETECT IF MULTIPLICATION CAN HAPPEN
if (r1 != c2) {
cout << "Error: matricies cannot be multiplied. Please enter a new set.n";
}
} while (r1 != c2); //have the user enter again if the rows and columns don't match
cout << endl;
//INTIALIZE MATRICIES USING DYNAMIC ARRAYS
//intialize MATRIX 1
IntArrayPtr *a = new IntArrayPtr[r1];
cout << "For MATRIX 1: Enter the contained values. Press enter after each entry.n";
for (int i = 0; i < r1; i++) {
a[i] = new int[c1]; //init columns for each row
cout << "ROW" << i + 1 << "n";
for (int j = 0; j < c1; j++) {
cin >> a[i][j]; //fill columns of rows
}
cout << endl;
}
//intialize MATRIX 2
IntArrayPtr *b = new IntArrayPtr[r2]; //init rows
cout << "For MATRIX 2: Enter the contained values. Press enter after each entry.n";
for (int i = 0; i < r2; i++) {
b[i] = new int[c2]; //intialize columns
cout << "ROW" << i + 1 << "n";
for (int j = 0; j < c2; j++) {
cin >> b[i][j]; //fill columns of rows
}
cout << endl;
}
//INITIALIZE MATRIX TO STORE RESULT IN
//matrix will have the rows of the first and columns of the second, according to matrix multiplication
IntArrayPtr *c = new IntArrayPtr[r1]; //init rows
for (int i = 0; i < r1; i++) {
c[i] = new int[c2]; //init columns
}
//MULTIPLY MATRICIES
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
//PRINT RESULT
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
cout << c[i][j] << " ";
}
cout << endl;
}
delete[] a; delete[] b; delete[] c;
system("pause");
return 0;
}
矩阵应该是乘法的结果,但是当我尝试使用小矩阵(例如 3 x 2 乘以 2 x 3(执行程序时,输出吐出在我看来是垃圾的东西。我确信我的错误是愚蠢的,但帮助将不胜感激。
您没有正确初始化矩阵 c。试试这个
//INITIALIZE MATRIX TO STORE RESULT IN
//matrix will have the rows of the first and columns of the second, according to matrix multiplication
IntArrayPtr *c = new IntArrayPtr[r1]; //init rows
for (int i = 0; i < r1; i++) {
c[i] = new int[c2]; //init columns
for (int j = 0; j < c2; j++) {
c[i][j] = 0;
}
}
相关文章:
- 密码登录程序将永远循环并显示不正确的结果
- 特征 LLT 模块给出不正确的结果?
- 计算幂级数的数学结果不正确
- wcslen() 在使用编译指示包时返回不正确的结果
- 使用动态分配的数组进行矩阵乘法;结果不正确
- CMake 查找特征不正确的结果
- 将函数的地址转换为UINTPTR_T给我不正确的结果
- 使用 C++ 中的运算符重载显示不正确的结果
- 简单的程序比较阵列运行但给出不正确的结果
- 在运行libtorch的模型时获得的结果是不正确的,该模型经过训练并从Pytorch出口
- 当我输入分数值时,它返回不正确的结果
- time_t提高日期转换,给出不正确的结果
- 为什么隐式类型转换在此代码中结果不正确
- 排序向量不断修改数据并打印不正确的结果
- 使用Boost Polygon的减法结果不正确
- 脑解释器在C++产生不正确的结果
- 具有OFN_ALLOWMULTISELECT不正确的快捷方式结果的 CFileDialog
- 递归阶乘函数在"22!"上产生不正确的结果
- GLM 矩阵乘法向量给出不正确的结果
- 浮点数和由于舍入行为导致的不正确结果