Matrix from numbers

Hi, I need help for this matrix.I'm trying to do but I can't think of solution how to do the middle part.Have to look like this...

(0 0 0 0 0 0 0 0 0 0)
(0 1 0 0 0 0 0 0 0 0)
(0 0 2 0 0 0 0 0 0 0)
(0 0 0 3 0 0 0 0 0 0)
(0 0 0 0 4 0 0 0 0 0)
(0 0 0 0 0 5 0 0 0 0)
(0 0 0 0 0 0 6 0 0 0)
(0 0 0 0 0 0 0 7 0 0)
(0 0 0 0 0 0 0 0 8 0)
(0 0 0 0 0 0 0 0 0 9)

I do not know how to fill the other side and I think there is a better way to solve this problem, or the entire matrix.If you can please paste code.
Get this far for now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>

using namespace std;

int main()
{
    int matrix[10][10];
    int a = 0;

    for(int x=0;x<10;x++)
    {
        for(int y=0;y<10;y++)
        {
            matrix[x][y];
        }
    }

    for(int x=0;x<10;x++)
    {

        for(int y=0;y<x;y++)
        {

            cout<<matrix[x][y] << " ";

        }
        cout << a;

    cout<<endl;  
    a++;
    }
    return 0;
}

Last edited on
1. Avoid global variables. There is no reason for the "matrix" to be outside of the main().

2. You have to assign a value to each element on line 15. Use a conditional to test whether an element is on the diagonal or not.

Hint: ternary operator ? :

Hint: With http://stackoverflow.com/questions/629017/how-does-array100-0-set-the-entire-array-to-0 you don't need nested loops for setting the diagonal values.
1.Done.
2.What is the condition for checking the diagonal elements.
Could you write me some code with comments of steps that you do...i'll be so grateful!
The basic algorithm would be like this:

1
2
3
4
5
6
For each row
    output (
    for each column
        if column == row output row
        else output 0
    output )



EDIT:

What is the condition for checking the diagonal elements
With a square that would mean that the column and row are the same.

Could you write me some code with comments of steps that you do...i'll be so grateful!
I tried to give you pseudo code without giving you code. Since giving pseudo code was pretty much the answer anyways. Though as keskiverto mentioned you can use ? : (ternary operator) instead of if/else


The alternative solution would be:

1
2
3
assign all values to 0 --you can use default initialization with int arrays by assign {} or {0}.
for each row (column and row will be same in a square)
    assign array[row][column] the row
Last edited on
I know there's a much easier way to build this program, it would be better someone to show the easier way.
I did it this way and it's working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;

int main()
{
    int matrix[10][10];
    int a = 0;
    int q = 9;

    
    for(int x=0;x<10;x++)
    {
        for(int y=0;y<10;y++)
        {
            matrix[x][y] = {};
        }
    }
    

    for(int x=0;x<10;x++)
    {

        cout << "( ";

        for(int y=0;y<x;y++)
        {


            cout<<matrix[x][y] << " ";
        }


    cout << a << " ";
    for(int z=0;z<q;z++)
        {
            if(matrix[x]!=matrix[q])
            {
                matrix[x][z]={0};
                cout << matrix[x][z] << " ";
            }
        }
        cout << ")";
    cout << endl;
    a++;
    q--;
    }

    return 0;
}
Last edited on
I decided to write more simple solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

int main()
{
    int matrix[10][10];

    for(int row=0;row<10;row++)
    {
        cout << "( ";

        for(int col=0;col<10;col++)
        {
            if(row==col)
            {
                cout << row << " ";
            }
            else cout << "0 ";
        }
        cout << ")" << endl;
    }
    return 0;
}


I think it's good and much simple than the last one.
Thank you so much: giblit and keskiverto!!!!
You really helped me to understand this...thanks again.
Last edited on
The second one is very similar to the first algorithm I mentioned. The second one would look like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    std::size_t const size = 10u; //size = rows and columns (since it is a square)

    int matrix[size][size] = {}; //initialize all to 0

    for(int row = 0; row < size; ++row) //row and column will be same since diagonal
    {
        matrix[row][row] = row;
    }


    //output if you want

    return 0;
}



Edit with your code earlier,
1
2
3
4
5
            if(row==col)
            {
                cout << row << " ";
            }
            else cout << "0 ";
could be std::cout << (row == col ? row : 0) << ' ';
Last edited on
Topic archived. No new replies allowed.