How do I ask the user to type the vector size?

Asked

Viewed 1,955 times

-1

I’m just using vectors with a defined number using the define or even direct on the variable. How I put the amount of vectors the user chooses?.

example:

    #define NUMERO x

    int main()
    {
    int valor[NUMERO];

I want this x to be a number that the user can type (choose) at the beginning of the program.

  • 3

    Any specific difficulties? Know how to use scanf()?

  • 2

    scanf("%d", &x); and then defines the int valor[x]

  • @Fábiomoral and in case I set using define, how would I choose a value for it? to facilitate code maintenance?

  • (edited the question for better viewing)

  • 1

    What you did didn’t make sense, #define NUMERO 10

  • On an average , I want you to calculate 10 numbers , I have to tell the vector that I want 10 right? , I tried to play in the scanf but I’m not getting:

  • I think the question boils down to "how do I read a C number?" or do I not understand the question ?

  • @Isac no, before that, I want to define a quantity that the user chooses for the vector , I tried to use the scanf and play for the vector and could not.

  • So you already create a vector with a specific size int valor[NUMERO]; If you can read a number, just put the variable you read where it is NUMERO. Maybe it’s confusion about the vector creation syntax not ? That maybe you haven’t fully realized yet

Show 4 more comments

1 answer

0


int main(){
    int x=10;
    int valor[x];
}

It’s the same thing as doing

int main(){
    int valor[10];
}

Or using define

// #define x ValorPredefinido 
#define x 10 /** EXEMPLO, caso saiba que será esse valor **/ 

int main(){
    int valor[x];
}

From the codes below we do not know how much will be needed at the outset.

int main(){
    int x;
    printf("Qual o tamanho do vetor?"); 
    scanf("%d", &x); /** caso seja um valor que o usuário terá de colocar**/
    int valor[x];
}

Another option is to use dynamic memory.

int main(){
  int *valor;
}

Go allocating memory as you need more space.


  • What you can’t do is int valor[x] without having a value, in the examples up when it comes to that line x will have a value
  • So the first code is not possible to change the amount of the value entered in the define , the second in case I type 10 for example the variable x is x[10] then?

  • 1

    Exactly, no sets the value is set for the full program

  • Thank you very much , my teacher said it was not possible for the user to type the amount of vector he wanted, I think he is wrong rsrs

  • 1

    When I learned C also told me that, what you can not define with a x without giving any value, the examples I gave always end up having a value

  • From C99 it is possible to initialize vectors with a variable

  • Initialize a vector without giving a value to the variable previously? It makes no sense, for this purpose it serves dynamic memory. If we initialize as int numero[x] the compiler will not know the memory he will need, now if it is as I mentioned in the examples above you will already know.

Show 1 more comment

Browser other questions tagged

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