Read multiple scanf numbers separated by space and place in a vector in C

Asked

Viewed 107 times

0

I need to make a program that receives a lot of elements and put a vector, but I want to put the elements in the scanf separated by space and then pass in the vector. Only I don’t know how to do this, someone can help me?

  • Welcome to Stackoverflow, we recommend that at least you try to implement something regarding your need, if there are still questions, add the code so we can help you!

  • you want it to automatically calculate the amount of numbers entered? or take from a for?

  • Do you want the elements to be separated by space at the entrance? This will depend on the type of data being read, except for the conversion specification containing:[, c, C, or n, the scanf will consider one or more characters of white space ( <space>, <tab>, <novalinha>, <vertical-tab>, or <form-feed>) as a separator between the elements being read. To read the elements of an array of elements one normally uses a loop for and the loop control variable as the index of the array.

  • Couldn’t figure out what you want to do in concrete ? Start by explaining in as much detail as possible. Also include the code you have already developed as it helps you understand your issues.

1 answer

2

The question is not very clear to me, but I have no score to comment, so I will answer based on what I understand.

According to what I interpreted you want to read multiple numbers in one scanf and save in a vector.

Code:

int vetor[4];
scanf( "%i %i %i %i", &vetor[0], &vetor[1], &veto[2], &vetor[3]);

If that’s not what you want I’ll delete the answer no problem!

  • Why not: for (i=0; i<4; i++) scanf("%i", &vector[i]); ?

  • Because from the way he was asked, it seems to me he doesn’t want to use a repeating structure. Look at the question: "more I want to put the elements in the scanf separated by space and then pass in the vector", when he says: "separated by space", gave to understand (at least for me) that he wanted to read several values in only one scanf and save them in a vector.

  • I fully agree with you that the question is very poorly worded.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.