Need help on writing a 4x4 matrix code

Hello. Can you help me out telling me how to print a 4x4 matrix? This is how the matrix should look
0 - - -
+ 0 - -
+ + 0 -
+ + + 0

I guess it is asking to make the code like if row>column then output +, if column>row output - and if row=column then output 0. Missed a few classes now im kinda confused.
Last edited on
Try two nested loops. A simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
for(int y = 0; y < 4; ++y) //Loop through the y locations
{
    for(int x = 0; x < 4; ++x) //Loop through the x locations
    {
        if(x > y) //If the column is greater than the row
            std::cout << "- ";
        else if(x == y) //If the column is equal to the row
            std::cout << "0 ";
        else //Else, if the row is greater than the column
            std::cout << "+ ";
    }
    std::cout << std::endl; // Print a newline, advance to the next line
}


You use two loops to go through all x and y coordinates, then depending on the relation between the two, you print the right character.
Thanks a lot man.
Topic archived. No new replies allowed.