5つの整数を大きい順に並び替えるプログラムがうまく動きません。
どこが間違っているのか教えてください。```lang-<c>
コード#include<stdio.h>
int max_index(int a[] , int s , int e){ // 配列の最大値の添え字を返す
int i;
int c;
int max = 0;
for(i = s-1; i < e-1; i++){
if(a[i] > max){
max = a[i];
c = i;}
}
return c;
}
void swap(int a[], int i, int j){ // 配列のi番目とj番目の値を交換する
int x;
x = a[i-1];
a[i-1] = a[j-1];
a[j-1] = x;
}
void sort(int a[], int s, int e){ // 配列を値の大きい順に並び替える
while(s<e){
swap(a,s,max_index(a,s,e)+1);
s = s+1;
}
}
main(){
int a[5] = {3,1,7,4,6};
printf("%d %d %d %d %d\n",a[0],a[1],a[2],a[3],a[4]);
sort(a,1,5);
printf("%d\n%d\n%d\n%d\n%d\n",a[0],a[1],a[2],a[3],a[4]);
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。