Read multiple numbers of scanf separated by space

-3

I need to make a program that receives a number of elements of a vector, and then each element of that vector, but in the same% with spaces. Only I do not know how to do this, someone can help me.

Example:

3
1 2 3
    
asked by anonymous 22.05.2018 / 01:08

3 answers

0

If I understand correctly you are trying to read numbers and insert into a vector, and these are trying to read this with more than one number ... Here's the example:

#include<stdio.h>
#include<stdlib.h>
#define N 3
 int main()
 {
 int i,x;
 int v[N];
 scanf("%d %d",&i,&x);
 v[0]=i;
 v[1]=x;
 v[2]=0;

 } 

This is a small example ... scanf ("% d% d", & x, & i);
It follows the criteria of% always with the type of variable in case d or i of decimal or integer (% d) repeats more than once if it is more than one variable and then uses &.

    
22.05.2018 / 02:05
0

If you want to read several separate numbers of space, then you want to read everything as a string and basically do split on a particular tab, in this case space. The easiest and most naive way to do this is to use strtok . This function reads a part of a string until you get the indicated tab, and then from the last point it is possible to call again to pick up another bit.

Example:

int quant;
scanf("%d", &quant);
int vetor[quant];
char nums[512]; //espaço para os numeros todos como um texto
scanf(" %[^\n]", nums); //lê todos os números como um texto
char *token = strtok (nums," "); //lê até ao primeiro espaço para a variavel token
int pos = 0; //posição para guardar os numeros no vetor começa em 0

while (token != NULL && pos < quant) {
    vetor[pos++] = atoi(token); //guarda o valor convertido em numero no vetor e avança
    token = strtok (NULL, " "); //lê até ao próximo espaço a partir de onde terminou
}

Text to number conversion was done with atoi . It is also important to note that the remaining strtok other than the first, take as the first parameter NULL to indicate that they are continuations of the previous ones.

Now it would be enough to use the vector of numbers:

int i;
for (i = 0; i < tamanho; ++i){
    printf("%d\n", vetor[i]);
}

That gives you the following output:

1
2
3

See the code working on Ideone

    
22.05.2018 / 02:46
0

I could not find a way to pass a vector or a pointer to vector, to scanf . See if this helps you:

#include <stdio.h>
#include <stdlib.h>

void pegaVetor(int* vetor, int qtd)
{
    int i;

    fflush(stdin);
    printf("Entre o vetor, abaixo:\n");
    for (i = 0; i < qtd; ++i)
    {
        scanf("%d", &vetor[i]);
    }
}

double media(int* vetor, int qtd)
{
    int i;
    int soma = 0;

    for (i = 0; i < qtd; ++i)
    {
        soma += vetor[i];
    }

    return (double)soma / qtd;
}

int main()
{
    int* vetor;
    int n;

    do
    {
        fflush(stdin);
        printf("Digite o numero de elementos (0 para sair): ");
        scanf("%d", &n);
        if (n)
        {
            vetor = (int*)malloc(n * sizeof(int));
            pegaVetor(vetor, n);
            printf("A media aritmetica do vetor e: %f\n", media(vetor, n));
            free(vetor);
        }
    } while (n);

    return 0;
}

The possible input token delimiters, for scanf , are whitespace and / or Enter s.

    
22.05.2018 / 03:04