Home > Back-end >  Ask why I run the answer is always 35?
Ask why I run the answer is always 35?

Time:09-21

# include
Int main ()
{
Int b, k, sum;
Printf (" please enter a positive integer number: \ n ");
The scanf (" % d ", & amp; K);
Int a, [k].
Printf (" please enter a positive integer: \ n ");
for(b=0; B<=(k - 1); B++)
{
The scanf (" % d ", & amp; [b]);
If (a [b] % 2! Sum==0) sum + a, [b].
}
Printf (" % d ", sum);
return 0;
}

CodePudding user response:

Don't use variable array length, or you can use the dynamic application memory

CodePudding user response:

What do you mean? Novice is not very good

CodePudding user response:

C language array length requirement is constant values,
The scanf (" % d ", & amp; K);
Int a, [k].
This is no good

CodePudding user response:

Then how should I say input quantity? Last time, I also in this way, not seen such a mistake

CodePudding user response:

Some compilers support the way of the definition of array

CodePudding user response:

Run your screenshots to look at

CodePudding user response:

Int a, [k].
To int a [100];//array length with constant
Or change to
Int * a=(int *) malloc (sizeof (int) * k);//dynamic application memory, and finally remember free (a)

In addition, the sum uninitialized, add in front of the for loop
sum=0;
  • Related