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;
}

CodePudding user response:

reference 1/f, zhao teacher reply:
 # 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;
}


This value should not be an array can not define the length of the
This will be not check the overflow system problem?

CodePudding user response:

Int a []={0} can't define arrays oh must want to have a fixed length

CodePudding user response:

 # include 
Int main () {

Int * a, n=0, I, c, t;
Printf (" please input array length: \ n ");
The scanf (" % d ", & amp; n);
If (n<1 | | 100000 & lt; N) {
Printf (" array length must be between 1 and 100000 \ n ");
return -1;
}
A=new int [n + 1);
Printf (" please enter the Numbers in turn: \ n ");
for(i=1; i<=n; I++) {
Printf (" a [% d]=", I); The fflush (stdout);
The scanf (" % d ", & amp; A [I]);
}

For (c=1; CFor (I=c + 1; i<=n; I++) {
If (a [c] T=a [c]; A [c]=a, [I]. A [I]=t;
}
}
}

for(i=1; i<=n; I++) {
Printf (" % d \ n ", a [I]);
}
The delete [] a;
return 0;
}
//please input array length:
//4
//please enter the Numbers in turn:
//a [1]=3
//a [2]=9
//a [3]=5
//a [4]=2
//9
//5
//3
//2
//

CodePudding user response:

quoted zhao 4, 4/f, the teacher's reply:
 # include 
Int main () {

Int * a, n=0, I, c, t;
Printf (" please input array length: \ n ");
The scanf (" % d ", & amp; n);
If (n<1 | | 100000 & lt; N) {
Printf (" array length must be between 1 and 100000 \ n ");
return -1;
}
A=new int [n + 1);
Printf (" please enter the Numbers in turn: \ n ");
for(i=1; i<=n; I++) {
Printf (" a [% d]=", I); The fflush (stdout);
The scanf (" % d ", & amp; A [I]);
}

For (c=1; CFor (I=c + 1; i<=n; I++) {
If (a [c]
T=a [c]; A [c]=a, [I]. A [I]=t;
}
}
}

for(i=1; i<=n; I++) {
Printf (" % d \ n ", a [I]);
}
The delete [] a;
return 0;
}
//please input array length:
//4
//please enter the Numbers in turn:
//a [1]=3
//a [2]=9
//a [3]=5
//a [4]=2
//9
//5
//3
//2
//

Probably understand, thank you! ??

CodePudding user response:

reference 3/f, 5250 response:
int a []={0} can't define arrays oh there must be a fixed length

But numerical array is not necessarily to define the length?

CodePudding user response:

Beg to write the code

CodePudding user response:

refer to 7th floor Zhi Zhao response:
beg to write the code

Ha ha 'new beginning??nullnullnullnull
  • Related