简体   繁体   中英

Code to Print a 6X10 Matrix with user inputs - C++

I am trying to program a 6x10 matrix, which is filled with the values entered by the user as follows: The user enters 6 different values, and the code prints the 6x10 matrix as follows: [example]

Image showing the expected result for the code

All blank space is filled with a "--" and the values are inserted in an increasing way as if the matrix followed the order 1, 2, 3, 4, 5... 60.

My question are: How do I print a matrix with user values like the image above?

Thanks in advance

You should use a nested for() loop. This would output the matrix, and if you include an if() statement, you will be able to see if the number should be output.

//Row
for (int i = 0; i < 6; ++i)
{
    //Column
    for (int j = 0; j < 10; ++j)
    {
        //If there is a number in the matrix entry, display it. If not, display '--'
        if (my_matrix[i][j] != 0)
        {
            std::cout << my_matrix[i][j] << " ";
        }
        else
        {
            std::cout << "-- ";
        }
    }
    //Start a new line
    std::cout << std::endl;
}

The easiest way to do this would be to have a matrix of bool , and if the user inputs that number, change the bool at that address to true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM