Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong output when creating a lower diagonal triangle

Tags:

c++

I am trying to create a program that takes lower diagonal values and than it stores in a 1-D array and then just print it .

Input:

1 0 0

2 3 0

4 5 6

expected output

1 0 0

2 3 0

4 5 6

current output

2 0 0

2 4 0

4 5 6

note I have given dimension to 3 already.

code:

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

Attached compiler: https://onlinegdb.com/IrzmB04dB

like image 816
BATMAN_X Avatar asked Mar 31 '26 17:03

BATMAN_X


1 Answers

Your formula to find a matrix element i,j in a flattened array representation (that you call a in your code) of said matrix, a[((i * (i - 1)) / 2) + (j - 1)] is only correct in case indices i,j start counting from 1, as in Fortran, and not from 0, as in C++.

Also, the size of lower triangular matrix should include the main diagonal in your convention, so it should be (size * (size - 1) / 2) + size with the added + size in the constructor.

Minding about this, your code becomes:

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

Resulting output:

1
0
0
2
3
0
4
5
6
1 0 0
2 3 0
4 5 6
like image 114
Giogre Avatar answered Apr 02 '26 12:04

Giogre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!