2
It is necessary to read only a line with a space-separated set of integers, each space-separated item will be inserted in a linked list: 1 41 2 15 6 7 87 4 2 1 9 0
List would be something like = [1, 41, 2, 15, 6, 7, 87, 4, 2, 1, 9, 0]
Example:
$ gcc example.c -o example -g -Wall
$ ./example
1 41 2 15 6 7 87 4 2 1 9 0<enter>
The biggest problem is that the amount of numbers that will come in the input is undefined, ie the input can also be: 1 8 6
Lista = [1, 8, 6]
Because the amount is undefined, I thought of two possible solutions:
- to)
scanf ("%[^\n]%*c", sequenciaNumeros);
- b) Use
fgets
In both cases, I would have to the problem of declaring a huge character vector to ensure it does not reach the limit.
If the quantity were set it would be simple:
scanf("%d %d %d, &valor1, &valor2, &valor3)
It is possible with the scanf
take this line and be able to manipulate each of the numbers (which are separated by space) to add to the list, knowing the amount of numbers may vary? The input is only one row of a set of numbers separated by space
has other possibilities, such as making 2 readings, one counting the items, and the other allocating and capturing.
– Bacco
@Bacco It is necessary to do the reading at once, it is not possible to count and then read.
– Daniela Morais
Is it a rule of utterance? It would be good to put in question tb. About what you said about allocating, you don’t need to allocate a huge space, you can go allocating with leftovers, and relocating if it doesn’t fit. 1-in-1 is a lot of silly allocation, but you don’t have to predict that they all fit.
– Bacco
@Bacco The statement has several pages and rules, will end up escaping the scope of the question. The entry must be like this with only one line.
– Daniela Morais
You run the risk of someone responding and not playing by the rules, and then people waste their time and not serve you. It would be nice to see if there’s no more "prank" like that, and at least mention it. If I stopped to help you with code and you told me about it later, I imagine I’d be very unhappy. It can happen to other people. If you describe the important details all, you will get better answers.
– Bacco
The question is not good, perhaps because it has an artificial requirement that does not help to have a good code. That is why the answers so far command to do otherwise than what is stated (at least what you can understand about it). The answers are good to solve the problem the way everyone would solve it, they just don’t solve it the way asked by the question, which is a weird way.
– Maniero
@bigown I edited the question again. I don’t know if the input way is hard to be manipulated or I’m used to simple entries.
– Daniela Morais
I think the question is clear, the requirement is that it doesn’t make much sense, as Bacco said. I think that because of this the people who decided to answer preferred not to pay attention to him because they were something out of the ordinary and were in what is intuitive to do.
– Maniero
In fact, the fact that the input is in "one line" does not change anything when the reading is done using
scanf
.– richardaum
I don’t know if you understood what I called two readings. I speak two readings inside the typed string once. The first counting how many numbers, and the second reading the values. Depending on what technique is used to read, of course. It only makes sense to treat the line as one thing.
– Bacco
"Interpreting" the requirements, what is probably being asked is something like this: "read a sequence of integer numbers separated by spaces etc...". In the terminal, in the example above, after <enter> (or instead of <enter>) there would be a control-d or control-z to finish reading the integers.
– zentrunix