我对数组的行主顺序的尝试显示正确的值但索引不正确的值

My attempt at Row-major order of array is showing correct values but indexing incorrect values

我制作了一个名为 class 的矩阵,它将值存储在一维数组中,但将其输出为二维数组。我包含了 print 语句来显示应该放入数组中的确切值,但是当我使用 print 函数对其进行索引时,它在最后一个索引的第二行显示了不正确的值。不完全确定我做错了什么。

#include <iostream>

class Matrix
{
    private:
        int rows{}, cols{};
        double *newArr;
    public:
        Matrix(int row, int col)
        {
            rows = row;
            cols = col;
            newArr = new double[rows * cols];
        }
        void setValue(int row, int col, double value)
        {
            std::cout << value << std::endl;
            newArr[row * row + col] = value;                
        }
        double getValue(int row, int col)
        {
            return newArr[row * row + col];                 
        }
        int getRows()
        {
            return rows;
        }
        int getCols()
        {
            return cols;
        }
        void print()
        {
            for (int i{}; i < rows; ++i){
                for (int j{}; j < cols; ++j){
                    std::cout << newArr[i * i + j] << " ";
                }
                std::cout << std::endl;
            }
        }
        ~Matrix()
        {
            delete[] newArr;
        }
};

int main()
{
    Matrix x(3, 4);

    for (int i{}; i < x.getRows(); i++){
        for (int j{}; j < x.getCols(); j++){
            x.setValue(i, j, (j+i)/2.0);                     
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;

    x.print();

    return 0;
}

我更改了您的索引逻辑,看起来没问题。 仍然不明白为什么使用 row * row + col 而不是 row * cols + col.

动态分配矩阵的大小并将2d矩阵布局为1d。那么你应该使用 length 来填充数组,而不是 (row index)^2.

Live Demo

#include <iostream>

class Matrix
{
    private:
        int rows{}, cols{};
        double *newArr;
    public:
        Matrix(int row, int col)
        {
            rows = row;
            cols = col;
            newArr = new double[rows * cols];
        }
        void setValue(int row, int col, double value)
        {
            std::cout << value << std::endl;
            newArr[row * cols + col] = value;                
        }
        double getValue(int row, int col)
        {
            return newArr[row * cols + col];                 
        }
        int getRows()
        {
            return rows;
        }
        int getCols()
        {
            return cols;
        }
        void print()
        {
            for (int i{}; i < rows; ++i){
                for (int j{}; j < cols; ++j){
                    std::cout << newArr[i * cols + j] << " ";
                }
                std::cout << std::endl;
            }
        }
        ~Matrix()
        {
            delete[] newArr;
        }
};


int main()
{
    Matrix x(3, 4);

    for (int i{}; i < x.getRows(); i++){
        for (int j{}; j < x.getCols(); j++){
            x.setValue(i, j, (j+i)/2.0);                     
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;

    x.print();

    return 0;
}