如何使用C++中的数组进行矩阵数学运算

How to do Matrix Math operations using arrays in C++

本文关键字:运算 C++ 何使用 数组      更新时间:2023-10-16

我有一个家庭作业问题,需要使用存储在数组中的 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;
}