總結:
1. 陣列中的內容要交換位置,所以應該有一個變動的指標-->maxPos
2. 在定義指標時要初始化!即 `*maxPos=0;`
3. 在做題之前應該先知道互換位置的排序的寫法
#include <stdio.h>
#define ARR 10
int main(){
int a[ARR],n,temp,i;
printf("陣列個數為:");
scanf("%d",&n);
printf("Input %d Numbers:\n", n);
for (i=0;i<n;i++){
scanf("%d", &a[i]);
}
for (i=0;i<n;i++){
for (int j=i+1;j<=n;j++){
if(a[i]<a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("降序為: \n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
4. 接下來,我們看看題目吧!

#include <stdio.h>
#define ARR_SIZE 10
void MaxMinExchang(int a[], int n,int *maxPos)//會變動,存盤,改變,換位置
{
int maxValue = a[0];
//*maxPos=0; maxValue = a[0],
*maxPos=0;
int i, temp;
for ((*maxPos);(*maxPos)<=n;(*maxPos)++)
{
for(i=(*maxPos)+1;i<=n;i++){
if(a[i]>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): ");
scanf("%d", &n);
printf("Input %d Numbers:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
MaxMinExchang(a, n,&maxPos);//&,連接指標
printf("After MaxMinExchange:\n");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}

uj5u.com熱心網友回復:
這里當輸入10值的時候出現一個小問題解決辦法:將ARR-SIZE改寫成11就好了
uj5u.com熱心網友回復:
這里在輸入10個值的時候出現一個小問題解決辦法:將ARR_SIZE 改成11
uj5u.com熱心網友回復:
這么簡單的事情不用寫那么多代碼。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/74516.html
標籤:C++ 語言
上一篇:求大佬幫我這道題怎么做的
下一篇:QQ機器人
