如何使用C++中的数组进行矩阵数学运算
How to do Matrix Math operations using arrays in C++
我有一个家庭作业问题,需要使用存储在数组中的 4x4 矩阵(由用户输入值(,我应该对它们进行一些不同的数学运算,以及能够显示和转置它们。我在矩阵加法和乘法的逻辑上遇到了困难,特别是我如何在矩阵中选择特定索引来进行操作。我也很难弄清楚如何显示矩阵,这就是为什么我的 displayMatrix 函数是空白的。我也在努力弄清楚如何将我的用户输入存储在 for 循环中的不同矩阵中。
我不完全确定如何使我的代码为我工作,并希望任何人对我的代码提供任何解决方案或其他建议!
#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
const int SIZE = 4;
const int SLICES = 25;
void addMatrices(int matrix1[][SIZE], int matrix2[][SIZE], int result[][SIZE],
int matricesUsed, string prompt);
void displayMatrix(int matrix1[][SIZE], int result[][SIZE]);
int main()
{
int matrix1[SIZE];
int matricesUsed = 0;
string prompt = "Enter the number of matrices you want to use (between 1 & 25): ";
int initialMatrices;
cout << prompt;
cin >> initialMatrices;
if (initialMatrices <= 0 || initialMatrices > 25)
{
cout << "Invalid number of matrices. Please enter a number between 1 and 25." << endl;
cout << "Enter the number of matrices you want to use (between 1 & 25): ";
cin >> initialMatrices;
}
matricesUsed = initialMatrices - 1;
for (int i = 0; i < initialMatrices; i++)
{
for (int i = 0; i < SIZE; i++)
{
cout << "Enter the value for position " << i + 1 << ": ";
cin >> matrix1[i];
}
}
matricesUsed++;
void addMatrices(int matrix1[][SIZE], int matrix2[][SIZE], int result[][SIZE], int matricesUsed, string prompt)
{
int firstIndex = getIndex(matricesUsed, prompt);
int firstIndex = getIndex(matricesUsed, prompt);
addMatrices(matrix1[], matrix[], result[matricesUsed]);
displayMatrix(matrix[matricesUsed]);
matricesUsed++;
}
void displayMatrix()
{
}
预期的输出是一个 4x4 矩阵,它已收到用户指定的运算符(我省略了这部分代码,因为它工作正常,但如果我也需要上传它,请告诉我!
程序的输出应如下所示:
How many initial matrices? 2
Enter matrix 1:
Row 1? 2 4 0 1
Row 2? 3 0 1 2
Row 3? 1 0 1 -1
Row 4? 0 1 2 0
Enter matrix 2:
Row 1? 1 0 0 0
Row 2? 0 1 0 0
Row 3? 0 0 1 0
Row 4? 0 0 0 1
Operation? +
First matrix for +? 1
Second matrix for +? 2
Result is matrix 3:
Row 1: 3 4 0 1
Row 2: 3 1 1 2
Row 3: 1 0 2 -1
Row 4: 0 1 2 1
我认为您可以尝试使用双循环来遍历输出矩阵。
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int matrix_1[4][4];
cout << "Please enter the value of the matrix:" << endl;
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
cin >> matrix_1[i][j];
}
}
printf("The matrix_1 is:n");
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
printf("%8d", matrix_1[i][j]);
}
printf("n");
}
int matrix_2[4][4];
cout << "Please enter the value of the matrix:" << endl;
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
cin >> matrix_2[i][j];
}
}
printf("The matrix_2 is:n");
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
printf("%8d", matrix_2[i][j]);
}
printf("n");
}
int matrix_3[4][4];
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
matrix_3[i][j] = 0;
}
}
cout << "operation:*" << endl;
for (auto i = 0; i < 4; i++) //The number of rows in matrix 1 is 4.
{
for (auto j = 0; j < 4; j++)//The number of columns in matrix 2 is 4.
{
for (auto k = 0; k < 4; k++) //The number of columns in matrix 1 is 4.
{
matrix_3[i][j] += matrix_1[i][k] * matrix_2[k][j];
}
}
}
cout << "The value of the output matrix:" << endl;
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
printf("%8d", matrix_3[i][j]);
}
printf("n");
}
int matrix_4[4][4];
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
matrix_4[i][j] = 0;
}
}
cout << "operation:+" << endl;
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
matrix_4[i][j] += matrix_1[i][j] + matrix_2[i][j];
}
}
cout << "The value of the output matrix:" << endl;
for (auto i = 0; i < 4; i++)
{
for (auto j = 0; j < 4; j++)
{
printf("%8d", matrix_4[i][j]);
}
printf("n");
}
return 0;
}
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- GCC本机矩阵运算库
- 位阵列上的快速AND运算
- 字符串中int的加法运算
- 算术运算的结果类似于:C浮点变量中的1/3
- 如何使用OpenMP并行化此矩阵时间矢量运算
- 如何在两个 boost::multi_arrays (C++) 之间执行数学运算?
- 如何在 vtk Poly 数据上进行布尔运算?
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 避免指针运算,修复叮当整齐错误
- 使用双精度的浮点运算
- 如何在 c++ 中使用带有数学运算的引用/指针?
- 更快的C++算术运算
- 是否存在用于 C 或 C++ 中常见数学运算(例如最小值、最大值和平均值)的可导入库?
- 为什么循环体中的一个基本算术运算执行得比两个算术运算慢
- 两个字符串之间的数学运算
- 为什么对无符号字符进行算术运算会将它们提升为有符号整数
- 何时使用按位运算而不是算术替代方法?
- Arduino 上数学运算的计时速度 - 异常
- 如何使用按位运算将随机uint64_t转换为范围 (0, 1) 的随机双精度