简体   繁体   English

使用用户输入打印 6X10 矩阵的代码 - C++

[英]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]我正在尝试编写一个 6x10 矩阵,其中填充了用户输入的值,如下所示:用户输入 6 个不同的值,代码打印 6x10 矩阵如下:[示例]

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.所有空格都用“--”填充,并且值以递增的方式插入,就好像矩阵遵循 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.您应该使用嵌套for()循环。 This would output the matrix, and if you include an if() statement, you will be able to see if the number should be output.这将是 output 矩阵,如果包含if()语句,您将能够查看数字是否应为 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 .最简单的方法是使用bool矩阵,如果用户输入该数字,则将该地址处的bool更改为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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