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

Time:03-01

I have made a class called matrix that stores values in a 1D array, but outputs it as a 2D array. I have included print statements to show the exact values supposedly being put into the array however when I use the print function to index it, it shows incorrect value on the second row last index. not entirely sure what I'm doing wrong.

#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;
}

CodePudding user response:

I changed your indexing logic and it seems okay. Still not getting why you use row * row col instead of row * cols col.

Dynamic allocated the size of the matrix and layout the 2d matrix into 1d. Then you should use the length to fill the array, not (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;
}
  • Related