Home > Back-end >  C # use Pointers to solve the maximum and the minimum switch places
C # use Pointers to solve the maximum and the minimum switch places

Time:09-25

Conclusion:

1. The contents of the array to swap places, so there should be a pointer changes - & gt; MaxPos
2. In the definition to initialize pointer! Namely ` * maxPos=0; `
3. Before making a problem should first know the swap sorting method
 # include & lt; Stdio. H> 
# define ARR 10
Int main () {
Int a (ARR), n, temp, I;
Printf (" array number is: ");
The scanf (" % d ", & amp; n);
Printf (" Input Numbers: % d \ n ", n);
for (i=0; iThe scanf (" % d ", & amp; A [I]);
}
for (i=0; iFor (int j=I + 1; j<=n; J++) {
If (a [I] Temp=a, [I].
A [I]=a, [j].
A [j]=temp;
}
}
}
Printf (" descending order is: \ n ");
for(i=0; iPrintf (" % d \ n ", a [I]);
}
return 0;
}



4. Next, let's look at the title!

 # include & lt; Stdio. H> 
# define ARR_SIZE 10
Void MaxMinExchang (int a [], int n, int * maxPos)//changes, storage, change the location of change,
{
Int maxValue=https://bbs.csdn.net/topics/a [0];
//* maxPos=0; MaxValue=https://bbs.csdn.net/topics/a [0],
* maxPos=0;
Int I, temp;
For ((* maxPos); (* maxPos) & lt;=n; (* maxPos) + +)
{
For (I=(* maxPos) + 1; i<=n; I++) {
If (a [I] & gt; A [* maxPos]) {
Temp=a, [I].
A [I]=a, [* maxPos].
A [* maxPos]=temp;
}
}
}
}
Int main ()
{
Int a [ARR_SIZE], I, n, maxPos;
Printf (" Input n (n<=10) : ");
The scanf (" % d ", & amp; n);
Printf (" Input Numbers: % d \ n ", n);
For (I=0; i {
The scanf (" % d ", & amp; A [I]);
}
MaxMinExchang (a, n, & amp; MaxPos);//& amp;
, the connection pointPrintf (" After MaxMinExchange: \ n ");
for(i=0; i{
Printf (" % d ", a [I]);
}
printf("\n");
return 0;
}




CodePudding user response:

Here when the input value of 10 a small problems
Solution: will ARR - SIZE rewritten to 11

CodePudding user response:

Here when 10 input value to a small problem
Solution: change ARR_SIZE to 11

CodePudding user response:

Such a simple thing don't have to write so much code,
  • Related