【问题标题】:How to do Matrix Math operations using arrays in C++如何在 C++ 中使用数组进行矩阵数学运算
【发布时间】: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


【解决方案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;
}

【讨论】:

  • 我已经根据矩阵乘法的工作原理重新编辑了代码。
猜你喜欢
  • 2011-07-27
  • 2021-12-27
  • 2019-07-03
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
相关资源
最近更新 更多