Home > Back-end > A sort of details about the C language
A sort of details about the C language
Time:12-04
Is there a big help to have a look at why changed the definition (array) and order of (loop condition) can lead to bugs! #include Int main () {
Int a []={0}, n=0, I, c;//the problem came from Printf (" please input array length: \ n "); The scanf (" % d ", & amp; n); Printf (" please enter the Numbers in turn: \ n "); for(i=1; i<=n; I++) { Printf (" \ na [% d]=", I); The scanf (" % d ", & amp; A [I]); }
For (c=1; C<=n; C + +) { for(i=1; i{ If (a [I] <=a [I + 1]) { A [I] +=a [I + 1); A [I + 1)=a [I] - a [I + 1); A [I] -=a [I + 1); } } }
for(i=1; i<=n; I++) { Printf (" % d \ n ", a [I]); } return 0; }
CodePudding user response:
# include Int main () {
Int a [100 + 1)={0}, n=0, I, c, t; Printf (" please input array length: \ n "); The scanf (" % d ", & amp; n); If (n<1 | | 100 & lt; N) { Printf (" array length must be between 1 and 100 \ n "); return -1; } Printf (" please enter the Numbers in turn: \ n "); for(i=1; i<=n; I++) { Printf (" \ na [% d]=", I); The fflush (stdout); The scanf (" % d ", & amp; A [I]); }
For (c=1; CFor (I=c + 1; i<=n; I++) { If (a [I] <+ 1=a [I]) { T=a [I]; A [I]=a, [I + 1]. A [I + 1]=t; } } }
for(i=1; i<=n; I++) { Printf (" % d \ n ", a [I]); } return 0; }