Home > Back-end >  [advisory post] C language output maximum number of 10 number
[advisory post] C language output maximum number of 10 number

Time:03-05

Just learning programming, a feeling is very strange problems, hope everybody can help explain,
This is output to the input of the number of five largest number, the result is very strange,,, it would put the following that I <=6 of 6 as a digital input (such as if the input 1 2 3 4 5, there will be a Max=6),
Why is this excuse me?

# include & lt; Stdio. H>
Int main (void)
{
Int a, [5].
Printf (" please enter 5 Numbers: \ n ");
for(int i=1; i<=5; I++)
{
The scanf (" % d ", & amp; A [I]);
}
Int I, temp;
For (I=2; i<=6; I++)
{
If (a [I - 1] & gt;=a [I])
{
Printf (" % d & gt; % d ", a [I - 1], a [I]);

Temp=a, [I - 1].
A [I]=temp;
}
}
Printf (" Max=% d ", a [I]);
}





The bow for you

CodePudding user response:

Who is going to help me

CodePudding user response:

For (I=2; I & lt;=6; I++) here, when I=6, an array of a [6] subscript crossing the line, the value of a [6] is random, just got 6,

CodePudding user response:

Instead I & lt; 6, right,

CodePudding user response:

As a common sense, starting from 0 C language array subscripts
Defined as the array of int a [5], their legal element is
A [0] a [1] a [2] a [3] a [4]

CodePudding user response:

So that is what it boils down to.
But change once, again meet a problem, is that when there are both positive and negative Numbers in the input values, the result output Max=0
Be able to answer it for me?

CodePudding user response:

reference QZJHJXJ reply: 3/f
instead I & lt; 6, right,

So that is what it boils down to.
But change once, again meet a problem, is that when there are both positive and negative Numbers in the input values, the result output Max=0
Be able to answer it for me?

CodePudding user response:

Ok, thank you! This common sense I'll remember,
But change once, again meet a problem, is that when there are both positive and negative Numbers in the input values, the result output Max=0
Be able to answer it for me?

CodePudding user response:

Reference:
 # include & lt; Stdio. H> 
Int main (void)
{
Int a, [5].
Printf (" please enter 5 Numbers: \ n ");
for(int i=0; i<5; I++)//for (int I=1; i<=5; I++)
{
The scanf (" % d ", & amp; A [I]);
}
Int I, temp=a, [0].
for(i=1; i<5; I++)//for (I=2; i<=6; I++)
{
If (temp & lt; A [I])
{
//printf (" % d & gt; % d \ n ", a [I - 1], a [I]);

Temp=a, [I].//to find large Numbers, and record to the temp
//a [I]=temp;
}
}
Printf (" Max=% d ", temp);//printf (" Max=% d ", a [I]);

return 0;
}
  • Related