创建下对角三角形时输出错误

时间:2021-01-20 12:17:18

标签: c++

我正在尝试创建一个程序,该程序采用较低的对角线值并将其存储在一个一维数组中,然后将其打印出来。

输入:

1 0 0

2 3 0

4 5 6

预期输出

1 0 0

2 3 0

4 5 6

电流输出

2 0 0

2 4 0

4 5 6

请注意,我已经为 3 指定了维度。

代码:

#include <bits/stdc++.h>
using namespace std;
class matrix
{
    int size;
    int *a;

public:
    matrix(int size)                          //Matrix class
    {
        this->size = size;
        a = new int[(size * (size + 1) / 2)];           //creating 1-D array to store values
    }
    void set(int i, int j, int val)               //Function to set values in array "a".
    {
        if (i >= j)
            a[((i * (i - 1)) / 2) + (j - 1)] = val;
    }
    
    void display()                               //function to display values
    {
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                if (i >= j)
                    cout << a[((i * (i - 1)) / 2) + (j - 1)] << " ";
                else
                    cout << "0 ";
            }
            cout << endl;
        }
    }
};
int main()
{
    int n = 3;                                   //dimension of matrix
    matrix a(n);
    for (int i = 0; i < n; i++)                //entering the values
    {
        int val;
        for (int j = 0; j < n; j++)
        {
            cin >> val;
            a.set(i, j, val);
        }
    }
    a.display();

    return 0;
}

附加编译器:https://onlinegdb.com/IrzmB04dB

1 个答案:

答案 0 :(得分:3)

在所述矩阵的扁平数组表示(在代码中称为 i,j)中查找矩阵元素 a 的公式,a[((i * (i - 1)) / 2) + (j - 1)] 仅在索引 {{ 1}} 从 i,j 开始计数,如在 Fortran 中,而不是从 1 开始,如在 C++ 中。

此外,下三角矩阵的大小应包括约定中的主对角线,因此应为 0,并在构造函数中添加 (size * (size - 1) / 2) + size

考虑到这一点,您的代码变为:

+ size

结果输出:

#include <iostream>

using namespace std;

class matrix
{
    int size;
    int *a;

public:
    //Matrix class, ADD '+ size' TO a 
    matrix(int input_size): size(input_size), a(new int[size * (size + 1) / 2] + size) {}    

    void set(int i, int j, int val)               //Function to set values in array "a".
    {
        if (i >= j) {
            //i++; j++;                            // !!HERE!! START i,j AT 1
            //a[((i * (i - 1)) / 2) + (j - 1)] = val;
            // OR, EQUIVALENTLY
            a[((i * (i + 1)) / 2) + j] = val;
        }
    }

    void display()                               //function to display values
    {
        for (int i = 1; i <= size; i++)        // START i AT 1
        {
            for (int j = 1; j <= size; j++)    // START j AT 1
            {
                if (i >= j)
                    cout << a[((i * (i - 1)) / 2) + (j - 1)] << " ";
                else
                    cout << "0 ";
            }
            cout << endl;
        }
    }
};


int main()
{
    int n = 3;                                   //dimension of matrix
    matrix a(n);
    for (int i = 0; i < n; i++)                //entering the values
    {
        int val;
        for (int j = 0; j < n; j++)
        {
            cin >> val;
            a.set(i, j, val);
        }
    }
    a.display();

    return 0;
}