【发布时间】:2020-02-05 07:35:29
【问题描述】:
我有一个作业问题,需要使用存储在数组中的 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
【问题讨论】:
-
第一个错误——函数中不能有函数。
-
好像使用了很多未定义的变量。你应该遵循 C++ 的规则。
标签: c++ arrays matrix visual-studio-2017 matrix-multiplication