简体   繁体   English

C 程序,让用户在一行中键入多个动态输入

[英]C program, make user to type multiple dynamic inputs in one line

How can I take one line dynamic input like this -> 1 4 3 5 of course, It can be done with this approach ->我怎样才能像这样采用一行动态输入 -> 1 4 3 5 当然,可以用这种方法来完成 ->

scanf("%d %d %d %d", &variable[0], &variable[1], &variable[2], &variable[3]);

In this example, I am taking only 4 inputs...在这个例子中,我只接受 4 个输入......

But my question is, how can I make it dynamic and work for a random number?但我的问题是,如何让它动态化并为随机数工作?

// Sample Input
3 // number of rows
5 // number of columns

// 2d array with dynamic input
1 4 3 5 97
5 6 7 4 51
2 9 8 3 0

Use a loop to read each array entry.使用循环读取每个数组条目。

int rows, cols;
scanf("%d %d", &rows, &cols);
int variable[cols];

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        scanf("%d", &variable[j]);
    }
    // do something with the variable array
}

Note that scanf() doesn't make a distinction between different types of whitespace that separate tokens.请注意, scanf()不会区分分隔标记的不同类型的空白。 So this will read all the numbers on the same line, different lines, or a mixture.所以这将读取同一行、不同行或混合的所有数字。

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

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