c - Regarding scanf and many values to input -


so i'm trying solve http://www.codeabbey.com/index/task_view/sum-in-loop , have 45 random numbers input. i'm coding using c want use scanf function. problem since it's 45 numbers (which separated spaces) want copy paste values program can solve them array. should this:

int x [45]; scanf("%d %d....(x42) %d",&x,&x,...(x42),&x);

or there more efficient way of doing this? (i hope there t_t)

you need not (and should not) write single format string containing 45(or whatever) format specifier, following 45 pointers.

you need use loop.

example:

  • for loop array, hold supplied operands, too

    int x[45] = {0}; int sum = 0; (int = 0; < 45; i++)    //style supported on c99 {    scanf("%d", &x[i]);    sum += x[i];  } printf("sum = %d\n", sum); 
  • for loop without array, won't hold operands, result

    int x = 0; int sum = 0; (int = 0; < 45; i++)    //style supported on c99 {     scanf("%d", &x);     sum += x;  } printf("sum = %d\n", sum); 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -