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

问题描述 投票:0回答:1

我有一个家庭作业问题,需要使用存储在数组中的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++ arrays matrix visual-studio-2017 matrix-multiplication
1个回答
0
投票

我认为您可以尝试使用双循环遍历输出矩阵。

© www.soinside.com 2019 - 2024. All rights reserved.