#include <stdio.h>
#define SIZE 7
void read_arr(double a[], int len);
void bubble(double a[], int len);
void print_arr(double a[], int len);
// get_p보다 의미가 명확한 이름
int main(void)
{
double x[SIZE];
printf("enter %d double : ", SIZE);
read_arr(x, SIZE);
bubble(x, SIZE);
print_arr(x, SIZE);
return 0;
}
void read_arr(double a[], int len)
{
int i;
for (i = 0; i < len; i++)
scanf("%lf", &a[i]); // double 입력은 %lf
}
void bubble(double a[], int len)
{
int i, j;
double temp; // 배열이 double이므로 temp도 맞춰야함!
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - 1 - i; j++) {
// 이전 오류: i가 아니라 j를 사용해서 옆 원소 비교해야 함
// 이전 오류: j < len이면 a[j+1]에서 배열 범위를 벗어날수있다
if (a[j] < a[j + 1]) { // 내림차순 정렬
// 이전 오류: temp에 먼저 값을 저장하지 않고 swap함..
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
void print_arr(double a[], int len)
{
int i;
printf("arr is : ");
for (i = 0; i < len; i++)
printf("%.2lf ", a[i]);
printf("\n");
}
To embed this project on your website, copy the following code and paste it into your website's HTML: